Coverage for fastid / oauth / models.py: 100%
26 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-10-18 16:37 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-10-18 16:37 +0000
1from __future__ import annotations
3from typing import TYPE_CHECKING, Self
4from uuid import UUID # noqa: TCH003
6from sqlalchemy import ForeignKey
7from sqlalchemy.orm import Mapped, mapped_column, relationship
9from fastid.database.base import VersionedEntity
11OAUTH_PROVIDER_NAMES = ("google", "telegram", "yandex", "vk")
13if TYPE_CHECKING:
14 from fastid.auth.models import User
15 from fastid.database.utils import UUIDv7
16 from fastid.oauth.schemas import OpenIDBearer
19class OAuthAccount(VersionedEntity):
20 __tablename__ = "oauth_accounts"
22 user_id: Mapped[UUID] = mapped_column(ForeignKey("users.id"), index=True)
23 provider: Mapped[str]
24 account_id: Mapped[str]
25 access_token: Mapped[str | None]
26 expires_in: Mapped[int | None]
27 scope: Mapped[str | None]
28 id_token: Mapped[str | None]
29 refresh_token: Mapped[str | None]
31 # Personal data
32 email: Mapped[str | None] = mapped_column(index=True)
33 first_name: Mapped[str | None]
34 last_name: Mapped[str | None]
35 display_name: Mapped[str | None]
36 picture: Mapped[str | None]
38 user: Mapped[User] = relationship(back_populates="oauth_accounts")
40 @classmethod
41 def from_open_id(cls, open_id: OpenIDBearer, user_id: UUIDv7) -> Self:
42 return cls(
43 **open_id.model_dump(exclude={"id", "id_token", "token_type", "token_id"}),
44 account_id=open_id.id,
45 user_id=user_id,
46 )
49class OAuthProvider(VersionedEntity):
50 __tablename__ = "oauth_providers"
52 name: Mapped[str] = mapped_column(unique=True)
53 enabled: Mapped[bool] = mapped_column(default=False)
54 client_id: Mapped[str] = mapped_column(default="")
55 client_secret: Mapped[str] = mapped_column(default="")