|
| 1 | +import time |
| 2 | +from enum import StrEnum |
| 3 | + |
| 4 | +from authlib.integrations.sqla_oauth2 import ( |
| 5 | + OAuth2AuthorizationCodeMixin, |
| 6 | + OAuth2ClientMixin, |
| 7 | + OAuth2TokenMixin, |
| 8 | +) |
| 9 | +from sqlalchemy import Column, ForeignKey, String, case, func |
| 10 | +from sqlalchemy.dialects import postgresql |
| 11 | +from sqlalchemy.ext.hybrid import hybrid_property |
| 12 | + |
| 13 | +from testgen import settings |
| 14 | +from testgen.common.models import Base |
| 15 | + |
| 16 | + |
| 17 | +class OAuth2ClientType(StrEnum): |
| 18 | + """How an OAuth2 client was provisioned. |
| 19 | +
|
| 20 | + PAT clients are created by the authenticated personal-access-token path and owned by |
| 21 | + a user; EXTERNAL clients are dynamically registered (MCP apps, automation scripts). |
| 22 | + """ |
| 23 | + |
| 24 | + PAT = "pat" |
| 25 | + EXTERNAL = "external" |
| 26 | + |
| 27 | + |
| 28 | +class PersonalAccessTokenStatus(StrEnum): |
| 29 | + """Display status of a personal access token. Revoked takes precedence over expired.""" |
| 30 | + |
| 31 | + ACTIVE = "active" |
| 32 | + REVOKED = "revoked" |
| 33 | + EXPIRED = "expired" |
| 34 | + |
| 35 | + |
| 36 | +class OAuth2Client(Base, OAuth2ClientMixin): |
| 37 | + __tablename__ = "oauth2_clients" |
| 38 | + |
| 39 | + id = Column(postgresql.UUID(as_uuid=True), primary_key=True, server_default="gen_random_uuid()") |
| 40 | + client_type = Column(String(20), nullable=False, default=OAuth2ClientType.EXTERNAL) |
| 41 | + |
| 42 | + # Override to widen — JWTs can exceed 255 chars |
| 43 | + # (the mixin defines client_id as VARCHAR(48) which is fine) |
| 44 | + |
| 45 | + |
| 46 | +class OAuth2AuthorizationCode(Base, OAuth2AuthorizationCodeMixin): |
| 47 | + __tablename__ = "oauth2_authorization_codes" |
| 48 | + |
| 49 | + id = Column(postgresql.UUID(as_uuid=True), primary_key=True, server_default="gen_random_uuid()") |
| 50 | + user_id = Column(postgresql.UUID(as_uuid=True), ForeignKey("auth_users.id", ondelete="CASCADE"), nullable=False) |
| 51 | + |
| 52 | + |
| 53 | +class OAuth2Token(Base, OAuth2TokenMixin): |
| 54 | + __tablename__ = "oauth2_tokens" |
| 55 | + |
| 56 | + id = Column(postgresql.UUID(as_uuid=True), primary_key=True, server_default="gen_random_uuid()") |
| 57 | + user_id = Column(postgresql.UUID(as_uuid=True), ForeignKey("auth_users.id", ondelete="CASCADE"), nullable=True) |
| 58 | + |
| 59 | + # Override to allow longer JWTs as access tokens |
| 60 | + access_token = Column(String(2048), unique=True, nullable=False) |
| 61 | + |
| 62 | + # Set only for personal access tokens; NULL for tokens from the auth-code / MCP flows |
| 63 | + name = Column(String(255), nullable=True) |
| 64 | + |
| 65 | + def is_refresh_token_active(self) -> bool: |
| 66 | + if self.refresh_token_revoked_at: |
| 67 | + return False |
| 68 | + expires_at = self.issued_at + settings.REFRESH_TOKEN_EXPIRES_IN |
| 69 | + return expires_at >= time.time() |
| 70 | + |
| 71 | + @hybrid_property |
| 72 | + def status(self) -> PersonalAccessTokenStatus: |
| 73 | + """Personal access token status, derived from revocation and expiry. |
| 74 | +
|
| 75 | + Instance side reads the app clock; the SQL expression reads the DB clock — |
| 76 | + both are wall-clock "now", evaluated independently. |
| 77 | + """ |
| 78 | + if self.access_token_revoked_at: |
| 79 | + return PersonalAccessTokenStatus.REVOKED |
| 80 | + if self.issued_at + self.expires_in < time.time(): |
| 81 | + return PersonalAccessTokenStatus.EXPIRED |
| 82 | + return PersonalAccessTokenStatus.ACTIVE |
| 83 | + |
| 84 | + @status.expression # type: ignore[no-redef] |
| 85 | + def status(cls): |
| 86 | + return case( |
| 87 | + (cls.access_token_revoked_at > 0, PersonalAccessTokenStatus.REVOKED.value), |
| 88 | + (cls.issued_at + cls.expires_in < func.extract("epoch", func.now()), PersonalAccessTokenStatus.EXPIRED.value), |
| 89 | + else_=PersonalAccessTokenStatus.ACTIVE.value, |
| 90 | + ) |
0 commit comments