Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Security Fundamentals

    Authentication and Authorization

    Authentication (AuthN) — proving who you are. (HTTP status 401 when missing — 05networking3web.md Ch 5.)

    Chapter 5 of 134 min read

    The Plain Picture#

    At an airport: showing your passport proves who you are. Your boarding pass says where you're allowed to go. A valid passport doesn't let you into the cockpit.

    The Real Names#

    • Authentication (AuthN) — proving who you are. (HTTP status 401 when missing — Networking 3: DNS, HTTP and HTTPS Ch 5.)

    • Authorization (AuthZ) — deciding what you're allowed to do. (Status 403 when denied.)

    • Authentication factors:

      • something you know — a password;

      • something you have — a phone app generating codes, a hardware security key;

      • something you are — a fingerprint, face.

    • MFA (multi-factor authentication) — requiring two or more different factors. It blocks most account takeovers even when the password leaks. Passkeys (built on the WebAuthn standard) replace passwords with a key pair stored on your device — the public key goes to the site, the private key never leaves the device, so there's nothing to phish or leak.

    • Authorization models:

      • RBAC (Role-Based Access Control) — permissions attached to roles: student, teacher, admin.

      • Ownership / ABAC (Attribute-Based) — rules like "a note may be read by its owner, or by members of a group it's shared with".

    Under the Hood — sessions vs tokens (the overview)#

    After login, the server needs to recognise you on every later request (HTTP is stateless — Networking 3: DNS, HTTP and HTTPS Ch 7). Two common approaches:

    Server-side session (cookie)Self-contained token (JWT)
    What the client holdsa random session IDa signed token containing claims (user, role, expiry)
    Server looks upthe session in a database/cachenothing — verifies the signature
    Log out / revokedelete the session — instanthard until it expires (needs a deny-list or short expiry)
    Typical useclassic web appsAPIs, mobile apps, service-to-service

    A JWT (JSON Web Token) is three Base64URL-encoded parts joined by dots — header.payload.signature. A real one, signed with HMAC-SHA256 (Ch 4):

    eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJwcml5YSIsInJvbGUiOiJzdHVkZW50IiwiZXhwIjoxNzkwNTczNDAwfQ.c5lotD0YEQeUM9YHLL_7jKjkISRKeRdXeR1G4rG3J7Q
    └─────────────── header ─────────────┘ └──────────────────────── payload ─────────────────────────┘ └──────── signature ────────┘
     {"alg":"HS256","typ":"JWT"}            {"sub":"priya","role":"student","exp":1790573400}

    The payload is only encoded, not encrypted (Ch 2) — anyone holding the token can read it. The signature only stops anyone from changing it. So: no secrets in JWTs, keep expiry short, always verify the signature and the algorithm on the server.

    The full picture — OAuth 2.0, OpenID Connect, "Sign in with Google", refresh tokens, and implementing all of it in FastAPI — is in Authentication and Authorization (coming soon).

    Gotchas

    • Authorization must be checked on the server, for every request. Hiding the "Delete" button in the UI is not authorization.

    • SMS codes are the weakest second factor (SIM-swap attacks); authenticator apps, hardware keys and passkeys are stronger.

    Check yourself

    4 questions
    1

    Authentication vs authorization — and which status code goes with each?

    Show answer

    Authentication = who you are (401 when missing/invalid); authorization = what you may do (403 when denied).

    2

    What are the three kinds of authentication factor?

    Show answer

    Something you know, have, are.

    3

    Can anyone read a JWT's payload? What stops them changing it?

    Show answer

    Yes — it's just Base64URL-encoded JSON. The signature: changing the payload invalidates it.

    4

    Why are server-side sessions easier to revoke than JWTs?

    Show answer

    Deleting the session record makes the ID useless immediately; a JWT stays valid until it expires unless you track revocations.