CMD Guide
HomeSystem Design

Authentication

Step 29 in the System Design path · 9 concepts · 0 problems

0 / 9 complete

📘 Learn Authentication from zero

Authentication is the process of verifying that someone is who they claim to be. Think of an airport: at check-in you say "I'm Alex." That claim alone proves nothing. The agent asks for your passport — a credential issued by a trusted authority that is hard to forge. Matching your face to the passport photo is authentication. (Whether you may board this particular flight is authorization — a different gate, a different check.)

From first principles, authentication needs three things: a claim of identity ("I am user 42"), a credential that only the real user could produce (a password, a private key, a one-time code from your phone), and a verifier the system trusts to confirm the credential.

Worked example — password login. A naive system stores passwords in plaintext; one database leak exposes everyone. The correct design never stores the password itself. At signup, the server generates a random salt and computes hash = bcrypt(password, salt), storing only the resulting record (bcrypt embeds the salt and work factor in its output string, so you persist that one string). At login it recomputes the hash from the entered password and the stored salt, then compares the two with a constant-time check. A match means the user knew the password — without the server ever keeping it. The salt makes two users with the same password get different hashes, defeating precomputed "rainbow table" attacks; the slow, memory-hard hash makes mass offline brute-forcing infeasible even after a leak.

Once verified, re-checking the password on every request is wasteful, so the server issues a session token (or a signed JWT) the client presents thereafter — proof that authentication already happened.

Key insight: authentication verifies a hard-to-forge credential; it never stores or trusts the raw secret — prove identity, then store proof, not the password.

✨ Added by the guide to build intuition — not from the source course.

Lessons in this topic

🎯 Guided practice

  1. Easy — Tell authentication from authorization. A user logs in successfully, then tries to delete another user's post and gets blocked. Which step blocked them?

    Reasoning: (1) Login succeeded → the authentication step passed; the system confirmed who they are. (2) The block happened when deciding what they may do to a resource they don't own. (3) That "what are you allowed to do" decision is authorization. Pattern: authn establishes identity once; authz uses that identity on every protected action. They are always two distinct checks — name them separately in an interview.

  2. Medium — Choose a token strategy for "Sign in with Google" across 10 microservices. Users authenticate via Google; your services must trust the result without each one re-asking Google.

    Reasoning: (1) Delegated identity from a third party is the textbook case for OpenID Connect (the authentication layer on top of OAuth 2.0) — Google is the identity provider and returns a signed ID token (JWT) proving who the user is. (2) Your edge/gateway validates that ID token and typically mints your own short-lived access token (also a signed JWT) for internal calls; with 10 stateless services, a central session lookup on every call is a bottleneck, so each service verifies the token's signature locally using the issuer's published public key (JWKS) — an O(1) check, no shared DB. (3) The cost of stateless: you can't revoke a JWT before it expires. Mitigate with short-lived access tokens (minutes) plus a refresh token that mints new ones, so a stolen token dies quickly; for hard revocation, keep a small deny-list of token IDs. (4) Never put secrets in the payload — a JWT is signed (tamper-evident) but not encrypted, so its claims are readable by anyone holding it. Pattern: third-party identity → OIDC; scale + statelessness → short-lived signed JWTs verified via JWKS, plus refresh tokens to cover the revocation gap.

✨ Added by the guide — work these before the full problem set.

🧠 Review & recall

Active recall is what moves a topic into long-term memory. Flip each card before revealing, then test yourself — your results are saved on this device.

Flashcard
What does authentication verify, and how does it differ from authorization?
tap to reveal →
Authentication verifies that someone is who they claim to be (who you are), like matching a face to a passport photo. Authorization is a separate check deciding what an already-identified user is allowed to do, like whether they may board a particular flight.
💡 AuthN = who you are; AuthZ = what you may do — different gates.
Flashcard
What are the three things authentication needs from first principles?
tap to reveal →
A claim of identity ('I am user 42'), a credential only the real user could produce (password, private key, one-time code), and a verifier the system trusts to confirm the credential.
💡 Claim, Credential, Verifier — the three C's (well, two C's and a V).
Flashcard
What are the three classic factor categories authentication relies on?
tap to reveal →
Something you know (password or PIN), something you have (a phone or security token), and something you are (a fingerprint or other biometric).
💡 Know / Have / Are.
Flashcard
How should a correct password-login system store and check passwords?
tap to reveal →
Never store the raw password. At signup generate a random salt and store bcrypt(password, salt). At login recompute the hash from the entered password plus stored salt and compare with a constant-time check. A match proves the user knew the password without the server keeping it.
💡 Store proof, not the password — salt + slow hash + constant-time compare.
Flashcard
Why does salting plus a slow, memory-hard hash like bcrypt matter after a database leak?
tap to reveal →
The salt makes two users with the same password get different hashes, defeating precomputed rainbow-table attacks. The slow, memory-hard hash makes mass offline brute-forcing infeasible even after the data leaks.
💡 Salt kills rainbow tables; slowness kills brute force.
Flashcard
After a password is verified, why issue a session token or JWT instead of re-checking the password every request?
tap to reveal →
Re-checking the password on every request is wasteful. The server issues a session token (or signed JWT) that the client presents thereafter as proof that authentication already happened.
💡 Prove once, then carry a token as proof.
Flashcard
For 'Sign in with Google' across 10 stateless microservices, what token strategy fits and what is its main drawback?
tap to reveal →
Use OpenID Connect (auth layer on OAuth 2.0): Google returns a signed ID token (JWT), and each service verifies the signature locally via the issuer's published public key (JWKS) — an O(1) check, no shared DB. Drawback: a JWT can't be revoked before it expires; mitigate with short-lived access tokens plus refresh tokens, and a small deny-list for hard revocation.
💡 Third-party identity -> OIDC; scale -> short-lived JWTs via JWKS + refresh tokens.
Q1. A user logs in successfully, then tries to delete another user's post and is blocked. Which step blocked them?
Q2. Why does a correct password system store bcrypt(password, salt) rather than the plaintext password?
Q3. What is the purpose of the random salt in password hashing?
Q4. In the 'Sign in with Google' across 10 microservices design, why have each service verify the JWT signature locally using JWKS instead of a central session lookup?
Q5. Which statement about a JWT's contents is correct per the lesson?