diff options
Diffstat (limited to 'db/models.py')
| -rw-r--r-- | db/models.py | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/db/models.py b/db/models.py index b66f5bc..e758671 100644 --- a/db/models.py +++ b/db/models.py | |||
| @@ -1,13 +1,29 @@ | |||
| 1 | from uuid import uuid4, UUID | 1 | from uuid import uuid4, UUID, uuid7 |
| 2 | 2 | ||
| 3 | from sqlmodel import SQLModel, Field | 3 | from sqlmodel import SQLModel, Field |
| 4 | from authlib.integrations.sqla_oauth2 import OAuth2ClientMixin, OAuth2AuthorizationCodeMixin, OAuth2TokenMixin | ||
| 4 | 5 | ||
| 6 | from authentication.utils import bcrypt_sha256_hash | ||
| 5 | 7 | ||
| 6 | class BaseModel(SQLModel): | ||
| 7 | id: int | None = Field(primary_key=True, default=None) | ||
| 8 | 8 | ||
| 9 | class ExternalModelMixin: | 9 | class BaseModel(SQLModel): |
| 10 | external_id: UUID = Field(index=True, unique=True, default_factory=uuid4) | 10 | id: UUID | None = Field(primary_key=True, default_factory=uuid7) |
| 11 | 11 | ||
| 12 | class User(BaseModel, ExternalModelMixin, table=True): | 12 | class User(BaseModel, table=True): |
| 13 | username: str = Field(unique=True, index=True) | 13 | username: str = Field(unique=True, index=True) |
| 14 | password: str = Field(nullable=False) | ||
| 15 | |||
| 16 | email: str | ||
| 17 | |||
| 18 | def set_password(self, value): | ||
| 19 | self.password = bcrypt_sha256_hash(value) | ||
| 20 | |||
| 21 | |||
| 22 | class ClientApplication(BaseModel, OAuth2ClientMixin, table=True): | ||
| 23 | name: str | ||
| 24 | |||
| 25 | class AuthCode(BaseModel, OAuth2AuthorizationCodeMixin, table=True): | ||
| 26 | user_id: UUID = Field(foreign_key="user.id", ondelete="CASCADE") | ||
| 27 | |||
| 28 | class AuthToken(BaseModel, OAuth2TokenMixin, table=True): | ||
| 29 | user_id: UUID = Field(foreign_key="user.id", ondelete="CASCADE") | ||
