OAuth2PasswordBearer
OAuth2 flow for authentication using a bearer token obtained with a password. An instance of it would be used as a dependency.Parameters
The URL to obtain the OAuth2 token. This would be the path operation that has
OAuth2PasswordRequestForm as a dependency.Security scheme name. It will be included in the generated OpenAPI (e.g. visible at
/docs).The OAuth2 scopes that would be required by the path operations that use this dependency.
Security scheme description. It will be included in the generated OpenAPI (e.g. visible at
/docs).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 or when you want to have authentication that can be provided in one of multiple optional ways (for example, with OAuth2 or in a cookie).The URL to refresh the token and obtain a new one.
Example
OAuth2PasswordRequestForm
This is a dependency class to collect theusername 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.
Parameters
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.username string. The OAuth2 spec requires the exact field name username.password string. The OAuth2 spec requires the exact field name password.A single string with actually several scopes separated by spaces. Each scope is also a string.For example, a single string with:would represent the scopes:
items:readitems:writeusers:readprofileopenid
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.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.Attributes
After initialization, the form will have these attributes:grant_type: The grant type valueusername: The username valuepassword: The password valuescopes: A list of scope strings (parsed from thescopeparameter)client_id: The client ID valueclient_secret: The client secret value
Example
OAuth2PasswordRequestFormStrict
This is a dependency class to collect theusername and password as form data for an OAuth2 password flow.
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, while for OAuth2PasswordRequestForm grant_type is optional.
Parameters
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 string. The OAuth2 spec requires the exact field name username.password string. The OAuth2 spec requires the exact field name password.A single string with actually several scopes separated by spaces.
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.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.OAuth2AuthorizationCodeBearer
OAuth2 flow for authentication using a bearer token obtained with an OAuth2 code flow. An instance of it would be used as a dependency.Parameters
The URL for OAuth2 authorization.
The URL to obtain the OAuth2 token.
The URL to refresh the token and obtain a new one.
Security scheme name. It will be included in the generated OpenAPI (e.g. visible at
/docs).The OAuth2 scopes that would be required by the path operations that use this dependency.
Security scheme description. It will be included in the generated OpenAPI (e.g. visible at
/docs).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 or when authentication can be provided in one of multiple optional ways.Example
SecurityScopes
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.Parameters
This will be filled by FastAPI.
Attributes
The list of all the scopes required by dependencies.
All the scopes required by all the dependencies in a single string separated by spaces, as defined in the OAuth2 specification.