Main Dependencies
Request token dependencies
Sometimes, you may need to access the data relative to JWT authentication in request. such data might include, the encoded JWT, the CSRF double submit token, the location of the JWT...
To retrieve these information from request, FastJWT provides a FastJWT.get_token_from_request
get_token_from_request
allow you to specify the token type you wish to retrieve with the type
argument and to enforce the token availability with the optional
argument
Please note that even if optional
is set to False
. The route will raise an error only because no token is available in request and not because the token in request has been invalidated.
get_token_from_request
dependencies does not provide token validation. This dependency only look for token's presence in request
Token validation dependencies
FastJWT provides 3 main dependencies for token requirements
These methods are FastJWT properties returning a FastAPI dependency Callable[[Request], TokenPayload]
. When these dependencies are resolved, they return a TokenPayload
FastJWT.access_token_required
access_token_required
is a property returning a FastAPI dependency to enforce the presence and validity of an access
token in request. This dependency will apply the following verification:
- JWT Validation: verify
exp
,iat
,nbf
,iss
,aud
claims - Token type verification:
access
only - CSRF double submit verification: if CSRF enabled and token location in cookies
- Token freshness: not required for this dependency
FastJWT.refresh_token_required
refresh_token_required
is a property returning a FastAPI dependency to enforce the presence and validity of a refersh
token in request. This dependency will apply the following verification:
- JWT Validation: verify
exp
,iat
,nbf
,iss
,aud
claims - Token type verification:
request
only - CSRF double submit verification: if CSRF enabled and token location in cookies
- Token freshness: not required for this dependency
FastJWT.fresh_token_required
access_token_required
is a property returning a FastAPI dependency to enforce the presence and validity of an access
token in request. It also needs the token to be fresh
This dependency will apply the following verification:
- JWT Validation: verify
exp
,iat
,nbf
,iss
,aud
claims - Token type verification:
access
only - CSRF double submit verification: if CSRF enabled and token location in cookies
- Token freshness: not required for this dependency
Additional token dependency
In addition to the 3 dependencies specified above, FastJWT provides FastJWT.token_required
as an additional layer of customization for token requirements
We have regenrated the main token dependencies from the FastJWT.token_required
method in the highlighted. FastJWT.token_required
returns a Callable to be used as a dependency.
(str, bool, bool, Optional[bool]) -> Callable[[Request], TokenPayload]
As a custom token validation dependency, we have created the no_csrf_required
. This dependency requires a valid access
token in request, but it will not execute CSRF validation if the token is located in cookies.
WIP
The verify_csrf
argument is a Optional boolean to enable/disable CSRF protection. If None
it uses the default FJWTConfig.JWT_COOKIE_CSRF_PROTECT
setting