Python fastapi.security.OAuth2PasswordBearer() Examples

The following are 3 code examples of fastapi.security.OAuth2PasswordBearer(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module fastapi.security , or try the search function .
Example #1
Source File: fastapi_login.py    From fastapi_login with MIT License 7 votes vote down vote up
def __call__(self, request: Request):
        """
        Provides the functionality to act as a Dependency

        :param Request request: The incoming request, this is set automatically
            by FastAPI
        :return: The user object or None
        :raises: The not_authenticated_exception if set by the user
        """

        if self.not_authenticated_exception is None:
            self.oauth_scheme = OAuth2PasswordBearer(tokenUrl=self.tokenUrl)
        else:
            # we handle Exception raising
            self.oauth_scheme = OAuth2PasswordBearer(tokenUrl=self.tokenUrl, auto_error=False)

        token = await self.oauth_scheme(request)
        if token is not None:
            return await self.get_current_user(token)

        # No token is present in the request and no Exception has been raised yet
        raise self.not_authenticated_exception 
Example #2
Source File: conftest.py    From fastapi-users with MIT License 5 votes vote down vote up
def __init__(self, name: str = "mock"):
        super().__init__(name, logout=True)
        self.scheme = OAuth2PasswordBearer("/login", auto_error=False) 
Example #3
Source File: jwt.py    From fastapi-users with MIT License 5 votes vote down vote up
def __init__(
        self,
        secret: str,
        lifetime_seconds: int,
        tokenUrl: str = "/login",
        name: str = "jwt",
    ):
        super().__init__(name, logout=False)
        self.scheme = OAuth2PasswordBearer(tokenUrl, auto_error=False)
        self.secret = secret
        self.lifetime_seconds = lifetime_seconds