liveBook · Manning

JSON Web Token

As explained in Grokking Web Application Security

A JSON Web Token is a compact, digitally signed token format that carries claims about a subject, such as a user's profile information.

A JSON Web Token (JWT, pronounced “jot”) is a compact JSON data structure used to carry session state or user profile information. It is digitally signed so a recipient can check that its contents were not altered, but signing does not make its contents secret. section 9.4 section 9.10

JWTs are useful when one authenticated web application calls multiple microservices. Each service can verify a token’s authenticity without asking the original authentication service, reducing repeated authentication lookups. A JWT can also act as the profile-bearing token returned through OpenID Connect on top of OAuth. section 9.4 section 8.3

Create a signed token

A Node.js application can sign a small payload such as a user ID and role with a secret key. The receiving service must validate that signature before treating the token as authentication credentials; decoded content is untrusted until then. section 9.4 section 9.10

Treat JWTs as client-side session data with specific safeguards:

  • Validate the digital signature in every microservice that accepts the token; rejecting altered tokens prevents a user from changing claims such as a username. section 9.10
  • Do not put secrets or private session details in a signed JWT, because the client can read signed content. section 9.10 section 9.11
  • Avoid exposing tokens to client-side JavaScript when possible. Sending a JWT in a Cookie marked HttpOnly reduces theft through cross-site scripting; session cookies should also use Secure over HTTPS. section 9.4 section 9.11
  • Use signing or encryption to tamperproof client-side sessions and JWTs, and keep the session-management framework patched. section 9.11
const tokens    = require('jsonwebtoken');
const payload   = { userId: '123456789', role: 'admin' };
const secretKey = process.env.SECRET_KEY;
const jwt       = tokens.sign(payload, secretKey);

This minimal Node.js example signs a JSON payload with a secret key, illustrating the difference between token claims and the signature that protects them. section 9.4

How Grokking Web Application Security covers it

How deep it goes
Defines JWTs and explains signing, validation, transport, readability, and XSS risks without detailing token internals.
Examples
Uses Node.js token signing, a Bearer Authorization request, and a Facebook OpenID sign-on flow.
What it assumes
Introduces JWTs in plain terms while assuming familiarity with web sessions, HTTP headers, and client-server applications.
What you can do after
Operate/configure JWT-based sessions by signing tokens, validating them at each service, and limiting client-side exposure.
How it connects
Connects JWTs with sessions, digital signatures, HMAC, microservices, OAuth, OpenID Connect, cookies, and cross-site scripting.

Also explained in

Compare how 11 books explain JSON Web Token →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Web Application Security. The section links point to the parts of the book it draws on.