diff options
Diffstat (limited to 'db')
| -rw-r--r-- | db/models.py | 18 |
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 @@ | |||
| 1 | import re | ||
| 1 | from datetime import datetime, UTC | 2 | from datetime import datetime, UTC |
| 3 | from urllib.parse import urlsplit | ||
| 2 | from uuid import uuid4, UUID, uuid7 | 4 | from uuid import uuid4, UUID, uuid7 |
| 3 | 5 | ||
| 4 | from sqlalchemy import DateTime | 6 | from sqlalchemy import DateTime |
| 5 | from sqlmodel import SQLModel, Field, Relationship | 7 | from sqlmodel import SQLModel, Field, Relationship |
| 6 | from authlib.integrations.sqla_oauth2 import OAuth2ClientMixin, OAuth2AuthorizationCodeMixin, OAuth2TokenMixin | 8 | from starlette.authentication import BaseUser |
| 7 | 9 | ||
| 10 | from authentication.mixins import OAuth2ClientMixin, OAuth2AuthorizationCodeMixin, OAuth2TokenMixin | ||
| 8 | from authentication.utils import bcrypt_sha256_hash | 11 | from authentication.utils import bcrypt_sha256_hash |
| 9 | 12 | ||
| 10 | 13 | ||
| @@ -24,7 +27,7 @@ class BaseModel(SQLModel): | |||
| 24 | ) | 27 | ) |
| 25 | 28 | ||
| 26 | 29 | ||
| 27 | class User(BaseModel, table=True): | 30 | class 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 | |||
| 36 | class Session(BaseModel, table=True): | 42 | class 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): | |||
| 40 | class ClientApplication(BaseModel, OAuth2ClientMixin, table=True): | 46 | class 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 | |||
| 43 | class AuthCode(BaseModel, OAuth2AuthorizationCodeMixin, table=True): | 55 | class 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 | ||
| 46 | class AuthToken(BaseModel, OAuth2TokenMixin, table=True): | 59 | class 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() | ||
