session.py
1 import jwt 2 from pydantic import BaseModel, Field 3 4 from .user import User 5 from ..utils.docs import example, get_example 6 7 8 class Session(BaseModel): 9 id: str = Field(description="Unique identifier for the session") 10 user_id: str = Field(description="Unique identifier for the user") 11 device_name: str = Field(description="Name of the device") 12 last_update: float = Field(description="Timestamp of the last time an access token was created") 13 14 Config = example( 15 id="74193090-b88c-4984-9e51-da9cd3372e62", 16 user_id=get_example(User)["id"], 17 device_name="test device", 18 last_update=1615725447.182818, 19 ) 20 21 22 class Login(BaseModel): 23 name: str = Field(description="Unique username") 24 password: str = Field(description="Password of the user") 25 mfa_code: str | None = Field(description="MFA TOTP code") 26 recovery_code: str | None = Field(description="Recovery code for MFA") 27 recaptcha_response: str | None = Field( 28 description="Recaptcha response (required if there have been too many failed login attempts)" 29 ) 30 31 32 class LoginResponse(BaseModel): 33 user: User = Field(description="User that was logged in") 34 session: Session = Field(description="Session that was created") 35 access_token: str = Field(description="Access token that can be used to authenticate requests") 36 refresh_token: str = Field(description="Refresh token that can be used to request a new access token") 37 38 Config = example( # noqa: S106 39 user=get_example(User), 40 session=get_example(Session), 41 access_token=jwt.encode( 42 {"user_id": get_example(User)["id"], "session_id": get_example(Session)["id"], "exp": 0}, "secret" 43 ), 44 refresh_token="KN4nF8BsiElQi_OoDYQ2BgVdhVirhTw67vOzfHutjONvazRXLsboZ__UG-oI-II3LoMNv9tgd6YBGYRGxNK7Ug", 45 ) 46 47 48 class OAuthLoginResponse(BaseModel): 49 login: LoginResponse | None = Field(description="Login response if the user was successfully logged in") 50 register_token: str | None = Field( 51 description="OAuth registration token for user creation if no user is linked to the remote account yet" 52 )