Learn System Design Authentication

Security

Authentication concepts, one picture at a time

Sessions, JWTs, OAuth, SSO and OIDC get mixed up because they show up in the same login screen. They solve different problems. Each section below draws one of them out and lets you poke at it.

8 concepts 9 animated diagrams ~12 min

1 · Authentication vs authorization

Two doors, two different questions

Authentication asks "who are you?". Authorization asks "what are you allowed to touch?". Think of an office: the front desk checks your ID, then your keycard decides which rooms open. Being inside the building doesn't open every door.

Who is asking?

What do they want?

Request
🪪 AuthN Who are you?
🚪 AuthZ Allowed?
🖼️ Photos The resource
…

401 Unauthorized

Badly named, but it means "unauthenticated": the server doesn't know who you are. Log in and try again.

403 Forbidden

The server knows exactly who you are, and the answer is still no. Logging in again won't help.

2 · Sessions and cookies

How a forgetful server remembers you

HTTP treats every request as brand new. To keep you logged in, the server gives you a ticket after login and keeps the matching record on its side. Press play and follow the envelope.

🧑‍💻 Browser
🖥️ Server
🗄️ Session store

Cookie = the ticket

Lives in your browser. Holds only an ID like sid=8f3a91c2. Useless on its own, which is the point.

Session = the coat

Lives on the server. Holds who you are, when you logged in, your cart. Delete it and the ticket stops working.

Mark the cookie HttpOnly so page scripts can't read it, Secure so it only travels over HTTPS, and SameSite so other sites can't send it for you.

3 · Sessions vs JWT

A JWT is a signed note, not a locked box

A session ID means "go look me up". A JSON Web Token carries the facts itself, plus a signature, so any server holding the key can check it without a lookup. It has three parts separated by dots.

The token, as it travels

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyXzQyIiwibmFtZSI6IkFuYSIsInJvbGUiOiJtZW1iZXIiLCJleHAiOjE3NjcyMjU2MDB9.k9Qx3vT7bWm1LzR0pYc8sN2eHfA5uJdG4oVnE6iKqXw

Header

{
  "alg": "HS256",
  "typ": "JWT"
}

Which algorithm signed it.

Payload (claims)

{
  "sub": "user_42",
  "name": "Ana",
  "role": "member",
  "exp": 1767225600
}

Readable by anyone. Encoded, not encrypted.

Signature

HMACSHA256(header + "." + payload, secret)

The catch: you can't easily take a JWT back

Both apps below send a request every second for Ana. Ban her and watch which one notices. (15 seconds here stands in for 15 minutes.)

🗄️ Session app

checks the store each time

    🧾 JWT app

    expires in 15s after ban

      Press the button to start the race.

      Pick sessions when…

      You have one app and a database anyway, and you need "log out everywhere" to work right now. A shared store (like Redis) lets many servers read the same sessions.

      Pick JWTs when…

      Several services need to verify the same user without calling home. Keep expiry short, and never put secrets in the payload.

      4 · Bearer, access and refresh tokens

      A day pass and the card that renews it

      A bearer token works like a festival wristband: security checks the band, not your face. So if someone steals it, it works for them too. The fix is two tokens with very different lifetimes.

      🎫 Access token: sent to the API on every call 15 min

      Short and narrow. If it leaks, the damage window is minutes.

      🔁 Refresh token: only shown to the auth server 30 days

      Long-lived, so guard it harder: HttpOnly cookie or the OS keychain, never localStorage.

      📱 App
      🔐 Auth server
      🖥️ API
      🕵️ Attacker

      5 · Single sign-on

      Log in once, walk into every app

      With SSO, apps stop checking passwords themselves. They send you to one trusted identity provider (Google Workspace, Okta, Microsoft Entra) and accept what it vouches for. Open the apps below in any order.

      Identity provider No session yet

      Pick any app to start.

      Passwords typed: 0

      SSO is the experience, not a token format. Under the hood it runs on OpenID Connect (ID tokens) or SAML (XML assertions). The bonus: one place to turn on MFA, and one switch to cut off someone who leaves the company.

      6 · OAuth 2.0

      A valet key, not your house key

      OAuth lets one app act for you on another app, with limits you approve, and without ever seeing your password. A valet key starts the car but won't open the trunk. OAuth is about authorization, not logging in.

      🙋 You
      🖨️ PrintShop
      🔐 Photo site login
      🖼️ Photo API

      login.photos.example.com/consent

      🖨️ PrintShop wants to:

      Signed in as [email protected] on the photo site

      Untick what you don't want to share. The token changes live.

      Access token PrintShop receives

      What PrintShop tries next

      • List photos to print
      • Delete a photo
      • Read your contacts
      • Change your password

      "Change your password" is never on offer. Your password stayed on the photo site.

      7 · OpenID Connect

      OAuth plus a name tag

      Plain OAuth tells an app "you may call this API". It never says who the user is. OpenID Connect adds that missing identity layer on top: ask for the openid scope and you also get an ID token. This is what "Log in with Google" really runs on.

      OpenID Connect

      + ID token · + "openid" scope · + /userinfo

      OAuth 2.0

      access tokens · scopes · refresh tokens

      HTTPS

      ID token

      For your app. Answers "who logged in?"

      {
        "iss": "https://login.example.com",
        "sub": "user_42",
        "aud": "printshop-app",
        "name": "Ana Silva",
        "email": "[email protected]",
        "iat": 1767222000,
        "exp": 1767225600
      }

      aud is your app's client ID. Check signature, issuer, audience and expiry before trusting it.

      Access token

      For the API. Answers "what may this caller do?"

      {
        "iss": "https://login.example.com",
        "sub": "user_42",
        "aud": "https://api.photos.example.com",
        "scope": "photos.read",
        "exp": 1767223800
      }

      It might not even be a JWT. Your app should treat it as an opaque string and just pass it along.

      ✗ Don't log users in with an access token

      It was written for an API, not for you. A token for someone else's app could be replayed at yours.

      ✗ Don't send the ID token to your API

      It proves identity to the app that asked for it. Calling APIs is the access token's job.

      8 · How it fits together

      8 tools, one job each

      None of these replaces another. They're layers of the same problem, and most real systems use several at once.

      Authentication

      Proves who you are

      Authorization

      Decides what you may do

      Session + cookie

      Remembers you between requests

      JWT

      Carries signed claims anyone can verify

      Access + refresh tokens

      Grant access over time, safely

      SSO

      Sign in once, use many apps

      OAuth 2.0

      Lets an app act for you, with limits

      OpenID Connect

      Tells an app who you are, via an ID token

      What are you building?

      Don't pick JWT because it's popular, or skip sessions because they feel old. Start from the situation.

      Reach for

      Test yourself

      Who are you, and what may you do?

      Answer from the diagrams above. Nothing is saved or sent anywhere.

      Question 1 of 6 Score: 0
      Ana is logged in but gets "403 Forbidden" when opening Ben's private album. Which check failed? basic

      Keep going

      Tokens get checked at the API gateway. See where that fits in the full API lifecycle.

      This page is built from the ideas in this video on 7 authentication concepts. Watch it for the spoken walkthrough. For the full spec, see how OpenID Connect works.