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 AuthX is designed to work with FastAPI, we provide quick aliases to get the most accessed dependencies.
The following example demonstrate how AuthX aliases can help you reduce verbosity.
from fastapi import FastAPI, Depends
from authx import AuthX
app = FastAPI()
security = AuthX()
security.handle_errors(app)
@app.get('/', dependencies=[Depends(security.access_token_required)])
def root(subject = Depends(security.get_current_subject), token = Depends(security.get_token_from_request)):
...
Aliases
ACCESS_REQUIRED
Returns the access token payload if valid. Enforce the access token validation
example
from fastapi import FastAPI
from authx import AuthX
app = FastAPI()
security = AuthX()
@app.get('/protected')
def protected(payload = security.ACCESS_REQUIRED):
return f"Your Access Token Payload is {payload}"
ACCESS_TOKEN
Returns the encoded access token. DOES NOT Enforce the access token validation
example
from fastapi import FastAPI
from authx import AuthX
app = FastAPI()
security = AuthX()
# Use route dependency to enforce validation in conjunction with ACCESS_TOKEN
@app.get('/protected', dependencies=[security.ACCESS_REQUIRED])
def protected(token = security.ACCESS_TOKEN):
return f"Your Access Token is {token}"
REFRESH_REQUIRED
Returns the refresh token payload if valid. Enforce the refresh token validation
example
from fastapi import FastAPI
from authx import AuthX
app = FastAPI()
security = AuthX()
@app.get('/refresh')
def refresh(payload = security.REFRESH_REQUIRED):
return f"Your Refresh Token Payload is {payload}"
REFRESH_TOKEN
Returns the encoded refresh token. DOES NOT Enforce the refresh token validation
example
from fastapi import FastAPI
from authx import AuthX
app = FastAPI()
security = AuthX()
# Use route dependency to enforce validation in conjunction with REFRESH_TOKEN
@app.get('/refresh', dependencies=[security.REFRESH_REQUIRED])
def refresh(token = security.REFRESH_TOKEN):
return f"Your Refresh Token is {token}"
FRESH_REQUIRED
Returns the access token payload if valid & FRESH. Enforce the access token validation
example
from fastapi import FastAPI
from authx import AuthX
app = FastAPI()
security = AuthX()
@app.get('/protected', dependencies=[security.FRESH_REQUIRED])
def protected():
return "Congratulations! Your have a fresh and valid access token."
CURRENT_SUBJECT
Any
Returns the current subject. Enforce the access token validation
Note
You must set a subject getter to use this dependency. See Callbacks > User Serialization
example
from fastapi import FastAPI
from authx import AuthX
app = FastAPI()
security = AuthX()
@app.get('/whoami')
def whoami(subject = security.CURRENT_SUBJECT):
return f"You are: {subject}"
BUNDLE
/ DEPENDENCY
Returns the AuthXDependency
dependency bundle to be used within the route