Phase 1: Add role, mfa_enforce_pending, must_change_password to users table. Create system_config (singleton) and audit_log tables. Migration 026. Phase 2: Add user_id FK to all 8 data tables (todos, reminders, projects, calendars, people, locations, event_templates, ntfy_sent) with 4-step nullable→backfill→FK→NOT NULL pattern. Migrations 027-034. Phase 3: Harden auth schemas (extra="forbid" on RegisterRequest), add MFA enforcement token serializer with distinct salt, rewrite auth router with require_role() factory and registration endpoint. Phase 4: Scope all 12 routers by user_id, fix dependency type bugs, bound weather cache (SEC-15), multi-user ntfy dispatch. Phase 5: Create admin router (14 endpoints), admin schemas, audit service, rate limiting in nginx. SEC-08 CSRF via X-Requested-With. Phase 6: Update frontend types, useAuth hook (role/isAdmin/register), App.tsx (AdminRoute guard), Sidebar (admin link), api.ts (XHR header). Security findings addressed: SEC-01, SEC-02, SEC-03, SEC-04, SEC-05, SEC-06, SEC-07, SEC-08, SEC-12, SEC-13, SEC-15. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
46 lines
1.8 KiB
Python
46 lines
1.8 KiB
Python
from sqlalchemy import String, Boolean, Integer, func
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
from datetime import datetime
|
|
from app.database import Base
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = "users"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, index=True)
|
|
username: Mapped[str] = mapped_column(String(50), unique=True, nullable=False, index=True)
|
|
password_hash: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
|
|
# MFA — populated in Track B
|
|
# String(500) because Fernet-encrypted secrets are longer than raw base32
|
|
totp_secret: Mapped[str | None] = mapped_column(String(500), nullable=True, default=None)
|
|
totp_enabled: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
|
|
# Account lockout
|
|
failed_login_count: Mapped[int] = mapped_column(Integer, default=0)
|
|
locked_until: Mapped[datetime | None] = mapped_column(nullable=True, default=None)
|
|
|
|
# Account state
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
|
|
# RBAC
|
|
role: Mapped[str] = mapped_column(
|
|
String(30), nullable=False, default="standard", server_default="standard"
|
|
)
|
|
|
|
# MFA enforcement (admin can toggle; checked at login)
|
|
mfa_enforce_pending: Mapped[bool] = mapped_column(
|
|
Boolean, default=False, server_default="false"
|
|
)
|
|
|
|
# Forced password change (set after admin reset)
|
|
must_change_password: Mapped[bool] = mapped_column(
|
|
Boolean, default=False, server_default="false"
|
|
)
|
|
|
|
# Audit
|
|
created_at: Mapped[datetime] = mapped_column(default=func.now())
|
|
updated_at: Mapped[datetime] = mapped_column(default=func.now(), onupdate=func.now())
|
|
last_login_at: Mapped[datetime | None] = mapped_column(nullable=True, default=None)
|
|
last_password_change_at: Mapped[datetime | None] = mapped_column(nullable=True, default=None)
|