Redactare: - handler RequestValidationError dropeaza input/ctx din 422 (vectorul de scurgere a rar_credentials.password pe /v1/prezentari); pastreaza type/loc/msg - app/security.py: scrub/scrub_text + CredentialRedactingFilter pe root+uvicorn - models.py: password cu repr=False Auth API-key: - app/auth.py: hash SHA-256 in api_keys (cheia in clar emisa o singura data), header X-API-Key / Authorization: Bearer, dependency resolve_account_id - enforcement pe flag AUTOPASS_require_api_key (prod on->401, dev off->cont default id=1; cheie prezenta invalida->401 mereu) - account_id real curge din cheie in ingestie + mapare - tools/apikey.py: CLI create/rotate/revoke/list (fara endpoint HTTP admin) 16 teste noi (tests/test_security.py). 85 pass total. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
98 lines
3.4 KiB
Python
98 lines
3.4 KiB
Python
"""Modele Pydantic pentru suprafata API.
|
|
|
|
ATENTIE: validarea completa (regex VIN ^[A-HJ-NPR-Z0-9]{17}$, nrInmatriculare,
|
|
dataPrestatie ∈ [2024-12-01, azi] TZ Bucuresti, R-ODO/I-ODO -> odometruInitial
|
|
obligatoriu, odometruInitial <= odometruFinal, normalizare strip/upper) este
|
|
**T3** — aici sunt doar formele de baza. Vezi plan.md sect. 2 + roadmap T3.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pydantic import BaseModel, Field, field_validator, model_validator
|
|
|
|
|
|
class RarCredentials(BaseModel):
|
|
"""Credentiale RAR per-cerere (vin de la ROAAUTO din Oracle). NU se stocheaza."""
|
|
|
|
email: str
|
|
# repr=False: str(creds) / loguri care fac repr pe model NU expun parola.
|
|
password: str = Field(..., repr=False)
|
|
|
|
|
|
class PrestatieItem(BaseModel):
|
|
"""O operatie de declarat. Contract hibrid (decis 2026-06-15):
|
|
|
|
ROAAUTO poate trimite FIE `cod_prestatie` (cod RAR direct, ex. OE-1), FIE
|
|
`cod_op_service` (cod intern ROAAUTO) + `denumire` — pe care gateway-ul le
|
|
mapeaza in cod RAR prin operations_mapping. Cel putin unul dintre
|
|
cod_prestatie / cod_op_service e obligatoriu (shape -> 422 daca lipsesc ambele).
|
|
"""
|
|
|
|
cod_prestatie: str | None = Field(None, description="cod din nomenclator RAR, ex. OE-1")
|
|
cod_op_service: str | None = Field(None, description="cod intern operatie ROAAUTO (mapat -> cod RAR)")
|
|
denumire: str | None = Field(None, description="denumirea operatiei ROAAUTO (pentru fuzzy lookup la mapare)")
|
|
|
|
@field_validator("cod_prestatie")
|
|
@classmethod
|
|
def _norm_cod(cls, v: str | None) -> str | None:
|
|
return v.strip().upper() if v else None
|
|
|
|
@field_validator("cod_op_service", "denumire")
|
|
@classmethod
|
|
def _norm_op(cls, v: str | None) -> str | None:
|
|
return v.strip() if v else None
|
|
|
|
@model_validator(mode="after")
|
|
def _require_one(self) -> "PrestatieItem":
|
|
if not self.cod_prestatie and not self.cod_op_service:
|
|
raise ValueError("fiecare prestatie are nevoie de cod_prestatie sau cod_op_service")
|
|
return self
|
|
|
|
|
|
class PrezentareIn(BaseModel):
|
|
"""O prezentare de declarat la RAR.
|
|
|
|
Pydantic doar NORMALIZEAZA aici (strip/upper pe vin/nrInm). Validarea de
|
|
continut (regex VIN, interval data, R-ODO/I-ODO, odometru) e in
|
|
app.validation.validate_prezentare si NU resping cererea — marcheaza
|
|
`needs_data` (plan.md sect. 3).
|
|
"""
|
|
|
|
vin: str
|
|
nr_inmatriculare: str
|
|
data_prestatie: str # YYYY-MM-DD
|
|
odometru_final: str # string per contract
|
|
odometru_initial: str | None = None
|
|
prestatii: list[PrestatieItem]
|
|
sistem_reparat: str = "null"
|
|
obs: str | None = None
|
|
b64_image: str | None = None
|
|
|
|
@field_validator("vin", "nr_inmatriculare")
|
|
@classmethod
|
|
def _norm_upper(cls, v: str) -> str:
|
|
return v.strip().upper()
|
|
|
|
@field_validator("data_prestatie", "odometru_final")
|
|
@classmethod
|
|
def _norm_strip(cls, v: str) -> str:
|
|
return v.strip()
|
|
|
|
|
|
class PrezentareRequest(BaseModel):
|
|
"""Body pentru POST /v1/prezentari — una sau mai multe prezentari + creds RAR."""
|
|
|
|
rar_credentials: RarCredentials
|
|
prezentari: list[PrezentareIn] = Field(..., min_length=1)
|
|
|
|
|
|
class SubmissionResult(BaseModel):
|
|
submission_id: int
|
|
status: str
|
|
id_prezentare: int | None = None
|
|
deduped: bool = False # True daca idempotency a intors un submission existent
|
|
|
|
|
|
class PrezentariResponse(BaseModel):
|
|
results: list[SubmissionResult]
|