firebreak/web/app/auth/session.py
Zach Tatman 200226015e
[codex] Update boilerplate UI design system (#12)
* feat(web): Jinja2 + HTMX + Pico CSS boilerplate

- Add jinja2, aiofiles, jinja2-fragments, python-multipart deps
- Wire up Jinja2Blocks (jinja2-fragments) and static file serving
- Create base.html (Pico Jade theme, HTMX, nav with auth state)
- Create index.html (landing) and dashboard.html (user profile)
- Add get_optional_session dependency for anonymous template context
- Refactor get_current_session to share session lookup logic

* feat(web): replace Pico CSS with Tailwind standalone CLI (dark+pink theme)

- Download Tailwind v4.3.0 standalone CLI binary (gitignored)
- Define custom dark+pink theme tokens in input.css
- Add dev.sh for parallel Tailwind watcher + uvicorn
- Update Dockerfile to build Tailwind during image build
- Update templates with Tailwind utility classes
- Generate minified output.css at build time

* Update boilerplate UI design system

* Refine Forgejo sign-in button

* feat(ui): adjust starting ui template html structure and theme styling

* refactor(ui): normalize Tailwind design tokens

* refactor(ui): fix Forgejo token input styling and validation styling

* refactor(ui): move panel titles and sub titles outside panels

* refactor(ui): vendor forgejo logo svg from /web/static/img
2026-05-26 22:50:25 -04:00

106 lines
2.9 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 _lookup_session(
db: AsyncSession,
myst_session: str | None,
) -> Session | None:
if not myst_session:
return None
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:
return None
sess.last_seen_at = now
await db.commit()
await db.refresh(sess, attribute_names=["user"])
return sess
async def get_current_session(
db: AsyncSession = Depends(get_db),
myst_session: str | None = Cookie(default=None),
) -> Session:
sess = await _lookup_session(db, myst_session)
if sess is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not signed in",
)
return sess
async def get_optional_session(
db: AsyncSession = Depends(get_db),
myst_session: str | None = Cookie(default=None),
) -> Session | None:
return await _lookup_session(db, myst_session)
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()