summaryrefslogtreecommitdiffstats
path: root/db/models.py
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <tom@vanderlee.io>2026-07-03 15:49:08 +0200
committerGravatar Tom van der Lee <tom@vanderlee.io>2026-07-03 15:49:14 +0200
commit7784c60c03ec277b456db6e2709383e302d9382b (patch)
treea008def666f04041fe77740693d42cfef89af558 /db/models.py
parentfb28c648390464c75ba4903b0e1e0ff2642c8ffb (diff)
downloadserver-7784c60c03ec277b456db6e2709383e302d9382b.tar.gz
server-7784c60c03ec277b456db6e2709383e302d9382b.tar.bz2
server-7784c60c03ec277b456db6e2709383e302d9382b.zip
Added basic oauth flow
Diffstat (limited to 'db/models.py')
-rw-r--r--db/models.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/db/models.py b/db/models.py
index e758671..7e0afb0 100644
--- a/db/models.py
+++ b/db/models.py
@@ -1,6 +1,8 @@
1from datetime import datetime, UTC
1from uuid import uuid4, UUID, uuid7 2from uuid import uuid4, UUID, uuid7
2 3
3from sqlmodel import SQLModel, Field 4from sqlalchemy import DateTime
5from sqlmodel import SQLModel, Field, Relationship
4from authlib.integrations.sqla_oauth2 import OAuth2ClientMixin, OAuth2AuthorizationCodeMixin, OAuth2TokenMixin 6from authlib.integrations.sqla_oauth2 import OAuth2ClientMixin, OAuth2AuthorizationCodeMixin, OAuth2TokenMixin
5 7
6from authentication.utils import bcrypt_sha256_hash 8from authentication.utils import bcrypt_sha256_hash
@@ -8,6 +10,19 @@ from authentication.utils import bcrypt_sha256_hash
8 10
9class BaseModel(SQLModel): 11class BaseModel(SQLModel):
10 id: UUID | None = Field(primary_key=True, default_factory=uuid7) 12 id: UUID | None = Field(primary_key=True, default_factory=uuid7)
13 created: datetime = Field(
14 default_factory=lambda: datetime.now(tz=UTC),
15 sa_type=DateTime(timezone=True),
16 )
17 updated: datetime = Field(
18 default=None,
19 nullable=True,
20 sa_column_kwargs={
21 'onupdate': lambda: datetime.now(tz=UTC),
22 },
23 sa_type=DateTime(timezone=True),
24 )
25
11 26
12class User(BaseModel, table=True): 27class User(BaseModel, table=True):
13 username: str = Field(unique=True, index=True) 28 username: str = Field(unique=True, index=True)
@@ -18,6 +33,9 @@ class User(BaseModel, table=True):
18 def set_password(self, value): 33 def set_password(self, value):
19 self.password = bcrypt_sha256_hash(value) 34 self.password = bcrypt_sha256_hash(value)
20 35
36class Session(BaseModel, table=True):
37 user_id: UUID = Field(foreign_key='user.id', exclude=True)
38 user: User = Relationship()
21 39
22class ClientApplication(BaseModel, OAuth2ClientMixin, table=True): 40class ClientApplication(BaseModel, OAuth2ClientMixin, table=True):
23 name: str 41 name: str