07. Security / Lesson 1 of 3

Authentication

Prove who you are, once, and keep it safe across many requests.

  • 10 min
  • Beginner
  • 10 steps
  • Factors0 (bad)
  • Access lasts—
BrowserApp serverwho is this?Database
  • Request
  • Response
  • Failed request
Step 1/10

Step 1 of 10: The server does not know who is calling

Read every step as text
  1. Step 1: The server does not know who is calling

    Authentication (authN) answers "who are you?". Authorization (authZ, the next lesson) answers "what may you do?". Here we only solve the first.

    HTTP has no memory: every request arrives as a stranger. The user logs in once, but the server must recognise them on the next 50 requests too, without asking for the password each time.

    Authentication happens once. After that, a small proof (a cookie or a token) rides along with every request.

  2. Step 2: Passwords: never store them, store a salted slow hash

    If the database stores passwords as text, one leak exposes every account. Store a hash: a one-way scramble. At login, hash what was typed and compare. Add a random salt per user, so equal passwords give different hashes and ready-made tables are useless.

    Speed matters here. Say a thief tries 10 billion guesses a second against a fast hash such as SHA-256, and 8 lowercase letters (208,827,064,576 options) fall in 21 seconds. A deliberately slow hash (bcrypt, scrypt or Argon2), at 10,000 a second, takes 242 days.

    Never use a plain fast hash for passwords, and never invent your own scheme. Use a library.

  3. Step 3: Sessions: the server remembers you

    After a correct password, the server creates a long random session id, saves session → user 42 in a store such as Redis, and sends it back as a cookie: Set-Cookie: sid=...; HttpOnly; Secure; SameSite=Lax.

    The browser sends the cookie on every request, and the server looks it up (about 1 ms). Logging out is simple: delete the entry. The cost is a lookup per request, and a store every server can reach.

    HttpOnly hides the cookie from page scripts, Secure sends it over HTTPS only.

  4. Step 4: Tokens: carry the proof inside

    A JSON Web Token has three parts: a header, a payload such as {"sub": 42, "exp": 1735000000}, and a signature made with the server's secret key. Any server with the key can check the signature, with no store.

    That makes services easy to scale, but a token cannot be taken back before it expires, so keep it short, say 15 minutes. The payload is only encoded, not encrypted: anyone can read it, so never put secrets in it.

    Always check the signature, the expiry (exp) and the algorithm. A token you do not verify proves nothing.

  5. Step 5: Refresh tokens: short access, long login

    Logging in every 15 minutes would annoy everyone. So login returns two tokens: a short access token and a long refresh token (say 30 days), kept in the database as a hash so it can be revoked.

    When the access token expires, the app sends the refresh token to /token and gets a new pair: 4 refreshes an hour for an active user. If a stolen access token is the only thing lost, it works for at most 15 minutes. Rotate refresh tokens, so a reused old one reveals theft.

    Access token: short, sent everywhere. Refresh token: long, sent only to one endpoint, and stored with care.

  6. Step 6: "Sign in with Google": log in without a new password

    With OAuth 2.0 and OpenID Connect, the app sends the user to an identity provider. The user signs in there and agrees to share their name and email. The provider sends the browser back with a one-time code.

    The app's server swaps the code for tokens, in a direct call that includes its client secret, and reads who the user is. The app never sees the user's password, and does not have to store it.

    Check the state value you sent, and use PKCE for apps that cannot keep a secret. They block forged logins.

  7. Step 7: Single sign-on: one login for many apps

    In a company, people use many apps: mail, wiki, tickets. With SSO, one identity provider signs the person in once (say for 8 hours), and every app trusts its signed answer, through SAML or OpenID Connect. Nobody keeps 20 passwords.

    Security gets easier, too: strong rules and MFA live in one place, and when someone leaves, disabling one account removes access to all 20 apps.

    The identity provider becomes a single point of failure and a prime target. Protect it, and have a backup way in.

  8. Step 8: MFA: a password alone is not enough

    Multi-factor authentication asks for two kinds of proof: something you know (password), something you have (phone or security key) or something you are (fingerprint). A stolen or phished password then fails on its own.

    An authenticator app shows a 6-digit code from a shared secret and the clock, new every 30 seconds. One guess is 1 in 1,000,000; with only 5 tries allowed, a thief has a 0.0005% chance. Passkeys and security keys go further: they cannot be tricked into answering a fake site.

    SMS codes are better than nothing, but SIM swapping can steal them. Prefer an app, a key or a passkey.

  9. Step 9: Common mistakes

    Most breaches come from small slips: plain or fast-hashed passwords, tokens that are never checked or never expire, secrets in the page's local storage where a script can read them, and no limit on login attempts.

    Also avoid error messages that say "no such user" and "wrong password" differently: they let an attacker list your accounts. Say "wrong email or password" for both, and rate-limit logins.

    Do not build your own login system if you can use a trusted library or provider. The details are many and easy to get wrong.

  10. Step 10: Authentication in one picture

    Prove who you are once, with a salted slow-hashed password, a provider (OAuth or SSO) or both, plus a second factor. Then carry a small proof: a session cookie, or a short access token with a refresh token behind it.

    Verify every token, keep lifetimes short, limit login tries, and use trusted libraries. Next, authorization decides what the proven user may do.