Skip to content

Token Transport handlers

fastauth.transport.TRANSPORT_GETTER module-attribute

TRANSPORT_GETTER = {'headers': BearerTransport, 'cookies': CookieTransport}

fastauth.transport.get_token_from_request

get_token_from_request(config, request=None, refresh=False, locations=None)

Get token from request using the token transport locations specified in the FastAuthConfig.TOKEN_LOCATIONS. If the token is not found in any of the locations, raise a MissingToken exception. Because used FastAPI SecuredBase, in Transport.scheme() method, we need to return callable to resolve the dependency later.

PARAMETER DESCRIPTION
config

FastAuthConfig

TYPE: FastAuthConfig

request

FastAPI Request

TYPE: Request | None DEFAULT: None

refresh

flag to set refresh token type

TYPE: bool DEFAULT: False

locations

pass locations to get token from or default will be used

TYPE: list[str] | None DEFAULT: None

RETURNS DESCRIPTION
Callable[..., Coroutine[Any, Any, str]]

Callable with coroutine to pass to FastAPI Depends

Source code in fastauth/transport/__init__.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def get_token_from_request(
    config: FastAuthConfig,
    request: Request | None = None,
    refresh: bool = False,
    locations: list[str] | None = None,
) -> Callable[..., Coroutine[Any, Any, str]]:
    """
    Get token from request using the token transport locations specified in the FastAuthConfig.TOKEN_LOCATIONS.
    If the token is not found in any of the locations, raise a MissingToken exception.
    Because used FastAPI SecuredBase, in Transport.scheme() method,  we need to return callable to resolve the dependency later.

    :param config: FastAuthConfig
    :param request: FastAPI Request
    :param refresh: flag to set refresh token type
    :param locations: pass locations to get token from or default will be used
    :return: Callable with coroutine to pass to FastAPI Depends
    """

    if locations is None:
        locations = config.TOKEN_LOCATIONS

    parameters: list[Parameter] = []
    for location in locations:
        transport = TRANSPORT_GETTER[location]
        parameters.append(
            Parameter(
                name=location,
                kind=Parameter.POSITIONAL_OR_KEYWORD,
                default=Depends(transport(config).schema(request, refresh)),
            )
        )

    @with_signature(Signature(parameters))
    async def _token_locations(**kwargs) -> str:
        errors: list[exceptions.MissingToken] = []
        for location_name, token in kwargs.items():
            if token is not None:
                return token
            errors.append(
                exceptions.MissingToken(
                    msg=f"Missing token in {location_name}: Not authenticated"
                )
            )
        if errors:
            raise exceptions.MissingToken(msg=[err.detail for err in errors])
        msg = f"No token found in request from {locations}"
        raise exceptions.MissingToken(msg)

    return _token_locations

fastauth.transport.get_login_response async

get_login_response(security, tokens, response=None)

Get login response from the token locations specified in the FastAuthConfig.TOKEN_LOCATIONS.

PARAMETER DESCRIPTION
security

FastAuth instance

TYPE: FastAuth

tokens

TokenResponse instance with access and/or refresh tokens

TYPE: TokenResponse

response

Optional FastAPI Response to modify to

TYPE: Response | None DEFAULT: None

RETURNS DESCRIPTION

FastAPI Response with multiple token locations(eg. headers, cookies) set

Source code in fastauth/transport/__init__.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
async def get_login_response(
    security: "FastAuth", tokens: TokenResponse, response: Response | None = None
):
    """
    Get login response from the token locations specified in the FastAuthConfig.TOKEN_LOCATIONS.
    :param security: FastAuth instance
    :param tokens: TokenResponse instance with access and/or refresh tokens
    :param response: Optional FastAPI Response to modify to
    :return: FastAPI Response with multiple token locations(eg. headers, cookies) set
    """
    for location in security.config.TOKEN_LOCATIONS:
        transport_callable = TRANSPORT_GETTER[location]
        transport: TokenTransport = transport_callable(security.config)
        response = await transport.login_response(
            security,
            tokens,
            response,
        )
    return response

fastauth.transport.get_logout_response async

get_logout_response(security, response=None)

Get logout response from the token locations specified in the FastAuthConfig.TOKEN_LOCATIONS.

PARAMETER DESCRIPTION
security

FastAuth instance

TYPE: FastAuth

response

Optional FastAPI Response to modify to

TYPE: Response | None DEFAULT: None

RETURNS DESCRIPTION

FastAPI Response with unset action for multiple token locations

Source code in fastauth/transport/__init__.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
async def get_logout_response(security: "FastAuth", response: Response | None = None):
    """
    Get logout response from the token locations specified in the FastAuthConfig.TOKEN_LOCATIONS.
    :param security: FastAuth instance
    :param response: Optional FastAPI Response to modify to
    :return: FastAPI Response with unset action for multiple token locations
    """
    for location in security.config.TOKEN_LOCATIONS:
        transport_callable = TRANSPORT_GETTER[location]
        transport: TokenTransport = transport_callable(security.config)
        response = await transport.logout_response(
            security,
            response,
        )
    return response