JSON Web Tokens (JWTs) are an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed using a secret key (with HMAC) or a public/private key pair using RSA or ECDSA.
Although JWTs can be encrypted to provide secrecy between parties, signed tokens focus on verifying the integrity of the claims within. When tokens are signed using public/private key pairs, the signature also certifies that only the party holding the private key is the one that signed it.
When should you use JWTs?
- Authorization: The most common scenario for using JWT. Once the user is logged in, each subsequent request will include the JWT, allowing the user to access routes, services, and resources permitted with that token. Single Sign On is a feature that widely uses JWT because of its small overhead and its ability to be easily used across different domains.
- Information Exchange: JWTs are a good way of securely transmitting information between parties. Because JWTs can be signed—for example, using public/private key pairs—you can be sure the senders are who they say they are. Additionally, as the signature is calculated using the header and the payload, you can also verify that the content hasn't been tampered with.
JWT Structure
A JWT consists of three parts separated by dots (.
), which are:
- Header: The header
typically consists of two parts: the type of the token (JWT) and the signing algorithm
being used, such as HMAC SHA256 or RSA. For example:
{"alg": "HS256", "typ": "JWT"}
. This is Base64Url encoded. - Payload: Contains the claims. Claims are statements about an entity (typically, the user) and additional data. There are three types of claims: registered, public, and private claims. This is also Base64Url encoded.
- Signature: To create
the signature part, you have to take the encoded header, the encoded payload, a secret,
the algorithm specified in the header, and sign that. For example, for HMAC SHA256:
HMACSHA256(base64UrlEncode(header) + "." + base64UrlEncode(payload), secret)
.
Types of JWT Claims
The payload portion of a JWT contains claims about the entity, typically the user and additional metadata:
- Registered claims:
These are predefined claims such as
iss
(issuer),exp
(expiration time),sub
(subject),aud
(audience), and others. These claims are not mandatory but recommended to provide a set of useful, interoperable claims. - Public claims: These can be defined at will by those using JWTs. But to avoid collisions they should be defined in the IANA JSON Web Token Registry or be defined as a URI that contains a collision resistant namespace.
- Private claims: These are custom claims created to share information between parties that agree on using them and are neither registered nor public claims.
Security Note: For signed tokens, the information contained within the token is exposed to users or other parties, even though they are unable to change it. This means you should not put secret information within the token unless it is encrypted.