summaryrefslogtreecommitdiffstats
path: root/db/models.py
blob: e75867116352dbbaa9e08698e795a237e12a8409 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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: UUID | None = Field(primary_key=True, default_factory=uuid7)

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")