myst/web/app/models/grant.py
ZTatman 5fec547983 feat(web): Forgejo OAuth, Myst sessions, and auth schema
Implement Myst-owned sessions after Forgejo OAuth per auth design: User and
LinkedIdentity with encrypted OAuth tokens, Session in its own module,
Grant token_hash plus user_id and expires_at. Add auth/identity and
auth/session helpers, Forgejo login/callback/logout routes, and a minimal
/dashboard that requires get_current_session.

Move get_db to app/deps.py so routers and auth avoid importing app.main.
Add httpx and cryptography; extend Settings with OAuth client fields,
optional MYST_PUBLIC_BASE_URL for redirect_uri, and OAUTH_TOKEN_ENCRYPTION_KEY
(Fernet) required when persisting OAuth tokens.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-12 15:12:17 -04:00

40 lines
1.4 KiB
Python

from __future__ import annotations
import uuid
from datetime import datetime
from typing import Literal, get_args
from sqlalchemy import Enum, ForeignKey, String, Uuid, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database import Base
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"), 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")