-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.py
More file actions
57 lines (42 loc) · 1.58 KB
/
users.py
File metadata and controls
57 lines (42 loc) · 1.58 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import logging
import uuid
from typing import Optional
from fastapi import Depends, Request
from fastapi_users import BaseUserManager, FastAPIUsers
from fastapi_users.authentication import (
AuthenticationBackend,
CookieTransport,
JWTStrategy
)
from config import settings
from db import get_user_db, FirestoreUserDatabase
from models import User
# --- User Manager ---
class UserManager(BaseUserManager[User, uuid.UUID]):
reset_password_token_secret = settings.RESET_PASSWORD_TOKEN_SECRET
verification_token_secret = settings.VERIFICATION_TOKEN_SECRET
async def on_after_register(self, user: User, request: Optional[Request] = None):
logging.info(f"User {user.id} has registered.")
def parse_id(self, id: str) -> uuid.UUID:
try:
return uuid.UUID(id)
except (ValueError, TypeError) as e:
logging.warning(f"Invalid UUID provided: {id}")
raise ValueError(f"Invalid user ID format: {id}") from e
async def get_user_manager(user_db: FirestoreUserDatabase = Depends(get_user_db)):
yield UserManager(user_db)
# --- Authentication Backend ---
# create secret with openssl rand -hex 32
SECRET = settings.AUTHENTICATION_BACKEND_SECRET
def get_jwt_strategy() -> JWTStrategy:
return JWTStrategy(secret=SECRET, lifetime_seconds=3600)
cookie_transport = CookieTransport(cookie_name="bonds", cookie_max_age=3600)
auth_backend = AuthenticationBackend(
name="jwt",
transport=cookie_transport,
get_strategy=get_jwt_strategy
)
api_users = FastAPIUsers[User, uuid.UUID](
get_user_manager,
[auth_backend]
)