Skip to content

AuthBackend

Auth Backend for FastAPI

Source code in fastapi_auth_middleware/middleware.py
class FastAPIAuthBackend(AuthenticationBackend):
    """ Auth Backend for FastAPI """

    def __init__(self, verify_authorization_header: callable):
        """ Auth Backend constructor. Part of an AuthenticationMiddleware as backend.

        Args:
            verify_authorization_header (callable): A function handle that returns a list of scopes and a BaseUser
        """
        self.verify_authorization_header = verify_authorization_header

    async def authenticate(self, conn: HTTPConnection) -> Tuple[AuthCredentials, BaseUser]:
        """ The 'magic' happens here. The authenticate method is invoked each time a route is called that the middleware is applied to.

        Args:
            conn (HTTPConnection): An HTTP connection by FastAPI/Starlette

        Returns:
            Tuple[AuthCredentials, BaseUser]: A tuple of AuthCredentials (scopes) and a user object that is or inherits from BaseUser
        """
        if "Authorization" not in conn.headers:
            raise AuthenticationError("Authorization header missing")

        authorization_header: str = conn.headers["Authorization"]
        scopes, user = self.verify_authorization_header(authorization_header)

        return AuthCredentials(scopes=scopes), user

__init__(self, verify_authorization_header) special

Auth Backend constructor. Part of an AuthenticationMiddleware as backend.

Parameters:

Name Type Description Default
verify_authorization_header callable

A function handle that returns a list of scopes and a BaseUser

required
Source code in fastapi_auth_middleware/middleware.py
def __init__(self, verify_authorization_header: callable):
    """ Auth Backend constructor. Part of an AuthenticationMiddleware as backend.

    Args:
        verify_authorization_header (callable): A function handle that returns a list of scopes and a BaseUser
    """
    self.verify_authorization_header = verify_authorization_header

authenticate(self, conn) async

The 'magic' happens here. The authenticate method is invoked each time a route is called that the middleware is applied to.

Parameters:

Name Type Description Default
conn HTTPConnection

An HTTP connection by FastAPI/Starlette

required

Returns:

Type Description
Tuple[AuthCredentials, BaseUser]

A tuple of AuthCredentials (scopes) and a user object that is or inherits from BaseUser

Source code in fastapi_auth_middleware/middleware.py
async def authenticate(self, conn: HTTPConnection) -> Tuple[AuthCredentials, BaseUser]:
    """ The 'magic' happens here. The authenticate method is invoked each time a route is called that the middleware is applied to.

    Args:
        conn (HTTPConnection): An HTTP connection by FastAPI/Starlette

    Returns:
        Tuple[AuthCredentials, BaseUser]: A tuple of AuthCredentials (scopes) and a user object that is or inherits from BaseUser
    """
    if "Authorization" not in conn.headers:
        raise AuthenticationError("Authorization header missing")

    authorization_header: str = conn.headers["Authorization"]
    scopes, user = self.verify_authorization_header(authorization_header)

    return AuthCredentials(scopes=scopes), user