From fb28c648390464c75ba4903b0e1e0ff2642c8ffb Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Thu, 2 Jul 2026 18:34:39 +0200 Subject: Added users and migrations --- db/models.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'db/models.py') 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 @@ -from uuid import uuid4, UUID +from uuid import uuid4, UUID, uuid7 from sqlmodel import SQLModel, Field +from authlib.integrations.sqla_oauth2 import OAuth2ClientMixin, OAuth2AuthorizationCodeMixin, OAuth2TokenMixin +from authentication.utils import bcrypt_sha256_hash -class BaseModel(SQLModel): - id: int | None = Field(primary_key=True, default=None) -class ExternalModelMixin: - external_id: UUID = Field(index=True, unique=True, default_factory=uuid4) +class BaseModel(SQLModel): + id: UUID | None = Field(primary_key=True, default_factory=uuid7) -class User(BaseModel, ExternalModelMixin, table=True): +class User(BaseModel, table=True): username: str = Field(unique=True, index=True) + password: str = Field(nullable=False) + + email: str + + def set_password(self, value): + self.password = bcrypt_sha256_hash(value) + + +class ClientApplication(BaseModel, OAuth2ClientMixin, table=True): + name: str + +class AuthCode(BaseModel, OAuth2AuthorizationCodeMixin, table=True): + user_id: UUID = Field(foreign_key="user.id", ondelete="CASCADE") + +class AuthToken(BaseModel, OAuth2TokenMixin, table=True): + user_id: UUID = Field(foreign_key="user.id", ondelete="CASCADE") -- cgit v1.2.3