Programatically resolve dependencies in FastAPI, or, configurable dependencies

Viewed 237

I would like to have a common auth entry point, such as get_authed_user, that loops through a configurable list of authentication dependencies, and the first one that is able to return a user does so. Something like the following:

from app.conf import settings  # this is pydantic's BaseSettings


async def get_authed_user(request: Request, Session = Depends(get_session)):
    for cls_name in settings.AUTHENTICATION_CLASSES:
        method = import_class(cls_name)(auto_error=False)
        user = method()  # how to resolve dependencies here?
        if user:
            return user

    raise HTTP401Error
        

When calling the authentication callables, is there a way to resolve the dependencies of those callables?

0 Answers
Related