Dependency Aliases
If you are familiar with FastAPI Dependency Injection system,
you know you need to import the Depends
object to declare a dependency.
Since FastJWT is designed to work with FastAPI, we provide quick aliases to get the most accessed dependencies.
The following example demonstrate how FastJWT aliases can help you reduce verbosity.
from fastapi import FastAPI
from fastapi import Depends
import fastjwt
app = FastAPI()
security = FastJWT()
security.handle_errors(app)
@app.route('/', dependencies=[Depends(security.access_token_required)])
def root(subject = Depends(security.get_current_subject), token = Depends(security.get_token_dep)):
...
Aliases
ACCESS_REQUIRED
Type: TokenPayload
Returns the access token payload if valid. Enforce the access token validation
Example
ACCESS_TOKEN
Type: RequestToken
Returns the encoded access token. DOES NOT Enforce the access token validation
Example
from fastapi import FastAPI
import fastjwt
app = FastAPI()
security = FastJWT()
# Use route dependency to enforce validation in conjunction with ACCESS_TOKEN
@app.route('/protected', dependencies=[security.ACCESS_REQUIRED])
def protected(token = security.ACCESS_TOKEN):
return f"Your Access Token is {token}"
REFRESH_REQUIRED
Type: TokenPayload
Returns the refresh token payload if valid. Enforce the refresh token validation
Example
REFRESH_TOKEN
Type: RequestToken
Returns the encoded refresh token. DOES NOT Enforce the refresh token validation
Example
from fastapi import FastAPI
import fastjwt
app = FastAPI()
security = FastJWT()
# Use route dependency to enforce validation in conjunction with REFRESH_TOKEN
@app.route('/refresh', dependencies=[security.REFRESH_REQUIRED])
def refresh(token = security.REFRESH_TOKEN):
return f"Your Refresh Token is {token}"
FRESH_REQUIRED
Type: TokenPayload
Returns the access token payload if valid & FRESH. Enforce the access token validation
Example
CURRENT_SUBJECT
Type: Any
Returns the current subject. Enforce the access token validation
Note
You must set a subject getter to use this dependency. See Custom Callbacks > User Serialization
Example
BUNDLE
/ DEPENDENCY
Type: FastJWTDeps
Returns the FastJWTDeps
dependency bundle to be used within the route