summaryrefslogtreecommitdiffstats
path: root/db
diff options
context:
space:
mode:
Diffstat (limited to 'db')
-rw-r--r--db/models.py18
1 files changed, 16 insertions, 2 deletions
diff --git a/db/models.py b/db/models.py
index 7e0afb0..8a01c8c 100644
--- a/db/models.py
+++ b/db/models.py
@@ -1,10 +1,13 @@
1import re
1from datetime import datetime, UTC 2from datetime import datetime, UTC
3from urllib.parse import urlsplit
2from uuid import uuid4, UUID, uuid7 4from uuid import uuid4, UUID, uuid7
3 5
4from sqlalchemy import DateTime 6from sqlalchemy import DateTime
5from sqlmodel import SQLModel, Field, Relationship 7from sqlmodel import SQLModel, Field, Relationship
6from authlib.integrations.sqla_oauth2 import OAuth2ClientMixin, OAuth2AuthorizationCodeMixin, OAuth2TokenMixin 8from starlette.authentication import BaseUser
7 9
10from authentication.mixins import OAuth2ClientMixin, OAuth2AuthorizationCodeMixin, OAuth2TokenMixin
8from authentication.utils import bcrypt_sha256_hash 11from authentication.utils import bcrypt_sha256_hash
9 12
10 13
@@ -24,7 +27,7 @@ class BaseModel(SQLModel):
24 ) 27 )
25 28
26 29
27class User(BaseModel, table=True): 30class User(BaseModel, BaseUser, table=True):
28 username: str = Field(unique=True, index=True) 31 username: str = Field(unique=True, index=True)
29 password: str = Field(nullable=False) 32 password: str = Field(nullable=False)
30 33
@@ -33,6 +36,9 @@ class User(BaseModel, table=True):
33 def set_password(self, value): 36 def set_password(self, value):
34 self.password = bcrypt_sha256_hash(value) 37 self.password = bcrypt_sha256_hash(value)
35 38
39 def is_authenticated(self) -> bool:
40 return True
41
36class Session(BaseModel, table=True): 42class Session(BaseModel, table=True):
37 user_id: UUID = Field(foreign_key='user.id', exclude=True) 43 user_id: UUID = Field(foreign_key='user.id', exclude=True)
38 user: User = Relationship() 44 user: User = Relationship()
@@ -40,8 +46,16 @@ class Session(BaseModel, table=True):
40class ClientApplication(BaseModel, OAuth2ClientMixin, table=True): 46class ClientApplication(BaseModel, OAuth2ClientMixin, table=True):
41 name: str 47 name: str
42 48
49 def check_redirect_uri(self, redirect_uri: str) -> bool:
50 return any(
51 re.fullmatch(re.escape(str(pattern)).replace(r"\*", r"\d+"), redirect_uri)
52 for pattern in self.redirect_uris
53 )
54
43class AuthCode(BaseModel, OAuth2AuthorizationCodeMixin, table=True): 55class AuthCode(BaseModel, OAuth2AuthorizationCodeMixin, table=True):
44 user_id: UUID = Field(foreign_key="user.id", ondelete="CASCADE") 56 user_id: UUID = Field(foreign_key="user.id", ondelete="CASCADE")
57 user: User = Relationship()
45 58
46class AuthToken(BaseModel, OAuth2TokenMixin, table=True): 59class AuthToken(BaseModel, OAuth2TokenMixin, table=True):
47 user_id: UUID = Field(foreign_key="user.id", ondelete="CASCADE") 60 user_id: UUID = Field(foreign_key="user.id", ondelete="CASCADE")
61 user: User = Relationship()