JWT Structure
A JWT consists of three Base64Url-encoded parts separated by dots (.). These parts are:
header.payload.signature1. Header
The header typically consists of two parts: the type of the token, which is JWT, and the signing algorithm being used, such as HMAC SHA256 or RSA.
{
"alg": "HS256",
"typ": "JWT"
}Common algorithms: HS256, RS256, ES256, none (insecure)
2. Payload
The payload contains the claims. Claims are statements about an entity (typically, the user) and additional data. There are three types of claims:
Registered Claims
Predefined claims recommended by JWT spec:
iss- Issuersub- Subjectaud- Audienceexp- Expiration timenbf- Not beforeiat- Issued atjti- JWT ID
Public Claims
Defined in the IANA JWT Registry or as a URI
Private Claims
Custom claims agreed upon by parties using JWTs
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022,
"exp": 1516242622
}3. Signature
The signature is used to verify that the sender of the JWT is who it says it is and to ensure that the message wasn't changed along the way.
HMACSHA256(\n base64UrlEncode(header) + '.' +\n base64UrlEncode(payload),\n secret\n)The signature ensures data integrity and authenticity
Complete Example
Here's a decoded example token:
Header
{
"alg": "HS256",
"typ": "JWT"
}Payload
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022,
"exp": 1516242622
}