Public Access
- backend: settings SESSION_COOKIE_SECURE + TRUSTED_NETWORK_AUTO_AUTH, require_session uses secrets.compare_digest and respects trusted-network opt-in, main.py adds require_family_session middleware gating all /api/ routes except auth/admin/email-vote-token paths - docker-compose: pass SESSION_COOKIE_SECURE + TRUSTED_NETWORK_AUTO_AUTH through to backend + scheduler (fixes env-file changes not reaching runtime) - frontend: Ingress path-prefix support (APP_BASE_PATH, BrowserRouter basename, vite base './'), Login redirect honors APP_BASE_PATH - nginx: no-cache headers on root + /assets/ - docs: Home Assistant Ingress install/troubleshooting + plan file - tests: test_auth expects 401 on no-session GET Defaults: SESSION_COOKIE_SECURE=false, TRUSTED_NETWORK_AUTO_AUTH=true (HA is the auth boundary; MealPlanner must not be port-forwarded directly).
68 lines
2.1 KiB
Python
68 lines
2.1 KiB
Python
"""
|
|
Family-shared session login.
|
|
|
|
POST /api/auth/login — body ``{"password": "..."}`` — must match
|
|
``settings.SESSION_PASSWORD``. On success: signs the first FamilyProfile.id
|
|
and writes it as the ``mp_session`` cookie, returns 204.
|
|
|
|
POST /api/auth/logout — clears the cookie, returns 204.
|
|
|
|
The session is intentionally simple: a single shared family password gates
|
|
mutations behind nginx on the trusted network. No per-user auth.
|
|
"""
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Response, status
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.config import settings
|
|
from app.database import get_db
|
|
from app.models import FamilyProfile
|
|
from app.security import SESSION_COOKIE, SESSION_MAX_AGE, issue_session
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
class LoginRequest(BaseModel):
|
|
password: str
|
|
|
|
|
|
@router.post("/login")
|
|
def login(payload: LoginRequest, db: Session = Depends(get_db)):
|
|
expected = settings.SESSION_PASSWORD
|
|
if not expected:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
detail="Session auth not configured",
|
|
)
|
|
if payload.password != expected:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid password"
|
|
)
|
|
|
|
profile = db.query(FamilyProfile).first()
|
|
# If no profile exists yet, sign a placeholder so the cookie still
|
|
# validates; the family-id will be re-issued the first time a profile
|
|
# is created. This avoids login being blocked on first-run.
|
|
family_id = str(profile.id) if profile else "bootstrap"
|
|
cookie_value = issue_session(family_id)
|
|
|
|
response = Response(status_code=status.HTTP_204_NO_CONTENT)
|
|
response.set_cookie(
|
|
key=SESSION_COOKIE,
|
|
value=cookie_value,
|
|
max_age=SESSION_MAX_AGE,
|
|
httponly=True,
|
|
secure=settings.SESSION_COOKIE_SECURE,
|
|
samesite="lax",
|
|
path="/",
|
|
)
|
|
return response
|
|
|
|
|
|
@router.post("/logout")
|
|
def logout():
|
|
response = Response(status_code=status.HTTP_204_NO_CONTENT)
|
|
response.delete_cookie(key=SESSION_COOKIE, path="/")
|
|
return response
|