e39521d51d
- TASK-03-01 server/auth/passwords.py: argon2id via argon2-cffi
PasswordHasher (t=3, m=64MiB, p=4 — exceeds OWASP). hash/verify/
needs_rehash; verify returns False on mismatch (uniform 401 path).
- TASK-03-02 server/auth/cookies.py: get_session_middleware_kwargs()
→ Starlette SessionMiddleware (itsdangerous HMAC-SHA256, D-056).
Cookie praxis_op, httpOnly, SameSite=strict, max_age=28800 (8h).
PRAXIS_COOKIE_SECURE default true; false logs WARNING (R-AUTH-01).
G-031 reframe documented: k-anon defense-in-depth is the PRIMARY
mitigation (sniffed cookie → no PII); secure flag is SECONDARY.
- TASK-03-03 server/auth/rate_limit.py: slowapi Limiter (in-memory,
D-041), 5/minute per IP on login. reset_login_rate_limit() helper.
- TASK-03-04 server/auth/dependencies.py + models.py: current_operator
Depends — reads signed-cookie session, fetches operator from PgStore,
401 on missing/invalid/inactive (clears session), 503 if no Postgres.
Never trusts the client (D-057).
- TASK-03-05 server/auth/routes.py: APIRouter(prefix=/api/operator)
with POST /login (rate-limited, rehash-on-login), POST /logout
(auth-gated, clears session), GET /me (auth-gated, React guard).
- TASK-03-06 tests/test_auth.py: 18 unit tests (mocked PgStore) —
passwords, cookie config, rate limit, 401/503 cases, login/logout/me,
rehash-on-login.
- pyproject.toml: added itsdangerous>=2.1 (SessionMiddleware dep).
---ci---
project: praxis
phase: 1
milestone: v0.4
status: execute
persona: security-engineer
task: 03-01,03-02,03-03,03-04,03-05,03-06
requirements:
covered: [REQ-AUTH-01, REQ-NFR-AUTH-01]
grill:
- G-031 (R-AUTH-01 reframe: k-anon primary, secure flag secondary)
---/ci---
118 lines
3.7 KiB
Python
118 lines
3.7 KiB
Python
"""Auth route handlers — login, logout, me (TASK-03-05, D-041, D-056, D-057).
|
|
|
|
APIRouter(prefix="/api/operator") with:
|
|
POST /login — rate-limited 5/min (TASK-03-03), NOT auth-gated.
|
|
POST /logout — auth-gated (Depends(current_operator)).
|
|
GET /me — auth-gated (React route guard — D-057).
|
|
|
|
Stateless cookies (D-056): logout clears the server-side session; the
|
|
client also clears its cookie. No sessions table.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Request, status
|
|
from pydantic import BaseModel
|
|
|
|
from server.auth.dependencies import current_operator
|
|
from server.auth.models import Operator
|
|
from server.auth.passwords import hash_password, needs_rehash, verify_password
|
|
from server.auth.rate_limit import rate_limit_login
|
|
|
|
router = APIRouter(prefix="/api/operator", tags=["operator-auth"])
|
|
|
|
|
|
class LoginBody(BaseModel):
|
|
username: str
|
|
password: str
|
|
|
|
|
|
class OperatorOut(BaseModel):
|
|
id: str
|
|
username: str
|
|
display_name: str | None
|
|
role: str = "operator"
|
|
|
|
|
|
class LoginResponse(BaseModel):
|
|
operator: OperatorOut
|
|
|
|
|
|
class MeResponse(BaseModel):
|
|
operator: OperatorOut
|
|
|
|
|
|
class OkResponse(BaseModel):
|
|
ok: bool = True
|
|
|
|
|
|
def _operator_out(op: Operator) -> OperatorOut:
|
|
return OperatorOut(
|
|
id=op.id,
|
|
username=op.username,
|
|
display_name=op.display_name,
|
|
role=op.role,
|
|
)
|
|
|
|
|
|
@router.post("/login", response_model=LoginResponse)
|
|
@rate_limit_login()
|
|
async def login(body: LoginBody, request: Request) -> LoginResponse:
|
|
"""Rate-limited login (5/min per IP — D-041).
|
|
|
|
On success: sets `request.session["operator_id"]` (signed cookie via
|
|
SessionMiddleware) + updates last_login_at. On needs_rehash → rehash +
|
|
update the store. On failure → 401 (no cookie set).
|
|
"""
|
|
pg_store = getattr(request.app.state, "pg_store", None)
|
|
if pg_store is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
detail="operator tier unavailable (no Postgres)",
|
|
)
|
|
row = await pg_store.get_operator_by_username(body.username)
|
|
if row is None or not row.get("is_active"):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="invalid credentials",
|
|
)
|
|
if not verify_password(row["password_hash"], body.password):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="invalid credentials",
|
|
)
|
|
op_id = str(row["id"])
|
|
request.session["operator_id"] = op_id
|
|
await pg_store.update_last_login(op_id)
|
|
if needs_rehash(row["password_hash"]):
|
|
new_hash = hash_password(body.password)
|
|
async with pg_store.pool.acquire() as conn:
|
|
await conn.execute(
|
|
"UPDATE operators SET password_hash = $1 WHERE id = $2",
|
|
new_hash, op_id,
|
|
)
|
|
return LoginResponse(
|
|
operator=OperatorOut(
|
|
id=op_id,
|
|
username=row["username"],
|
|
display_name=row.get("display_name"),
|
|
role=row.get("role", "operator"),
|
|
)
|
|
)
|
|
|
|
|
|
@router.post("/logout", response_model=OkResponse)
|
|
async def logout(request: Request, op: Operator = Depends(current_operator)) -> OkResponse:
|
|
# Stateless (D-056): clearing the server session invalidates the signed
|
|
# cookie's payload; the client also clears its cookie.
|
|
request.session.clear()
|
|
return OkResponse(ok=True)
|
|
|
|
|
|
@router.get("/me", response_model=MeResponse)
|
|
async def me(op: Operator = Depends(current_operator)) -> MeResponse:
|
|
"""React route guard endpoint (D-057). 200 → render; 401 → redirect."""
|
|
return MeResponse(operator=_operator_out(op))
|
|
|
|
|
|
__all__ = ["router"] |