myst/web/app/auth/session.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

92 lines
2.6 KiB
Python

from __future__ import annotations
import hashlib
import secrets
import uuid
from datetime import UTC, datetime, timedelta
from fastapi import Cookie, Depends, HTTPException, Response, status
from sqlalchemy import select, update
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload
from app.deps import get_db
from app.models.session import Session
SESSION_COOKIE = "myst_session"
SESSION_DURATION_DAYS = 7
async def create_session(
db: AsyncSession, response: Response, user_id: uuid.UUID, secure: bool = True
) -> None:
raw_token = secrets.token_hex(32)
token_hash = hashlib.sha256(raw_token.encode()).hexdigest()
expires_at = datetime.now(UTC) + timedelta(days=SESSION_DURATION_DAYS)
row = Session(
user_id=user_id,
token_hash=token_hash,
expires_at=expires_at,
)
db.add(row)
await db.commit()
response.set_cookie(
key=SESSION_COOKIE,
value=raw_token,
httponly=True,
samesite="lax",
secure=secure,
max_age=60 * 60 * 24 * SESSION_DURATION_DAYS,
path="/",
)
async def get_current_session(
db: AsyncSession = Depends(get_db),
myst_session: str | None = Cookie(default=None),
) -> Session:
if not myst_session:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not signed in",
)
token_hash = hashlib.sha256(myst_session.encode()).hexdigest()
now = datetime.now(UTC)
stmt = (
select(Session)
.options(selectinload(Session.user))
.where(
Session.token_hash == token_hash,
Session.expires_at > now,
Session.revoked_at.is_(None),
)
)
result = await db.execute(stmt)
sess = result.scalar_one_or_none()
if sess is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Session invalid or expired",
)
sess.last_seen_at = now
await db.commit()
await db.refresh(sess, attribute_names=["user"])
return sess
async def revoke_session(db: AsyncSession, response: Response, myst_session: str | None) -> None:
response.delete_cookie(
key=SESSION_COOKIE,
path="/",
samesite="lax",
httponly=True,
)
if not myst_session:
return
token_hash = hashlib.sha256(myst_session.encode()).hexdigest()
await db.execute(
update(Session)
.where(Session.token_hash == token_hash, Session.revoked_at.is_(None))
.values(revoked_at=datetime.now(UTC))
)
await db.commit()