Skip to content
engineering workbench.by StudyPort Academy
Learning path
Intermediate18 min

JWTs in depth

Understand the idea

A JWT is three parts and a signature. Where you keep it decides who can steal it.

The idea

A is three base64url parts: header, payload, and signature. The header names the algorithm. The payload is JSON the client can decode, so it must not hold a secret. The signature is what you trust. With HMAC, such as HS256, every service that verifies must hold the same secret that signed the token. Steal that secret from any one service and you can mint tokens the others will accept. With an asymmetric algorithm such as EdDSA, only the identity provider holds the private key. Other services verify with the public key, often fetched as a JWKS document keyed by kid. A stolen public key cannot sign. The first run shows both checks, and then a : the access token stays short-lived, and presenting the same refresh id twice is rejected because the first use already retired it.

What changes

Why these two defaults changed
Old approachWeakness that showed upWhat replaced it
HMAC, one shared secret on every serviceVerification uses the same secret as signing. A leak on any verifier is a leak of the mint. The attacker can sign tokens every service will accept.Asymmetric signing. Services verify with the public key from JWKS. Only the identity provider holds the private key, so a leaked public key cannot mint tokens.
Access token in localStorageAny script on the page can read localStorage, including a script injected by XSS. The attacker copies the token and uses it until it expires.An HttpOnly cookie. The browser stores it and sends it, but document.cookie and page script cannot read it, so that theft path is closed in the browser.

The command

The storage run is the browser rule written out. The cookie is in the jar. Page script is allowed to see localStorage, and it is not allowed to see an HttpOnly cookie.

HMAC secret versus a public key

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

Page script reads localStorage, not the HttpOnly cookie

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

The mistake

Check the result

hmac true and ed true mean both signatures checked. parts is 3. The second refresh presentation prints reuse rejected. The storage run prints the token from localStorage, an empty cookie read, and the jar still holding access_token.

Guided challenge

Stop page script from reading the token

The token sits in localStorage and the jar is empty, so xss prints the token. Move it into an HttpOnly cookie and leave localStorage empty. The script should print an empty read, and the jar count should be 1.

Stop page script from reading the token

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

EXPECTED RESULT

script read []
jar count 1

Hints 0/2

    Use all hints or make 3 failed checks (0/3).

    Free exploration

    Change this example and run it again. Each run is a new process. Nothing is saved.

    Your variation

    3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Why is a shared HMAC secret a bad fit for many services?
    02Why is localStorage the wrong place for an access token?

    Ready to call this one understood?

    Mark this lesson complete to earn 150 XP. You can always revisit it.

    Sign in to save progress
    Next lesson