When you need to declare dependencies with OAuth2 scopes you use Security().
But you still need to define what is the dependable, the callable that you pass as a parameter to Depends() or Security().
There are multiple tools that you can use to create those dependables, and they get integrated into OpenAPI so they are shown in the automatic docs UI, they can be used by automatically generated clients and SDKs, etc.
This defines the name of the cookie that should be provided in the request with
the API key and integrates that into the OpenAPI documentation. It extracts
the key value sent in the cookie automatically and provides it as the dependency
result. But it doesn't define how to set that cookie.
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
description
Security scheme description.
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
auto_error
By default, if the cookie is not provided, APIKeyCookie will
automatically cancel the request and send the client an error.
If auto_error is set to False, when the cookie is not available,
instead of erroring out, the dependency result will be None.
This is useful when you want to have optional authentication.
It is also useful when you want to have authentication that can be
provided in one of multiple optional ways (for example, in a cookie or
in an HTTP Bearer token).
def__init__(self,*,name:Annotated[str,Doc("Cookie name.")],scheme_name:Annotated[Optional[str],Doc(""" Security scheme name. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,description:Annotated[Optional[str],Doc(""" Security scheme description. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,auto_error:Annotated[bool,Doc(""" By default, if the cookie is not provided, `APIKeyCookie` will automatically cancel the request and send the client an error. If `auto_error` is set to `False`, when the cookie is not available, instead of erroring out, the dependency result will be `None`. This is useful when you want to have optional authentication. It is also useful when you want to have authentication that can be provided in one of multiple optional ways (for example, in a cookie or in an HTTP Bearer token). """),]=True,):self.model:APIKey=APIKey(**{"in":APIKeyIn.cookie},# type: ignore[arg-type]name=name,description=description,)self.scheme_name=scheme_nameorself.__class__.__name__self.auto_error=auto_error
This defines the name of the header that should be provided in the request with
the API key and integrates that into the OpenAPI documentation. It extracts
the key value sent in the header automatically and provides it as the dependency
result. But it doesn't define how to send that key to the client.
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
description
Security scheme description.
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
auto_error
By default, if the header is not provided, APIKeyHeader will
automatically cancel the request and send the client an error.
If auto_error is set to False, when the header is not available,
instead of erroring out, the dependency result will be None.
This is useful when you want to have optional authentication.
It is also useful when you want to have authentication that can be
provided in one of multiple optional ways (for example, in a header or
in an HTTP Bearer token).
def__init__(self,*,name:Annotated[str,Doc("Header name.")],scheme_name:Annotated[Optional[str],Doc(""" Security scheme name. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,description:Annotated[Optional[str],Doc(""" Security scheme description. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,auto_error:Annotated[bool,Doc(""" By default, if the header is not provided, `APIKeyHeader` will automatically cancel the request and send the client an error. If `auto_error` is set to `False`, when the header is not available, instead of erroring out, the dependency result will be `None`. This is useful when you want to have optional authentication. It is also useful when you want to have authentication that can be provided in one of multiple optional ways (for example, in a header or in an HTTP Bearer token). """),]=True,):self.model:APIKey=APIKey(**{"in":APIKeyIn.header},# type: ignore[arg-type]name=name,description=description,)self.scheme_name=scheme_nameorself.__class__.__name__self.auto_error=auto_error
This defines the name of the query parameter that should be provided in the request
with the API key and integrates that into the OpenAPI documentation. It extracts
the key value sent in the query parameter automatically and provides it as the
dependency result. But it doesn't define how to send that API key to the client.
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
description
Security scheme description.
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
auto_error
By default, if the query parameter is not provided, APIKeyQuery will
automatically cancel the request and send the client an error.
If auto_error is set to False, when the query parameter is not
available, instead of erroring out, the dependency result will be
None.
This is useful when you want to have optional authentication.
It is also useful when you want to have authentication that can be
provided in one of multiple optional ways (for example, in a query
parameter or in an HTTP Bearer token).
def__init__(self,*,name:Annotated[str,Doc("Query parameter name."),],scheme_name:Annotated[Optional[str],Doc(""" Security scheme name. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,description:Annotated[Optional[str],Doc(""" Security scheme description. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,auto_error:Annotated[bool,Doc(""" By default, if the query parameter is not provided, `APIKeyQuery` will automatically cancel the request and send the client an error. If `auto_error` is set to `False`, when the query parameter is not available, instead of erroring out, the dependency result will be `None`. This is useful when you want to have optional authentication. It is also useful when you want to have authentication that can be provided in one of multiple optional ways (for example, in a query parameter or in an HTTP Bearer token). """),]=True,):self.model:APIKey=APIKey(**{"in":APIKeyIn.query},# type: ignore[arg-type]name=name,description=description,)self.scheme_name=scheme_nameorself.__class__.__name__self.auto_error=auto_error
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
realm
HTTP Basic authentication realm.
TYPE:Optional[str]DEFAULT:None
description
Security scheme description.
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
auto_error
By default, if the HTTP Basic authentication is not provided (a
header), HTTPBasic will automatically cancel the request and send the
client an error.
If auto_error is set to False, when the HTTP Basic authentication
is not available, instead of erroring out, the dependency result will
be None.
This is useful when you want to have optional authentication.
It is also useful when you want to have authentication that can be
provided in one of multiple optional ways (for example, in HTTP Basic
authentication or in an HTTP Bearer token).
def__init__(self,*,scheme_name:Annotated[Optional[str],Doc(""" Security scheme name. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,realm:Annotated[Optional[str],Doc(""" HTTP Basic authentication realm. """),]=None,description:Annotated[Optional[str],Doc(""" Security scheme description. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,auto_error:Annotated[bool,Doc(""" By default, if the HTTP Basic authentication is not provided (a header), `HTTPBasic` will automatically cancel the request and send the client an error. If `auto_error` is set to `False`, when the HTTP Basic authentication is not available, instead of erroring out, the dependency result will be `None`. This is useful when you want to have optional authentication. It is also useful when you want to have authentication that can be provided in one of multiple optional ways (for example, in HTTP Basic authentication or in an HTTP Bearer token). """),]=True,):self.model=HTTPBaseModel(scheme="basic",description=description)self.scheme_name=scheme_nameorself.__class__.__name__self.realm=realmself.auto_error=auto_error
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
description
Security scheme description.
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
auto_error
By default, if the HTTP Bearer token is not provided (in an
Authorization header), HTTPBearer will automatically cancel the
request and send the client an error.
If auto_error is set to False, when the HTTP Bearer token
is not available, instead of erroring out, the dependency result will
be None.
This is useful when you want to have optional authentication.
It is also useful when you want to have authentication that can be
provided in one of multiple optional ways (for example, in an HTTP
Bearer token or in a cookie).
def__init__(self,*,bearerFormat:Annotated[Optional[str],Doc("Bearer token format.")]=None,scheme_name:Annotated[Optional[str],Doc(""" Security scheme name. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,description:Annotated[Optional[str],Doc(""" Security scheme description. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,auto_error:Annotated[bool,Doc(""" By default, if the HTTP Bearer token is not provided (in an `Authorization` header), `HTTPBearer` will automatically cancel the request and send the client an error. If `auto_error` is set to `False`, when the HTTP Bearer token is not available, instead of erroring out, the dependency result will be `None`. This is useful when you want to have optional authentication. It is also useful when you want to have authentication that can be provided in one of multiple optional ways (for example, in an HTTP Bearer token or in a cookie). """),]=True,):self.model=HTTPBearerModel(bearerFormat=bearerFormat,description=description)self.scheme_name=scheme_nameorself.__class__.__name__self.auto_error=auto_error
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
description
Security scheme description.
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
auto_error
By default, if the HTTP Digest is not provided, HTTPDigest will
automatically cancel the request and send the client an error.
If auto_error is set to False, when the HTTP Digest is not
available, instead of erroring out, the dependency result will
be None.
This is useful when you want to have optional authentication.
It is also useful when you want to have authentication that can be
provided in one of multiple optional ways (for example, in HTTP
Digest or in a cookie).
def__init__(self,*,scheme_name:Annotated[Optional[str],Doc(""" Security scheme name. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,description:Annotated[Optional[str],Doc(""" Security scheme description. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,auto_error:Annotated[bool,Doc(""" By default, if the HTTP Digest is not provided, `HTTPDigest` will automatically cancel the request and send the client an error. If `auto_error` is set to `False`, when the HTTP Digest is not available, instead of erroring out, the dependency result will be `None`. This is useful when you want to have optional authentication. It is also useful when you want to have authentication that can be provided in one of multiple optional ways (for example, in HTTP Digest or in a cookie). """),]=True,):self.model=HTTPBaseModel(scheme="digest",description=description)self.scheme_name=scheme_nameorself.__class__.__name__self.auto_error=auto_error
This is the base class for OAuth2 authentication, an instance of it would be used
as a dependency. All other OAuth2 classes inherit from it and customize it for
each OAuth2 flow.
You normally would not create a new class inheriting from it but use one of the
existing subclasses, and maybe compose them if you want to support multiple flows.
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
description
Security scheme description.
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
auto_error
By default, if no HTTP Authorization header is provided, required for
OAuth2 authentication, it will automatically cancel the request and
send the client an error.
If auto_error is set to False, when the HTTP Authorization header
is not available, instead of erroring out, the dependency result will
be None.
This is useful when you want to have optional authentication.
It is also useful when you want to have authentication that can be
provided in one of multiple optional ways (for example, with OAuth2
or in a cookie).
def__init__(self,*,flows:Annotated[Union[OAuthFlowsModel,Dict[str,Dict[str,Any]]],Doc(""" The dictionary of OAuth2 flows. """),]=OAuthFlowsModel(),scheme_name:Annotated[Optional[str],Doc(""" Security scheme name. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,description:Annotated[Optional[str],Doc(""" Security scheme description. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,auto_error:Annotated[bool,Doc(""" By default, if no HTTP Authorization header is provided, required for OAuth2 authentication, it will automatically cancel the request and send the client an error. If `auto_error` is set to `False`, when the HTTP Authorization header is not available, instead of erroring out, the dependency result will be `None`. This is useful when you want to have optional authentication. It is also useful when you want to have authentication that can be provided in one of multiple optional ways (for example, with OAuth2 or in a cookie). """),]=True,):self.model=OAuth2Model(flows=cast(OAuthFlowsModel,flows),description=description)self.scheme_name=scheme_nameorself.__class__.__name__self.auto_error=auto_error
OAuth2 flow for authentication using a bearer token obtained with an OAuth2 code
flow. An instance of it would be used as a dependency.
PARAMETER
DESCRIPTION
authorizationUrl
TYPE:str
tokenUrl
The URL to obtain the OAuth2 token.
TYPE:str
refreshUrl
The URL to refresh the token and obtain a new one.
TYPE:Optional[str]DEFAULT:None
scheme_name
Security scheme name.
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
scopes
The OAuth2 scopes that would be required by the path operations that
use this dependency.
TYPE:Optional[Dict[str, str]]DEFAULT:None
description
Security scheme description.
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
auto_error
By default, if no HTTP Authorization header is provided, required for
OAuth2 authentication, it will automatically cancel the request and
send the client an error.
If auto_error is set to False, when the HTTP Authorization header
is not available, instead of erroring out, the dependency result will
be None.
This is useful when you want to have optional authentication.
It is also useful when you want to have authentication that can be
provided in one of multiple optional ways (for example, with OAuth2
or in a cookie).
def__init__(self,authorizationUrl:str,tokenUrl:Annotated[str,Doc(""" The URL to obtain the OAuth2 token. """),],refreshUrl:Annotated[Optional[str],Doc(""" The URL to refresh the token and obtain a new one. """),]=None,scheme_name:Annotated[Optional[str],Doc(""" Security scheme name. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,scopes:Annotated[Optional[Dict[str,str]],Doc(""" The OAuth2 scopes that would be required by the *path operations* that use this dependency. """),]=None,description:Annotated[Optional[str],Doc(""" Security scheme description. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,auto_error:Annotated[bool,Doc(""" By default, if no HTTP Authorization header is provided, required for OAuth2 authentication, it will automatically cancel the request and send the client an error. If `auto_error` is set to `False`, when the HTTP Authorization header is not available, instead of erroring out, the dependency result will be `None`. This is useful when you want to have optional authentication. It is also useful when you want to have authentication that can be provided in one of multiple optional ways (for example, with OAuth2 or in a cookie). """),]=True,):ifnotscopes:scopes={}flows=OAuthFlowsModel(authorizationCode=cast(Any,{"authorizationUrl":authorizationUrl,"tokenUrl":tokenUrl,"refreshUrl":refreshUrl,"scopes":scopes,},))super().__init__(flows=flows,scheme_name=scheme_name,description=description,auto_error=auto_error,)
The URL to obtain the OAuth2 token. This would be the path operation
that has OAuth2PasswordRequestForm as a dependency.
TYPE:str
scheme_name
Security scheme name.
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
scopes
The OAuth2 scopes that would be required by the path operations that
use this dependency.
TYPE:Optional[Dict[str, str]]DEFAULT:None
description
Security scheme description.
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
auto_error
By default, if no HTTP Authorization header is provided, required for
OAuth2 authentication, it will automatically cancel the request and
send the client an error.
If auto_error is set to False, when the HTTP Authorization header
is not available, instead of erroring out, the dependency result will
be None.
This is useful when you want to have optional authentication.
It is also useful when you want to have authentication that can be
provided in one of multiple optional ways (for example, with OAuth2
or in a cookie).
def__init__(self,tokenUrl:Annotated[str,Doc(""" The URL to obtain the OAuth2 token. This would be the *path operation* that has `OAuth2PasswordRequestForm` as a dependency. """),],scheme_name:Annotated[Optional[str],Doc(""" Security scheme name. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,scopes:Annotated[Optional[Dict[str,str]],Doc(""" The OAuth2 scopes that would be required by the *path operations* that use this dependency. """),]=None,description:Annotated[Optional[str],Doc(""" Security scheme description. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,auto_error:Annotated[bool,Doc(""" By default, if no HTTP Authorization header is provided, required for OAuth2 authentication, it will automatically cancel the request and send the client an error. If `auto_error` is set to `False`, when the HTTP Authorization header is not available, instead of erroring out, the dependency result will be `None`. This is useful when you want to have optional authentication. It is also useful when you want to have authentication that can be provided in one of multiple optional ways (for example, with OAuth2 or in a cookie). """),]=True,):ifnotscopes:scopes={}flows=OAuthFlowsModel(password=cast(Any,{"tokenUrl":tokenUrl,"scopes":scopes}))super().__init__(flows=flows,scheme_name=scheme_name,description=description,auto_error=auto_error,)
This is a dependency class to collect the username and password as form data
for an OAuth2 password flow.
The OAuth2 specification dictates that for a password flow the data should be
collected using form data (instead of JSON) and that it should have the specific
fields username and password.
All the initialization parameters are extracted from the request.
Note that for OAuth2 the scope items:read is a single scope in an opaque string.
You could have custom internal logic to separate it by colon characters (:) or
similar, and get the two parts items and read. Many applications do that to
group and organize permissions, you could do it as well in your application, just
know that that it is application specific, it's not part of the specification.
PARAMETER
DESCRIPTION
grant_type
The OAuth2 spec says it is required and MUST be the fixed string
"password". Nevertheless, this dependency class is permissive and
allows not passing it. If you want to enforce it, use instead the
OAuth2PasswordRequestFormStrict dependency.
TYPE:Union[str, None]DEFAULT:None
username
username string. The OAuth2 spec requires the exact field name
username.
TYPE:str
password
password string. The OAuth2 spec requires the exact field name
`password".
TYPE:str
scope
A single string with actually several scopes separated by spaces. Each
scope is also a string.
If there's a client_id, it can be sent as part of the form fields.
But the OAuth2 specification recommends sending the client_id and
client_secret (if any) using HTTP Basic auth.
TYPE:Union[str, None]DEFAULT:None
client_secret
If there's a client_password (and a client_id), they can be sent
as part of the form fields. But the OAuth2 specification recommends
sending the client_id and client_secret (if any) using HTTP Basic
auth.
def__init__(self,*,grant_type:Annotated[Union[str,None],Form(pattern="password"),Doc(""" The OAuth2 spec says it is required and MUST be the fixed string "password". Nevertheless, this dependency class is permissive and allows not passing it. If you want to enforce it, use instead the `OAuth2PasswordRequestFormStrict` dependency. """),]=None,username:Annotated[str,Form(),Doc(""" `username` string. The OAuth2 spec requires the exact field name `username`. """),],password:Annotated[str,Form(),Doc(""" `password` string. The OAuth2 spec requires the exact field name `password". """),],scope:Annotated[str,Form(),Doc(""" A single string with actually several scopes separated by spaces. Each scope is also a string. For example, a single string with: ```python "items:read items:write users:read profile openid" ```` would represent the scopes: * `items:read` * `items:write` * `users:read` * `profile` * `openid` """),]="",client_id:Annotated[Union[str,None],Form(),Doc(""" If there's a `client_id`, it can be sent as part of the form fields. But the OAuth2 specification recommends sending the `client_id` and `client_secret` (if any) using HTTP Basic auth. """),]=None,client_secret:Annotated[Union[str,None],Form(),Doc(""" If there's a `client_password` (and a `client_id`), they can be sent as part of the form fields. But the OAuth2 specification recommends sending the `client_id` and `client_secret` (if any) using HTTP Basic auth. """),]=None,):self.grant_type=grant_typeself.username=usernameself.password=passwordself.scopes=scope.split()self.client_id=client_idself.client_secret=client_secret
This is a dependency class to collect the username and password as form data
for an OAuth2 password flow.
The OAuth2 specification dictates that for a password flow the data should be
collected using form data (instead of JSON) and that it should have the specific
fields username and password.
All the initialization parameters are extracted from the request.
The only difference between OAuth2PasswordRequestFormStrict and
OAuth2PasswordRequestForm is that OAuth2PasswordRequestFormStrict requires the
client to send the form field grant_type with the value "password", which
is required in the OAuth2 specification (it seems that for no particular reason),
while for OAuth2PasswordRequestFormgrant_type is optional.
Note that for OAuth2 the scope items:read is a single scope in an opaque string.
You could have custom internal logic to separate it by colon characters (:) or
similar, and get the two parts items and read. Many applications do that to
group and organize permissions, you could do it as well in your application, just
know that that it is application specific, it's not part of the specification.
the OAuth2 spec says it is required and MUST be the fixed string "password".
This dependency is strict about it. If you want to be permissive, use instead the
OAuth2PasswordRequestForm dependency class.
username: username string. The OAuth2 spec requires the exact field name "username".
password: password string. The OAuth2 spec requires the exact field name "password".
scope: Optional string. Several scopes (each one a string) separated by spaces. E.g.
"items:read items:write users:read profile openid"
client_id: optional string. OAuth2 recommends sending the client_id and client_secret (if any)
using HTTP Basic auth, as: client_id:client_secret
client_secret: optional string. OAuth2 recommends sending the client_id and client_secret (if any)
using HTTP Basic auth, as: client_id:client_secret
PARAMETER
DESCRIPTION
grant_type
The OAuth2 spec says it is required and MUST be the fixed string
"password". This dependency is strict about it. If you want to be
permissive, use instead the OAuth2PasswordRequestForm dependency
class.
TYPE:str
username
username string. The OAuth2 spec requires the exact field name
username.
TYPE:str
password
password string. The OAuth2 spec requires the exact field name
`password".
TYPE:str
scope
A single string with actually several scopes separated by spaces. Each
scope is also a string.
If there's a client_id, it can be sent as part of the form fields.
But the OAuth2 specification recommends sending the client_id and
client_secret (if any) using HTTP Basic auth.
TYPE:Union[str, None]DEFAULT:None
client_secret
If there's a client_password (and a client_id), they can be sent
as part of the form fields. But the OAuth2 specification recommends
sending the client_id and client_secret (if any) using HTTP Basic
auth.
def__init__(self,grant_type:Annotated[str,Form(pattern="password"),Doc(""" The OAuth2 spec says it is required and MUST be the fixed string "password". This dependency is strict about it. If you want to be permissive, use instead the `OAuth2PasswordRequestForm` dependency class. """),],username:Annotated[str,Form(),Doc(""" `username` string. The OAuth2 spec requires the exact field name `username`. """),],password:Annotated[str,Form(),Doc(""" `password` string. The OAuth2 spec requires the exact field name `password". """),],scope:Annotated[str,Form(),Doc(""" A single string with actually several scopes separated by spaces. Each scope is also a string. For example, a single string with: ```python "items:read items:write users:read profile openid" ```` would represent the scopes: * `items:read` * `items:write` * `users:read` * `profile` * `openid` """),]="",client_id:Annotated[Union[str,None],Form(),Doc(""" If there's a `client_id`, it can be sent as part of the form fields. But the OAuth2 specification recommends sending the `client_id` and `client_secret` (if any) using HTTP Basic auth. """),]=None,client_secret:Annotated[Union[str,None],Form(),Doc(""" If there's a `client_password` (and a `client_id`), they can be sent as part of the form fields. But the OAuth2 specification recommends sending the `client_id` and `client_secret` (if any) using HTTP Basic auth. """),]=None,):super().__init__(grant_type=grant_type,username=username,password=password,scope=scope,client_id=client_id,client_secret=client_secret,)
This is a special class that you can define in a parameter in a dependency to
obtain the OAuth2 scopes required by all the dependencies in the same chain.
This way, multiple dependencies can have different scopes, even when used in the
same path operation. And with this, you can access all the scopes required in
all those dependencies in a single place.
def__init__(self,scopes:Annotated[Optional[List[str]],Doc(""" This will be filled by FastAPI. """),]=None,):self.scopes:Annotated[List[str],Doc(""" The list of all the scopes required by dependencies. """),]=scopesor[]self.scope_str:Annotated[str,Doc(""" All the scopes required by all the dependencies in a single string separated by spaces, as defined in the OAuth2 specification. """),]=" ".join(self.scopes)
OpenID Connect authentication class. An instance of it would be used as a
dependency.
PARAMETER
DESCRIPTION
openIdConnectUrl
The OpenID Connect URL.
TYPE:str
scheme_name
Security scheme name.
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
description
Security scheme description.
It will be included in the generated OpenAPI (e.g. visible at /docs).
TYPE:Optional[str]DEFAULT:None
auto_error
By default, if no HTTP Authorization header is provided, required for
OpenID Connect authentication, it will automatically cancel the request
and send the client an error.
If auto_error is set to False, when the HTTP Authorization header
is not available, instead of erroring out, the dependency result will
be None.
This is useful when you want to have optional authentication.
It is also useful when you want to have authentication that can be
provided in one of multiple optional ways (for example, with OpenID
Connect or in a cookie).
TYPE:boolDEFAULT:True
Source code in fastapi/security/open_id_connect_url.py
def__init__(self,*,openIdConnectUrl:Annotated[str,Doc(""" The OpenID Connect URL. """),],scheme_name:Annotated[Optional[str],Doc(""" Security scheme name. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,description:Annotated[Optional[str],Doc(""" Security scheme description. It will be included in the generated OpenAPI (e.g. visible at `/docs`). """),]=None,auto_error:Annotated[bool,Doc(""" By default, if no HTTP Authorization header is provided, required for OpenID Connect authentication, it will automatically cancel the request and send the client an error. If `auto_error` is set to `False`, when the HTTP Authorization header is not available, instead of erroring out, the dependency result will be `None`. This is useful when you want to have optional authentication. It is also useful when you want to have authentication that can be provided in one of multiple optional ways (for example, with OpenID Connect or in a cookie). """),]=True,):self.model=OpenIdConnectModel(openIdConnectUrl=openIdConnectUrl,description=description)self.scheme_name=scheme_nameorself.__class__.__name__self.auto_error=auto_error