-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbackends.py
More file actions
40 lines (30 loc) · 1.08 KB
/
backends.py
File metadata and controls
40 lines (30 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from functools import lru_cache
from typing import List
from fastapi_users.authentication import (
AuthenticationBackend,
BearerTransport,
CookieTransport,
JWTStrategy,
)
from fastapi_users.authentication.strategy.base import Strategy
from fastapi_users.authentication.transport.base import Transport
from utils import get_settings
@lru_cache
def get_backends() -> List[AuthenticationBackend]:
return [
AuthenticationBackend(
name="jwt", transport=_get_bearer_transport(), get_strategy=_get_strategy
),
AuthenticationBackend(
name="cookie", transport=_get_cookie_transport(), get_strategy=_get_strategy
),
]
def _get_bearer_transport() -> Transport:
return BearerTransport(tokenUrl="auth/jwt/login")
def _get_cookie_transport() -> Transport:
return CookieTransport(cookie_max_age=get_settings().AUTH_ACCESS_TOKEN_EXPIRE_SECONDS)
def _get_strategy() -> Strategy:
return JWTStrategy(
secret=get_settings().AUTH_JWT_SECRET,
lifetime_seconds=get_settings().AUTH_ACCESS_TOKEN_EXPIRE_SECONDS,
)