Password Recovery and MFA State
Follow a reset link and a second-factor login as two clear journeys, with server checks for account, expiry, and one-time use.
You’re reading a preview. Premium includes the full lesson and specialist practice. View the full curriculum.
Mia forgets her password. A reset link should let her choose a new one once, for a limited time, for her account. Anyone who can guess or reuse that link may get the same opportunity.
Now consider a separate login: Mia knows her password, but the app also requires a code from an authenticator. The browser saying "the second factor passed" is not the same as the server checking it.
These are two journeys with related rules:
Recovery: issue a random token → check its account and expiry → change password and consume token
MFA: verify password → create pending login → verify second factor → create full session
We'll inspect each journey on its own before checking the alternate routes that can create a session. MFA means multifactor authentication: the password alone does not complete this login.
Recovery: follow a reset link from creation to use
Start at issuance, then follow delivery, lookup, validation, password update, token consumption, and session consequences. A token field alone tells you little. Check which account and action it authorizes, how long the server accepts it, and whether concurrent requests can reuse it.
# Unsafe original teaching outline.
def issue_reset(email):
token = hashlib.md5(str(int(time.time())).encode()).hexdigest()
resets.insert(email=email, token=token, created_at=now())
send_reset_link(email, token)
def finish_reset(email, token, new_password):
row = resets.find(email=email, token=token)
if not row:
return invalid_reset()
users.replace_password(email, hash_password(new_password))
resets.delete(row.id)
return reset_complete()
The email predicate and deletion are meaningful controls. They do not make the token unpredictable. A timestamp has a small search space when issuance time can be estimated; hashing it expands its representation without adding secret entropy. Requests issued within the same timestamp unit may collide. The saved created_at value does not enforce expiry unless the consuming path compares it against a server deadline.
Select the stages and ask which server evidence authorizes the next transition.
Issue. Generate independent entropy and bind its digest to an account, purpose, and deadline.
const raw = randomBytes(32);
await store({ digest: hash(raw), userId, purpose, expiresAt });
Validate. Check the stored binding and current state at consumption.
requirePurpose(token, "password-reset");
requireUnexpired(token);
requireUnused(token);
Transition. Commit the change with consumption; a caller flag cannot replace verification.
await transaction(async tx => {
await tx.changePassword(userId, verifier);
await tx.consumeToken(token.id);
});
Review question: A token or factor is useful only when its guarantees survive through the final state change.