Skip to content

FastbaseDependency

Source code in fastbase/main.py
class FastbaseDependency:
    engine: AsyncEngine
    User: Type[USER]

    # TESTME: Untested
    @staticmethod
    def verify_idtoken(authorization: Annotated[str, Header()]) -> str:
        """
        Dependency to verify if a Google **idtoken** is valid. Out of the decrypted data only the email is returned.

        !!! tip "Use with"
            Use with the `current_user` dependency which gets the User based on the email.

        ??? example

            ```python
            @app.get('/', email: Annotated[str, Depends(verify_idtoken)]):
                # Header:  Authorization=Bearer abc123...
                ...
            ```

        :param authorization:   Bearer token taken taken from Google idtoken
        :return:                Decrypted token data
        :raises InvalidToken:   Token cannot be used e.g. it's expired, malformed, etc.
        """
        try:
            token = authorization.split(' ')[1]
            token_data = auth.verify_id_token(token)
            return token_data.pop('email')
        except Exception:
            raise InvalidToken()

    # TESTME: Untested
    async def current_user(self, email: Annotated[str, Depends(verify_idtoken)]) -> Type[USER]:
        """
        Dependency for getting the user by their verified idtoken.

        !!! tip "Use with"
            Use with the `verify_idtoken` dependency which gets the User based on the email.

        ??? example

            ```python
            @app.get('/', user: Annotated[User, Depends(current_user)]):
                ...
            ```
        :param email:   Email taken from the bearer token
        :return:        Valid user
        :raises UserNotFoundError:  The user who owns the email doesn't exist
        """
        try:
            async with self.async_session() as session:                 # noqa
                user = await self.User.get_by_email(session, email)
                return user
        except Exception as _:
            raise UserNotFoundError()

Attributes

User: Type[USER] instance-attribute

engine: AsyncEngine instance-attribute

Functions

current_user(email) async

Dependency for getting the user by their verified idtoken.

Use with

Use with the verify_idtoken dependency which gets the User based on the email.

Example
@app.get('/', user: Annotated[User, Depends(current_user)]):
    ...
PARAMETER DESCRIPTION
email

Email taken from the bearer token

TYPE: Annotated[str, Depends(verify_idtoken)]

RETURNS DESCRIPTION
Type[USER]

Valid user

RAISES DESCRIPTION
UserNotFoundError

The user who owns the email doesn't exist

Source code in fastbase/main.py
async def current_user(self, email: Annotated[str, Depends(verify_idtoken)]) -> Type[USER]:
    """
    Dependency for getting the user by their verified idtoken.

    !!! tip "Use with"
        Use with the `verify_idtoken` dependency which gets the User based on the email.

    ??? example

        ```python
        @app.get('/', user: Annotated[User, Depends(current_user)]):
            ...
        ```
    :param email:   Email taken from the bearer token
    :return:        Valid user
    :raises UserNotFoundError:  The user who owns the email doesn't exist
    """
    try:
        async with self.async_session() as session:                 # noqa
            user = await self.User.get_by_email(session, email)
            return user
    except Exception as _:
        raise UserNotFoundError()

verify_idtoken(authorization) staticmethod

Dependency to verify if a Google idtoken is valid. Out of the decrypted data only the email is returned.

Use with

Use with the current_user dependency which gets the User based on the email.

Example
@app.get('/', email: Annotated[str, Depends(verify_idtoken)]):
    # Header:  Authorization=Bearer abc123...
    ...
PARAMETER DESCRIPTION
authorization

Bearer token taken taken from Google idtoken

TYPE: Annotated[str, Header()]

RETURNS DESCRIPTION
str

Decrypted token data

RAISES DESCRIPTION
InvalidToken

Token cannot be used e.g. it's expired, malformed, etc.

Source code in fastbase/main.py
@staticmethod
def verify_idtoken(authorization: Annotated[str, Header()]) -> str:
    """
    Dependency to verify if a Google **idtoken** is valid. Out of the decrypted data only the email is returned.

    !!! tip "Use with"
        Use with the `current_user` dependency which gets the User based on the email.

    ??? example

        ```python
        @app.get('/', email: Annotated[str, Depends(verify_idtoken)]):
            # Header:  Authorization=Bearer abc123...
            ...
        ```

    :param authorization:   Bearer token taken taken from Google idtoken
    :return:                Decrypted token data
    :raises InvalidToken:   Token cannot be used e.g. it's expired, malformed, etc.
    """
    try:
        token = authorization.split(' ')[1]
        token_data = auth.verify_id_token(token)
        return token_data.pop('email')
    except Exception:
        raise InvalidToken()