Skip to main content

Worked solution: account-recovery design

Attempt the student worksheet before reading this reference. The application files in this folder are a complete runnable solution. Run node server.mjs from this folder, or node checks.mjs to verify the repair. Stop other lesson servers first.

Revised design​

An account's email identifies where recovery is requested; it does not prove control. The owner receives an unpredictable temporary token through a private channel. The server accepts a password change only after verifying that the token belongs to this account, has not expired and has not already been used. Rejected proof leaves the password unchanged.

The local teaching mailbox represents that channel for demonstration. In production it must not be public. Requesting a replacement token invalidates the prior token in this lab, and successful recovery invalidates outstanding tokens for the account.

Policy repair​

import { consumeRecoveryToken, setAccountPassword } from './recovery-helpers.mjs';

export function completeRecovery(state, { email, token, password }) {
if (!consumeRecoveryToken(state, email, token)) return false;
return setAccountPassword(state, email, password);
}

The helper performs verification and consumption before the update. The surrounding server validates input before calling this policy. The starter only checked state.accounts.has(email), allowing an unauthorized requester who knew an existing email to choose a new password.

The token helper and password-storage implementation are supplied, not student-created cryptography. Only recovery-policy.mjs differs between the student and solution application files.

Expected observations​

  • Missing, wrong, expired, cross-account, superseded and reused tokens do not authorize a change.
  • An invalid attempt does not consume a valid token belonging to another account.
  • The owner's latest valid token authorizes one change; the new password then works and the old one fails.
  • The normal request acknowledgement does not disclose a token or whether the account exists.
  • All supplied checks pass. Expiry is tested with controlled time rather than a ten-minute wait.

This repair is a focused design lesson. A complete service also needs private delivery, HTTPS, abuse controls, consistent response timing, recovery notifications and session policies. This app creates no authenticated sessions. Do not deploy it or use real credentials.

Screenshot preview