← Questions

Decode a JWT by hand and explain browser authentication

0
MicrosoftCodingLast asked Feb 2026

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 over header.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: localStorage is readable by any injected script (XSS); an HttpOnly, Secure, SameSite cookie hides it from scripts but needs CSRF protection.
  • Validation on every request: signature with the expected algorithm (never trust alg: none or 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 alg to none or 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

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding