Files
F-r-ldraledigplannerare/backend/app/routers/fetch_data.py
T
jacobh2 02db5430eb Harden the API and the data layer after a security and performance audit
Security
- Every route now requires a login and refuses ids outside the caller's
  workspace with 404: plan, leave-wishes, day-overrides and vacation-days
  were fully anonymous; every other router trusted client-supplied
  workspace/child/parent ids. Scraper sessions are bound to their owner.
- Invitations can no longer be created for a foreign workspace, and a
  redeemer who is already a member (or a full workspace) is refused.
- Production refuses the default JWT secret; API docs are hidden in
  production; a failed migration aborts startup instead of serving.
- Login and register no longer reveal whether an email exists; inactive
  accounts are rejected; /auth/refresh is rate limited.
- The FK scraper writes debug artefacts only when explicitly enabled,
  logs no scraped values or page text, clears the BankID QR as soon as it
  is scanned and purges finished sessions after five minutes.
- Real FK page captures are removed from the tree (history rewrite
  follows); the parser tests use a synthetic fixture.

Performance and correctness
- Plan apply/sync run inside one locked transaction with batched writes
  instead of one commit per row, so a failure mid-batch leaves nothing
  half-applied and concurrent syncs no longer corrupt each other.
- SQLite runs in WAL mode with a busy timeout; 14 indexes cover the
  filters that were full scans; argon2 hashing runs off the event loop;
  responses are gzipped.
- Frontend: a 401 now refreshes the token once and retries, so sessions
  no longer die silently after 15 minutes; every data hook discards
  stale responses; logout clears state even when the server call fails.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CsXCCcDfmV4Sps4ZQSvNEY
2026-09-18 07:54:21 +02:00

217 lines
7.5 KiB
Python

"""HTTP routes for parental-leave data: manual entry + scraper session.
POST /api/parental-leave/manual — manual entry (always available)
GET /api/parental-leave/{personal_data_uuid}/{child_id} — latest snapshot
GET /api/parental-leave/{personal_data_uuid}/{child_id}/history — all snapshots
Scraper endpoints (start / poll / cancel) return 501 when scraper_enabled is
false. When enabled, they drive the FKScraperService session state machine.
"""
from uuid import UUID
from fastapi import APIRouter, HTTPException, status
from pydantic import BaseModel
from starlette.requests import Request
from app.config import settings
from app.dependencies import (
ChildRepositoryDep,
CurrentUserDep,
ParentalLeaveRepositoryDep,
WorkspaceScopeDep,
)
from app.entities.parental_leave import ParentalLeaveFetch
from app.rate_limit import limiter
from app.models.parental_leave import (
ManualFetchRequest,
ParentalLeaveFetchResponse,
)
from app.scrapers.fk_scraper import FKScraperService
from app.services.fetch_data_service import ManualFetchService
from app.utils.errors import ValidationError
router = APIRouter(prefix="/api/parental-leave", tags=["parental-leave"])
scraper_router = APIRouter(prefix="/api/fetch-data", tags=["fetch-data"])
_SCRAPER_DISABLED = HTTPException(
status.HTTP_501_NOT_IMPLEMENTED,
"FK scraping is disabled. Set APP_SCRAPER_ENABLED=true to enable it. "
"Use /api/parental-leave/manual for manual data entry.",
)
# Module-level service instance — sessions are in-memory per process.
_scraper_service = FKScraperService()
# ---------------------------------------------------------------------------
# Parental-leave CRUD (manual entry, always available)
# ---------------------------------------------------------------------------
@router.post(
"/manual", response_model=ParentalLeaveFetchResponse, status_code=status.HTTP_201_CREATED
)
async def save_manual_entry(
payload: ManualFetchRequest,
repo: ParentalLeaveRepositoryDep,
scope: WorkspaceScopeDep,
) -> ParentalLeaveFetchResponse:
scope.check_workspace(payload.workspace_id)
await scope.check_member(payload.personal_data_uuid)
await scope.check_child(payload.child_id)
service = ManualFetchService(repo)
try:
fetch = await service.save_manual_entry(
personal_data_uuid=payload.personal_data_uuid,
workspace_id=payload.workspace_id,
child_id=payload.child_id,
total_days=payload.total_days,
days_used=payload.days_used,
reserved_days_total=payload.reserved_days_total,
reserved_days_left=payload.reserved_days_left,
transferable_days_left=payload.transferable_days_left,
dubbeldagar_left=payload.dubbeldagar_left,
sjukpenning_total=payload.sjukpenning_total,
sjukpenning_used=payload.sjukpenning_used,
lagsta_niva_total=payload.lagsta_niva_total,
lagsta_niva_used=payload.lagsta_niva_used,
)
except ValidationError as exc:
raise HTTPException(status.HTTP_422_UNPROCESSABLE_ENTITY, str(exc)) from exc
return ParentalLeaveFetchResponse.from_entity(fetch)
@router.get("/{personal_data_uuid}/{child_id}", response_model=ParentalLeaveFetchResponse | None)
async def get_latest(
personal_data_uuid: UUID,
child_id: UUID,
repo: ParentalLeaveRepositoryDep,
scope: WorkspaceScopeDep,
) -> ParentalLeaveFetchResponse | None:
await scope.check_member(personal_data_uuid)
await scope.check_child(child_id)
service = ManualFetchService(repo)
fetch = await service.get_latest(personal_data_uuid, child_id)
return ParentalLeaveFetchResponse.from_entity(fetch) if fetch else None
@router.get(
"/{personal_data_uuid}/{child_id}/history", response_model=list[ParentalLeaveFetchResponse]
)
async def get_history(
personal_data_uuid: UUID,
child_id: UUID,
repo: ParentalLeaveRepositoryDep,
scope: WorkspaceScopeDep,
) -> list[ParentalLeaveFetchResponse]:
await scope.check_member(personal_data_uuid)
await scope.check_child(child_id)
service = ManualFetchService(repo)
history = await service.list_history(personal_data_uuid, child_id)
return [ParentalLeaveFetchResponse.from_entity(f) for f in history]
# ---------------------------------------------------------------------------
# Scraper session endpoints
# ---------------------------------------------------------------------------
class _StartRequest(BaseModel):
workspace_id: UUID | None = None
personal_data_uuid: UUID | None = None
child_id: UUID | None = None
class _StartResponse(BaseModel):
session_id: str
status: str
workspace_id: UUID
personal_data_uuid: UUID
child_id: UUID
class _CancelRequest(BaseModel):
session_id: str
@scraper_router.post("/start", response_model=_StartResponse)
@limiter.limit("20/minute")
async def start_fetch(
request: Request,
payload: _StartRequest,
current_user: CurrentUserDep,
scope: WorkspaceScopeDep,
child_repo: ChildRepositoryDep,
parental_leave_repo: ParentalLeaveRepositoryDep,
) -> _StartResponse:
"""`workspace_id` is always the caller's own — `payload.workspace_id` is
accepted on the wire (frontend compatibility) but never trusted; the
caller's own personal_data_uuid/child_id default when omitted, and any
id explicitly supplied is verified against the caller's workspace.
"""
if not settings.scraper_enabled:
raise _SCRAPER_DISABLED
workspace_id = scope.workspace_id
personal_data_uuid = payload.personal_data_uuid or scope.personal_data_uuid
await scope.check_member(personal_data_uuid)
if payload.child_id is not None:
child = await scope.check_child(payload.child_id)
child_id = child.id
else:
children = await child_repo.list_by_workspace(workspace_id)
if not children:
raise HTTPException(
status.HTTP_422_UNPROCESSABLE_ENTITY,
"No child in workspace — add one before fetching.",
)
child_id = children[0].id
# Capture the repo for the background task. The connection lives for the app's
# lifetime (app.state.db_connection), so using it from the background coroutine
# is safe — aiosqlite serialises all operations through its internal queue.
async def _persist(fetch: ParentalLeaveFetch) -> None:
await parental_leave_repo.create(fetch)
session_id = await _scraper_service.start_session(
workspace_id=workspace_id,
personal_data_uuid=personal_data_uuid,
child_id=child_id,
owner_user_id=current_user.id,
persist_callback=_persist,
)
return _StartResponse(
session_id=session_id,
status="launching",
workspace_id=workspace_id,
personal_data_uuid=personal_data_uuid,
child_id=child_id,
)
@scraper_router.get("/poll")
@limiter.limit("120/minute")
async def poll_fetch(
request: Request,
session_id: str,
current_user: CurrentUserDep,
) -> dict:
if not settings.scraper_enabled:
raise _SCRAPER_DISABLED
return await _scraper_service.poll_session(session_id, current_user.id)
@scraper_router.post("/cancel")
@limiter.limit("20/minute")
async def cancel_fetch(
request: Request,
payload: _CancelRequest,
current_user: CurrentUserDep,
) -> dict:
if not settings.scraper_enabled:
raise _SCRAPER_DISABLED
await _scraper_service.cancel_session(payload.session_id, current_user.id)
return {"status": "cancelled"}