diff options
Diffstat (limited to 'db/models.py')
| -rw-r--r-- | db/models.py | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/db/models.py b/db/models.py index 8a01c8c..8946bd2 100644 --- a/db/models.py +++ b/db/models.py | |||
| @@ -1,13 +1,16 @@ | |||
| 1 | import re | 1 | import re |
| 2 | from datetime import datetime, UTC | 2 | from datetime import UTC, datetime |
| 3 | from urllib.parse import urlsplit | 3 | from uuid import UUID, uuid7 |
| 4 | from uuid import uuid4, UUID, uuid7 | ||
| 5 | 4 | ||
| 6 | from sqlalchemy import DateTime | 5 | from sqlalchemy import DateTime |
| 7 | from sqlmodel import SQLModel, Field, Relationship | 6 | from sqlmodel import Field, Relationship, SQLModel |
| 8 | from starlette.authentication import BaseUser | 7 | from starlette.authentication import BaseUser |
| 9 | 8 | ||
| 10 | from authentication.mixins import OAuth2ClientMixin, OAuth2AuthorizationCodeMixin, OAuth2TokenMixin | 9 | from authentication.mixins import ( |
| 10 | OAuth2AuthorizationCodeMixin, | ||
| 11 | OAuth2ClientMixin, | ||
| 12 | OAuth2TokenMixin, | ||
| 13 | ) | ||
| 11 | from authentication.utils import bcrypt_sha256_hash | 14 | from authentication.utils import bcrypt_sha256_hash |
| 12 | 15 | ||
| 13 | 16 | ||
| @@ -21,7 +24,7 @@ class BaseModel(SQLModel): | |||
| 21 | default=None, | 24 | default=None, |
| 22 | nullable=True, | 25 | nullable=True, |
| 23 | sa_column_kwargs={ | 26 | sa_column_kwargs={ |
| 24 | 'onupdate': lambda: datetime.now(tz=UTC), | 27 | "onupdate": lambda: datetime.now(tz=UTC), |
| 25 | }, | 28 | }, |
| 26 | sa_type=DateTime(timezone=True), | 29 | sa_type=DateTime(timezone=True), |
| 27 | ) | 30 | ) |
| @@ -39,10 +42,12 @@ class User(BaseModel, BaseUser, table=True): | |||
| 39 | def is_authenticated(self) -> bool: | 42 | def is_authenticated(self) -> bool: |
| 40 | return True | 43 | return True |
| 41 | 44 | ||
| 45 | |||
| 42 | class Session(BaseModel, table=True): | 46 | class Session(BaseModel, table=True): |
| 43 | user_id: UUID = Field(foreign_key='user.id', exclude=True) | 47 | user_id: UUID = Field(foreign_key="user.id", exclude=True) |
| 44 | user: User = Relationship() | 48 | user: User = Relationship() |
| 45 | 49 | ||
| 50 | |||
| 46 | class ClientApplication(BaseModel, OAuth2ClientMixin, table=True): | 51 | class ClientApplication(BaseModel, OAuth2ClientMixin, table=True): |
| 47 | name: str | 52 | name: str |
| 48 | 53 | ||
| @@ -52,10 +57,12 @@ class ClientApplication(BaseModel, OAuth2ClientMixin, table=True): | |||
| 52 | for pattern in self.redirect_uris | 57 | for pattern in self.redirect_uris |
| 53 | ) | 58 | ) |
| 54 | 59 | ||
| 60 | |||
| 55 | class AuthCode(BaseModel, OAuth2AuthorizationCodeMixin, table=True): | 61 | class AuthCode(BaseModel, OAuth2AuthorizationCodeMixin, table=True): |
| 56 | user_id: UUID = Field(foreign_key="user.id", ondelete="CASCADE") | 62 | user_id: UUID = Field(foreign_key="user.id", ondelete="CASCADE") |
| 57 | user: User = Relationship() | 63 | user: User = Relationship() |
| 58 | 64 | ||
| 65 | |||
| 59 | class AuthToken(BaseModel, OAuth2TokenMixin, table=True): | 66 | class AuthToken(BaseModel, OAuth2TokenMixin, table=True): |
| 60 | user_id: UUID = Field(foreign_key="user.id", ondelete="CASCADE") | 67 | user_id: UUID = Field(foreign_key="user.id", ondelete="CASCADE") |
| 61 | user: User = Relationship() | 68 | user: User = Relationship() |
