Bỏ qua, tới nội dung chính

Kiến trúc · ADR 0024

Password reset by email

Trạng thái
accepted
Ngày
2026-09-27
Người quyết định
Alex

Context

ADR-0021 shipped self-registration and explicitly rejected a password reset flow: "a reset is an email, and there is no email", so a forgotten password was to be an admin ticket. That admin route was never built, and the owner now wants the ordinary thing — "forgot password → send a reset link to the registered email." The blocker in ADR-0021 was cost: the project will not buy a mail provider. That is a transport problem, not a design one, and it can be solved for free in development.

Decision

  • Revisit ADR-0021's "no reset" stance. Self-service reset is back, by emailed link. Everything else ADR-0021 decided stands.
  • Two public endpoints in the auth module. POST /auth/forgot-password takes an email; POST /auth/reset-password takes a token and a new password. Both are @Public().
  • No user enumeration. forgot-password always answers 200 with the same empty-data envelope, whether or not the address has an account, and whether or not the email actually sent (a mail failure is logged, never surfaced). The web form shows one neutral message either way. It is throttled to five requests per hour per client (FORGOT_THROTTLE) — the abuse here is using the endpoint as a mailer, not guessing.
  • Tokens are hashed, single-use and short-lived. A request mints 32 random bytes; only the SHA-256 of that token is stored (PasswordResetToken.tokenHash), the same way refresh tokens are stored — a database dump is not a set of live reset links. The raw token rides in the link ${WEB_URL}/reset-password?token=… and nowhere else. It expires in 30 minutes (PASSWORD_RESET_TTL_MINUTES) and usedAt makes it single-use.
  • The reset is claimed atomically. reset-password flips usedAt with one conditional updateMany that matches only an unused, unexpired row, so two requests racing on the same link cannot both succeed — the same pattern refresh-token rotation uses. A missing, expired or already used token is one error, RESET_TOKEN_INVALID: distinguishing them would reveal whether a token ever existed.
  • Reset revokes every session. On success the password is rehashed at bcrypt cost 12 (one number for every password this system writes, ADR-0021), every _other_ outstanding reset token for the account is spent, and revokeAllSessions drops every refresh token — as a password _change_ already does. Whoever knew the old password is signed out everywhere. The reset does not sign the user in; they sign in fresh with the new password.
  • Google-only accounts get nothing. An account with no local passwordHash has no password to reset, so no token is minted — indistinguishable from the no-account path.
  • A dev-first mail transport. MailService in infra/mail picks its transport at boot: a real server when SMTP_URL is set; an in-memory JSON transport under NODE_ENV=test (hermetic, no network); otherwise a throwaway Ethereal test account, whose preview URL is logged so a developer can read the mail in a browser with no paid provider. The reset link is also logged directly, so the flow is completable from the logs alone. SMTP_URL (secret), MAIL_FROM and WEB_URL are new, optional/defaulted env vars.
  • New table PasswordResetToken, added to the design (schema.target.prisma) first, then the live schema, with RLS enabled in its migration (ADR-0009). It carries userId, a unique tokenHash, expiresAt, usedAt and createdAt, indexed on userId and expiresAt. It is not part of the M1–M6 group; it lands with the auth work.
  • RESET_TOKEN_INVALID joins ERROR_CODES, and two audit actions — user.password_reset_requested and user.password_reset — join AUDIT_ACTIONS.

Alternatives considered

  • Keep ADR-0021's admin-ticket stance. Rejected: the admin route was never built, and a self-service reset is the ordinary expectation. The only real objection was mail cost, and the Ethereal/JSON transport removes it for development.
  • Store the raw token, or a JWT with no server row. Rejected: a stored raw token turns a database dump into live reset links, and a stateless JWT cannot be made single-use or revoked before it expires.
  • A pending "verify then reset" state, or telling the user the email is unknown. Rejected: both leak which addresses have accounts, the same enumeration ADR-0021 was careful to avoid on the sign-up form.
  • Signing the user in on a successful reset. Rejected: the reset revokes every session by design; minting a new one in the same breath undoes half of that and hides the "you were signed out everywhere" behaviour the user should see.

Consequences

  • A forgotten password is no longer an admin ticket; the admin module owes one less route.
  • The project can develop and demo the whole flow with no paid provider. Production needs SMTP_URL pointed at a real server; nothing else changes.
  • One more table and one more secret (SMTP_URL) to carry. The table follows every rule the others do (timestamptz, RLS, hashed secret), so it adds no new pattern.
  • A reset, like a password change, signs the user out everywhere including the tab they reset from. The web client already handles that for password change; the reset page states it plainly.
  • ADR-0021 is not superseded — it still governs registration, the password policy and refresh-token reuse detection. Only its "no password reset flow" bullet is reversed, and this ADR records why.