mirror of
https://github.com/ZTatman/myst.git
synced 2026-11-21 04:17:59 +00:00
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>
56 lines
2 KiB
Python
56 lines
2 KiB
Python
from __future__ import annotations
|
|
|
|
import uuid
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import ForeignKey, String, Text, UniqueConstraint, Uuid, func
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.database import Base
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = "users"
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
Uuid, primary_key=True, server_default=func.gen_random_uuid()
|
|
)
|
|
display_name: Mapped[str | None] = mapped_column(String, nullable=True)
|
|
email: Mapped[str | None] = mapped_column(String, nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(server_default=func.now())
|
|
|
|
identities: Mapped[list[LinkedIdentity]] = relationship(
|
|
back_populates="user",
|
|
cascade="all, delete-orphan",
|
|
)
|
|
sessions: Mapped[list["Session"]] = relationship(
|
|
"Session",
|
|
back_populates="user",
|
|
cascade="all, delete-orphan",
|
|
)
|
|
grants: Mapped[list["Grant"]] = relationship(
|
|
"Grant",
|
|
back_populates="user",
|
|
)
|
|
|
|
|
|
class LinkedIdentity(Base):
|
|
__tablename__ = "linked_identities"
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
Uuid, primary_key=True, server_default=func.gen_random_uuid()
|
|
)
|
|
user_id: Mapped[uuid.UUID] = mapped_column(
|
|
Uuid, ForeignKey("users.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
provider: Mapped[str] = mapped_column(String, nullable=False)
|
|
provider_user_id: Mapped[str] = mapped_column(String, nullable=False)
|
|
provider_username: Mapped[str] = mapped_column(String, nullable=False)
|
|
access_token_encrypted: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
access_token_expires_at: Mapped[datetime | None] = mapped_column(nullable=True)
|
|
refresh_token_encrypted: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(server_default=func.now())
|
|
|
|
user: Mapped[User] = relationship(back_populates="identities")
|
|
|
|
__table_args__ = (UniqueConstraint("provider", "provider_user_id"),)
|