Anatomy of a JWT: Header, Payload, and Cryptographic Signature
A JSON Web Token (JWT) is a compact, URL-safe means of transferring claims between two parties. Standardized in RFC 7519, a JWT consists of three distinct parts separated by dots (.): the Header, the Payload, and the Signature. The Header specifies the token type (JWT) and signing algorithm (e.g., HS256, RS256). The Payload contains registered and custom claims describing the subject, issuer, and permissions. The Signature is produced by hashing the Base64URL-encoded header and payload with a secret key or private cryptographic key.
// Structure of a JWT
// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkFsZXgiLCJpYXQiOjE1MTYyMzkwMjJ9.signature
// Deconstructed Parts:
// 1. Header: base64url( { "alg": "HS256", "typ": "JWT" } )
// 2. Payload: base64url( { "sub": "1234567890", "name": "Alex", "iat": 1516239022 } )
// 3. Signature: HMACSHA256( base64UrlEncode(header) + "." + base64UrlEncode(payload), secret )