What is a JSON Web Token (JWT)?
The JWT is an open standard (RFC 7519) that defines a compact, self-contained way to securely transmit information between two parties as a JSON object. It is now widely used forAPI authentication (REST/GraphQL) and user authorization in modern applications (single-page applications).
Token structure
A JWT always consists of 3 distinct parts, separated by a dot (.) :
- The header: Usually contains the token type ("JWT") and the signing algorithm used (e.g. HMAC SHA256 or RSA).
- The payload: Contains the "claims". This is the information about the user, such as their ID, their role (Admin/User) and the token expiration date (exp).
- The signature: This is the security part. It is created by the server by hashing the header, the payload and a Secret. It lets the server verify that the token was not tampered with by an attacker along the way.
Warning: a JWT is not encrypted!
It is a very common mistake to think a JWT is "encrypted". The header and payload are simply Base64Url-encoded. As you can see with this tool, anyone who intercepts the token can read it easily. Never put passwords or sensitive data in a JWT payload! What makes a JWT safe is not the secrecy of its content, but its signature, which prevents tampering.