Decode a JWT by hand and explain browser authentication
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Microsoft's coding round, last asked Feb 2026. It tests authentication, jwt and parsing.
Two parts. First, explain how a browser stays signed in with JSON Web Tokens: how the token is issued, stored and sent, how the server checks it, how sessions are renewed, and the security risks. Then decode a JWT string without a JWT library and say what verifying its signature takes.
Part 1: browser authentication with JWTs
Walk through sign-in, where the token lives in the browser, how it travels with each request, refresh tokens and session renewal, sign-out and revocation, and how this compares with a classic server-side session cookie.
Part 2: decode a token
Unless you agree on a different interface with the interviewer, assume:
-
decode_jwt(token: str) -> tuple[dict, dict, bytes]— splits on., base64url-decodes header and payload (restoring the stripped=padding), parses them as JSON, and returns the raw signature bytes. Raise on a malformed token. -
verify_hs256(token: str, secret: bytes) -> bool— recomputes HMAC-SHA256 overheader.payload(the original encoded text) and compares in constant time.
token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
".eyJzdWIiOiJ1NDIiLCJuYW1lIjoiQWRhIiwiZXhwIjoxNzY3MjI1NjAwfQ"
".hvo1jEJFOkKF7BSuU5DV8A9bB8cxL82_tVMB3dnOTpU"
decode_jwt(token) -> ({"alg": "HS256", "typ": "JWT"},
{"sub": "u42", "name": "Ada", "exp": 1767225600},
<32 signature bytes>)
verify_hs256(token, b"secret") -> True
verify_hs256(token, b"wrong") -> False
Clarifying questions
- Is the token used by a single-page app calling an API, or by server-rendered pages?
- Same site or cross-site API?
- Symmetric (HS256) or asymmetric (RS256, ES256) signing?
- How quickly must a revoked session stop working?
What a strong answer covers
- The token structure: header, payload (claims such as
sub,exp,iat,aud,iss) and signature; base64url is encoding, not encryption, so anyone can read the payload. - Storage trade-offs:
localStorageis readable by any injected script (XSS); anHttpOnly,Secure,SameSitecookie hides it from scripts but needs CSRF protection. - Validation on every request: signature with the expected algorithm (never trust
alg: noneor let the token pick), expiry, audience and issuer. - Short-lived access tokens with a refresh token (rotated, stored safely) to renew sessions, and why revocation is hard without server state (denylist, short TTL).
- Server sessions in contrast: an opaque id, easy revocation, a lookup per request.
- Correct decoding: padding, the URL-safe alphabet, and verifying over the original encoded segments rather than re-serialized JSON.
Follow-ups
- An attacker changes
algtononeor to HS256 with the public key as the secret. What stops it? - How do you sign out a user everywhere immediately?
- Verify an RS256 token with a public key fetched from a JWKS endpoint, including key rotation.
- Where would you put the token in a mobile app instead?
Where it's asked
- Microsoft · Coding · Last asked Feb 202660 min · Ten to fifteen minutes of behavioral questions, then coding in HackerRank where you write a main and make the tests pass; usually three rounds · No AI unless the interviewer invites it; some 2026 loops add a round where you bring your own AI agent
What it tests
authentication · jwt · parsing
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
