A JWT goes through these stages: issue, store, use, validate, expire, renew and (optionally) revoke. 1. Issuance - The user authenticates (password, SSO or social login) with the authorization server or your auth endpoint. - The server creates a token with a header (alg, typ, kid), payload claims (sub, iss, aud, exp, iat, nbf, roles or scopes) and a signature using its private key or secret. - It usually returns a short-lived access token (for example 15 minutes) and a longer-lived refresh token. 2. Storage on the client - SPA: in memory (or via a BFF with HttpOnly cookies); mobile: secure storage (Keychain or Keystore). Avoid localStorage where possible because of XSS. 3. Usage - Sent on each API request: Authorization: Bearer <token>. 4. Validation by the API (every request) - Verify the signature using the issuer's public key (fetched from the JWKS endpoint and cached). - Check iss (issuer), aud (audience), exp (not expired) and nbf (not before), allowing small clock skew. - Build the user principal from claims, then apply authorization policies. - In ASP.NET Core, AddJwtBearer does all of this. 5. Expiry - After exp, the API rejects the token with 401 Unauthorized (often with a WWW-Authenticate header indicating an invalid token). 6. Renewal - The client uses the refresh token at the token endpoint to get a new access token, ideally with refresh-token rotation. If the refresh token is expired or revoked, the user must sign in again. 7. Revocation and logout - JWTs are stateless, so a valid access token cannot be "deleted" before it expires. Strategies: keep access tokens short-lived; revoke refresh tokens on logout or password change; for high-risk cases, keep a deny-list of token IDs (jti) or check a security-stamp or session version claim. 8. Key rotation - The issuer periodically rotates signing keys; the kid header tells APIs which key to use, and JWKS publishing lets them pick up new keys automatically. Throughout: use HTTPS, avoid putting sensitive data in the payload (it is only encoded, not encrypted), and log authentication failures without logging tokens.
Ravindra Wadile · · 0 likes