Highlights

Contents

BACKEND ENGINEERING / UNIT III / FIELD GUIDEAuthenti-cationSessions, passwords, JWTs and OAuth, proved and revokedheaderpayloadsignaturewho you are, proved on every requestSessionsstores, expiry, rotationPasswordshashing, attacks, resetJWTclaims, keys, refresh, attacksOAuth and OIDCdelegation and identity
Before we start

How to read this chapter

Authentication answers one question, who is making this request, and then has to keep answering it on every request without asking for a password each time. This chapter covers both halves, proving identity once and carrying that proof safely afterwards.

The parts follow a login’s life. First comes the server-side session, the oldest and simplest design. Then the password behind it, the attacks on login forms, and the reset flow that is everyone’s back door. Then tokens, JWT, refresh tokens and their failure modes, and finally OAuth and OpenID Connect, where a third party does the proving.

Each section works through one Wren example with numbers, then a figure. Read the text, then check the figure against it. Picture this gives an analogy, notes add detail, Watch out names a real mistake, and Interview lens gives an answer to say aloud. In one breath closes each part.

By the end you should be able to design Wren’s login from scratch and defend each choice: how passwords are stored, how long a session lives, what is in a token, how it is revoked, and why the OAuth callback checks state.

The running system

Meet Wren

Wren is the illustrative food-ordering service from earlier units. These are its authentication settings. All values are assumptions chosen for readable arithmetic.

Setting Value What it controls
Accounts 1,000,000 Size of the user table
Active sessions 200,000 Session store size
Session record about 200 bytes 40,000,000 bytes in Redis
Idle and absolute session limits 30 min, 12 h How long a login lasts
Password hash Argon2id, m = 19 MiB, t = 2, p = 1 Cost of each guess
Peak logins 20 per second CPU spent on hashing
Login limit 5 failures per account per minute Brute-force defence
Access token JWT, RS256, 15 min Proof sent on each API call
Refresh token opaque, 30 days, rotated Renews access tokens
Reset token 32 random bytes, 30 min, single use Account recovery
Identity provider Google, via OpenID Connect “Sign in with Google”

The user is still user 42. In this unit they log in with a password, then later with Google, and an attacker tries to get into their account every way the chapter describes.

The map

What is inside

Draw how a user proves who they are once, and how every later request carries that proof safely.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Session authentication2Logout, devices andrevocation3Password storage4Login attacks anddefences5Password reset6JSON Web Tokens7Access and refresh tokens8JWT attacks and mistakes9OAuth 2.010OpenID Connect and thefull login
  1. 01Session authenticationSections 1 to 5. We build stateful session authentication from a single login to a request that carries it.
  2. 02Logout, devices and revocationSections 6 to 7. We learn how to end sessions on purpose, one at a time or all at once.
  3. 03Password storageSections 8 to 10. We learn how to store passwords so that a stolen database does not hand over the accounts.
  4. 04Login attacks and defencesSections 11 to 13. We study how attackers get into accounts without ever touching the password database.
  5. 05Password resetSections 14 to 15. We design the account recovery flow that bypasses the password entirely.
  6. 06JSON Web TokensSections 16 to 19. We take apart a JSON Web Token, its claims, its signature and the keys behind it.
  7. 07Access and refresh tokensSections 20 to 22. We split one long-lived credential into a short access token and a long refresh token.
  8. 08JWT attacks and mistakesSections 23 to 25. We go through the ways JWT verification fails in real code.
  9. 09OAuth 2.0Sections 26 to 30. We learn how one application gets limited access to a user's data on another service without seeing the user's password.
  10. 10OpenID Connect and the full loginSections 31 to 33. We add identity on top of OAuth and assemble Wren's complete login.
  11. +Interview prepInterview question bank, graded exercises and worked solutions.