myst/web/app/models/grant.py
ZTatman b6486b7d5a fix(web): address CodeRabbit review comments
- Fix logout cookie not reaching browser (same pattern as callback)
- Add cascade delete on Grant.user_id FK
- Bump httpx to >=0.28.1
- Return plain dict from /dashboard instead of ORM session
- Add decrypt functions and key validation to crypto.py
- Constrain session token_hash to String(64), add expires_at index
- Move Session import to TYPE_CHECKING in user.py
- Add indexes on email and LinkedIdentity.user_id
2026-05-15 15:11:58 -04:00

43 lines
1.5 KiB
Python

from __future__ import annotations
import uuid
from datetime import datetime
from typing import TYPE_CHECKING, Literal, get_args
from sqlalchemy import Enum, ForeignKey, String, Uuid, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database import Base
if TYPE_CHECKING:
from .user import User
GrantType = Literal["public", "private"]
class Grant(Base):
__tablename__ = "grants"
id: Mapped[uuid.UUID] = mapped_column(
Uuid,
primary_key=True,
server_default=func.gen_random_uuid(),
)
token_hash: Mapped[str] = mapped_column(String, unique=True, nullable=False, index=True)
user_id: Mapped[uuid.UUID] = mapped_column(
Uuid, ForeignKey("users.id", ondelete="CASCADE"), nullable=False
)
created_at: Mapped[datetime] = mapped_column(server_default=func.now())
expires_at: Mapped[datetime] = mapped_column(nullable=False)
revoked_at: Mapped[datetime | None] = mapped_column(nullable=True)
repo_owner: Mapped[str] = mapped_column(String, nullable=False)
repo_name: Mapped[str] = mapped_column(String, nullable=False)
commit_sha: Mapped[str] = mapped_column(String, nullable=False)
grant_type: Mapped[GrantType] = mapped_column(
Enum(*get_args(GrantType), name="grant_type_enum"),
nullable=False,
default="public",
)
recipient_email: Mapped[str | None] = mapped_column(String, nullable=True)
user: Mapped[User] = relationship("User", back_populates="grants")