浅念
|
39cae26106
|
feat(api): password login, sliding renewal, revocable sessions
- POST /v1/auth/password|refresh|logout per contract operationIds;
long-lived single token (32B hex, DB stores sha256 only), sliding
renewal without rotation, logout sets sessions.revokedAt.
- SessionGuard: Bearer -> session -> user eligibility (disabled/locked/
pending revoke access); all failures map to 401 AUTH_TOKEN_EXPIRED.
- Anti-enumeration: unknown account / wrong password / status-blocked
share one 401 AUTH_INVALID_CREDENTIALS; unknown-account path runs a
dummy argon2 verify to equalize timing.
- Login rate limiting (P1): in-memory account+IP sliding window,
5 failures / 15 min -> 429; success clears. Contract amendment
(registered): new ErrorCodes.AUTH_RATE_LIMITED added to errors.ts +
openapi Error enum + 429 response on /auth/password; generated types
regenerated and committed. P1a single-instance limiter; P5 needs a
Redis-backed implementation.
- Global ValidationPipe (APP_PIPE) enforces class-validator DTOs ->
400 VALIDATION_FAILED via the existing exception filter.
Co-Authored-By: Claude Code <noreply@anthropic.com>
|
2026-09-17 21:17:16 +08:00 |
|
浅念
|
368b1d5b47
|
feat(api): argon2 password, idempotent admin seed, api-exception infra
Task 12 + Task 7 quality-review carryovers:
Part A - password hashing & controlled initial admin:
- src/auth/password.ts: hashPassword/verifyPassword over @node-rs/argon2
(napi prebuilt, argon2id default); empty plain rejected at the door
- src/scripts/seed-admin.ts: idempotent admin seed (ADMIN_ACCOUNT/
ADMIN_PASSWORD env, walk-up root .env load); existing account -> skip,
never overwrites the password; no audit_logs (system op, no actor)
- pnpm seed script (build + node dist/scripts/seed-admin.js);
.env.example dev defaults admin/admin-secret-change-me
- tests: test/password.spec.ts (5), test/seed.spec.ts real-DB idempotency (4)
Part B - Task 7 carryovers (all 8):
- ApiException(apiCode, message, httpStatus, details?) recognized first by
the filter; unmapped-status rule: >=500 -> INTERNAL_ERROR + generic
message, <500 -> VALIDATION_FAILED keeping original status (warn-logged)
- 5xx/INTERNAL_ERROR responses forced to generic message (leak fix)
- pino redact adds req.headers.cookie / res.headers.set-cookie
- x-request-id echo capped at 128 chars (fresh UUID beyond, no log)
- LOG_LEVEL validated against pino levels at module wiring -> fail fast
- shared/ constants (API_PREFIX), request-with-id, log-level (+spec)
- jest ts-jest reuses tsconfig.json; unit testMatch includes test/*.spec.ts
- build:watch script (no concurrently dep; dev = build:watch + node --watch)
api: 35 unit + 10 e2e tests green; root lint/typecheck/test green.
Co-Authored-By: Claude Code <noreply@anthropic.com>
|
2026-09-17 20:54:30 +08:00 |
|