Skip to content

Use FastAPI's Depends to inject dependencies #3557

Answered by kleynjan
JacobMaldonado asked this question in Q&A
Discussion options

You must be logged in to vote

Notice this question is still marked as open, maybe this helps - you or someone else.

This is how I inject authentication/authorization as dependencies in the page functions:

from fastapi import HTTPException, Depends

# app.storage.user = { 
#   "authenticated": True,
#   "tenant": "tenant name",
#   "authz": ['something', 'config', 'something_else']
# }

def auth_tenant():
    if app.storage.user.get("authenticated", False):
        tenant = app.storage.user.get("tenant", None)
        if tenant:
            return tenant
    raise HTTPException(status_code=307, detail="Authentication Required", headers={"Location": "/login"})

def check_authz(scope: str):
    async def dependency():
  …

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by rodja
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants