BBackend CourseLeroy Labs
0/0
Search the course

One-time tokens for email verification, password reset & invitations

Generating unguessable tokens with SecureRandom, hashing them at rest, single use and short expiry, anti-enumeration responses, and invalidating sessions after a reset.

● intermediateHostel-Web-AppSperoTek (in dev)Report-Manager-2

The concept

A password-reset link is a temporary password. Treat it with the same care.

// 1. generate: 32 bytes of CSPRNG randomness = 256 bits
byte[] bytes = new byte[32];
SECURE_RANDOM.nextBytes(bytes);
String raw = HexFormat.of().formatHex(bytes);           // or base64url

// 2. store only the hash, with purpose and expiry
tokens.save(new OneTimeToken(user, sha256(raw), Purpose.PASSWORD_RESET,
                             Instant.now().plus(Duration.ofMinutes(15))));

// 3. email the raw token in a link
mail.send(user, "https://app/reset?token=" + raw);

// 4. on use: hash the input, look it up, check purpose + expiry + unused, then mark it used

Rules

Rule Why
SecureRandom, at least 128 bits Random and UUID.randomUUID()-style ids may be predictable or too short
Store a hash A DB leak must not let attackers reset accounts
Single use Links leak via browser history, referrer headers and email forwarding
Short expiry (15–60 min for reset, 24–72 h for verify or invite) Limits the window of exposure
Purpose-bound A verify-email token must not work as a reset token
Same response for unknown emails Prevents account enumeration
Revoke sessions after a reset If the attacker had a session, the reset kicks them out
Rate-limit requests Stops inbox flooding and abuse

In your projects

Hostel-Web-AppAuthService · email/password reset

This is a model implementation. It uses 32 bytes from SecureRandom, hex-encoded and stored as a SHA-256 hash, single use, with 15-minute reset expiry. Unknown emails get the same success message. The rate limiter allows 5 requests per window on /api/auth/password-reset. There are separate Thymeleaf templates for student verification, password reset and staff and manager activation.

SperoTek (in dev)identity/domain/OneTimeToken · PasswordHistory · invitation flow

Generic OneTimeToken with purposes for verify-email, password reset and invitations (CompanyInvitationController). After a reset, password history is checked (the last 5 are blocked) and a welcome / roles-changed style email is sent.

Report-Manager-2PasswordResetService.java:37

An in-memory rate limit of “1 request per minute per email”, throwing RateLimitExceededException.

Alternatives

  • Short numeric codes (6 digits, typed by the user). Mobile-friendly, but they need tight rate limiting and attempt counters.
  • Signed stateless tokens (JWT with exp and purpose). No DB lookup, but single use is hard without storing something. A common trick is to include a hash of the current password, so the token dies once the password changes.
  • Magic-link login. The same mechanism used as passwordless login.

Check yourself

1. Unknown email on "forgot password". Best response?
2. Why hash reset tokens in the DB?