1
votes

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?

If the number of authentication dependencies is limited, you could simply list them as dependencies in the parameters of the function and check weather one of them is not None. Otherwise, the approach you use seems correct. Just add it as dependency to your entry points - lsabi