diff options
Diffstat (limited to 'authentication/mixins.py')
| -rw-r--r-- | authentication/mixins.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/authentication/mixins.py b/authentication/mixins.py new file mode 100644 index 0000000..3d6a91f --- /dev/null +++ b/authentication/mixins.py | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | import json | ||
| 2 | import time | ||
| 3 | |||
| 4 | from authlib.integrations.sqla_oauth2 import OAuth2ClientMixin as BaseOAuth2ClientMixin, OAuth2AuthorizationCodeMixin as BaseOAuth2AuthorizationCodeMixin, OAuth2TokenMixin as BaseOAuth2TokenMixin | ||
| 5 | from sqlalchemy.sql.sqltypes import Text | ||
| 6 | from sqlmodel import Field | ||
| 7 | |||
| 8 | |||
| 9 | class OAuth2ClientMixin(BaseOAuth2ClientMixin): | ||
| 10 | client_id: str = Field(max_length=48, index=True, nullable=True) | ||
| 11 | client_secret: str = Field(max_length=120, nullable=True) | ||
| 12 | client_id_issued_at: int = Field(nullable=False, default=0) | ||
| 13 | client_secret_expires_at: int = Field(nullable=False, default=0) | ||
| 14 | client_metadata_field: str = Field(sa_type=Text, sa_column_kwargs={ | ||
| 15 | 'name': 'client_metadata' | ||
| 16 | }, nullable=True) | ||
| 17 | |||
| 18 | _client_metadata = None | ||
| 19 | |||
| 20 | @property | ||
| 21 | def client_metadata(self) -> dict: | ||
| 22 | if self.client_metadata_field: | ||
| 23 | return json.loads(self.client_metadata_field) | ||
| 24 | return {} | ||
| 25 | |||
| 26 | def set_client_metadata(self, metadata: dict): | ||
| 27 | self.client_metadata_field = json.dumps(metadata) | ||
| 28 | |||
| 29 | |||
| 30 | class OAuth2AuthorizationCodeMixin(BaseOAuth2AuthorizationCodeMixin): | ||
| 31 | code: str = Field(max_length=120, unique=True, nullable=False) | ||
| 32 | client_id: str = Field(max_length=48, nullable=True) | ||
| 33 | redirect_uri: str = Field(sa_type=Text, default="", nullable=True) | ||
| 34 | response_type: str = Field(sa_type=Text, default="", nullable=True) | ||
| 35 | scope: str = Field(sa_type=Text, default="", nullable=True) | ||
| 36 | nonce: str = Field(sa_type=Text, nullable=True) | ||
| 37 | auth_time: int = Field(nullable=False, default_factory=lambda: int(time.time())) | ||
| 38 | acr: str = Field(sa_type=Text, nullable=True) | ||
| 39 | amr: str = Field(sa_type=Text, nullable=True) | ||
| 40 | |||
| 41 | code_challenge: str = Field(sa_type=Text, nullable=True) | ||
| 42 | code_challenge_method: str = Field(max_length=48, nullable=True) | ||
| 43 | |||
| 44 | |||
| 45 | class OAuth2TokenMixin(BaseOAuth2TokenMixin): | ||
| 46 | client_id: str = Field(max_length=48, nullable=True) | ||
| 47 | token_type: str = Field(max_length=40, nullable=True) | ||
| 48 | access_token: str = Field(max_length=255, unique=True, nullable=False) | ||
| 49 | refresh_token: str = Field(max_length=255, index=True, nullable=True) | ||
| 50 | scope: str = Field(sa_type=Text, default="", nullable=True) | ||
| 51 | issued_at: int = Field(nullable=False, default_factory=lambda: int(time.time())) | ||
| 52 | access_token_revoked_at: int = Field(nullable=False, default=0) | ||
| 53 | refresh_token_revoked_at: int = Field(nullable=False, default=0) | ||
| 54 | expires_in: int = Field(nullable=False, default=0) | ||
