Q2: What is a JSON Web Token (JWT) and what are its three structural components?
A JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained format for securely transmitting information between parties as a JSON object.
A JWT consists of three parts separated by dots (.):
- Header: Specifies the token type (
JWT) and signing algorithm (HS256,RS256). - Payload: Contains the claims (statements about an entity, e.g., user ID, roles, expiration time).
- Signature: Cryptographic signature generated by combining the Base64URL-encoded header and payload with a secret or private key.
JWT String Structure and Decoded Components
// Base64URL Encoded JWT String:
// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
// Decoded Header:
{
"alg": "HS256",
"typ": "JWT"
}
// Decoded Payload:
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
}Header and Payload are Base64URL encoded JSON strings. The signature is computed using the secret key over the encoded header and payload.
Key Takeaways
A JWT consists of three Base64URL encoded strings separated by dots: Header, Payload, and Signature.
JWT payloads are encoded, not encrypted by default; sensitive raw data must never be placed inside a standard JWT.