Authentication Challenges
Authentication mistakes are public and expensive.
Introduction
Authentication mistakes are public and expensive. These challenges make you implement real Boot 3 / Security 6 flows for a TechLearningPRO-style identity service: signup, login, email verification, password reset, refresh-token rotation with reuse detection, TOTP 2FA, Google OIDC, and device session revoke.
Use placeholders for client ids and mail credentials — never commit secrets. Prefer jakarta.* validation annotations.
Build it, break it with replay tests, then harden it.
Purpose of this lesson
Implement end-to-end auth flows with JWT/session options, rotation, 2FA, OIDC, and session management on Boot 3.
Understanding the topic
Five builds — each has abuse tests.
- Core auth — signup, login, verify email, reset password.
- Refresh rotation — reuse detection revokes the family.
- 2FA TOTP — enroll, challenge, backup codes.
- Google OIDC — authorization code flow into local user link.
- Device sessions — list and revoke individual sessions.
Step-by-step explanation
- Model
users,refresh_tokens,totp_secrets,sessionstables via Flyway. - Implement password hash with BCrypt; issue access + refresh tokens.
- Add rotation: storing token hash only; on reuse, revoke family.
- Add TOTP enroll/verify and OIDC login; expose session list/revoke APIs.
Informative example
Refresh rotation with reuse detection (sketch).
@Servicepublic class RefreshService {public TokenPair rotate(String presentedRaw) {String hash = sha256(presentedRaw);RefreshToken current = repo.findByHash(hash).orElseThrow(() -> new InvalidRefreshException());if (current.isRevoked()) {repo.revokeFamily(current.familyId()); // reuse detectedthrow new ReuseDetectedException();}current.revoke();RefreshToken next = RefreshToken.newInFamily(current.familyId());repo.save(current);repo.save(next);return tokens.issue(current.userId(), next.rawOnce());}}// Abuse test@Testvoid reusedRefresh_revokesFamily() {TokenPair first = login();TokenPair second = refresh(first.refresh());assertThrows(ReuseDetectedException.class, () -> refresh(first.refresh()));}
Real-world use
A leaked refresh token once let an attacker mint new access tokens for a TechLearningPRO admin after password change. Rotation with family revoke stopped the session within one reuse attempt — the exact failure mode this challenge encodes in tests.
Security implications
Auth code is high-risk surface area.
- Never commit OAuth client secrets — use env placeholders.
- Disable dangerous account enumeration where policy requires.
- Step-up MFA for password change and email change.
Best practices
- Short-lived access tokens; rotatable refresh tokens.
- Hash tokens at rest; encrypt TOTP secrets with KMS.
- Rate-limit login and reset endpoints.
Common mistakes
- Storing raw refresh tokens in the database.
- Silent reuse (issuing new tokens without revoking the family).
- Logging TOTP secrets or backup codes.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1AdvancedQuestionWhat is refresh-token reuse detection?+
Answer
2IntermediateQuestionWhy hash refresh tokens at rest?+
Answer
3BeginnerQuestionWhen is session listing valuable?+
Answer
Hands-on exercise
Auth challenge pack.
1) Signup/login/verify/reset with email token (mailer stubbed).
2) Refresh rotation + reuse test that asserts family revoke.
3) TOTP enroll + login challenge.
4) 'Sign in with Google' via OIDC (mock provider in tests).
5) GET /api/sessions + DELETE /api/sessions/{id}.
Definition of done: SecurityFilterChain tests green; no secrets in repo; README with threat notes.
Summary
Auth challenges harden identity for Orders-adjacent apps. Next: architecture whiteboard challenges.