JWT Claims and Validation
You'll learn how JWTs carry data through claims and how to verify their authenticity and freshness.
What Are JWT Claims?
A JWT (JSON Web Token) contains claims—key-value pairs that carry information about the user or token itself. Think of a claim like a stamp on a passport: it asserts something specific about the holder. Claims are organized into three categories:
Standard Claims
These are predefined, widely-recognized fields that serve common security needs:
iss(issuer): Who created the token (e.g.,"auth.example.com")sub(subject): Who the token is about (typically a user ID)exp(expiration): When the token expires (Unix timestamp)iat(issued at): When the token was createdaud(audience): Who should accept this token
Custom Claims
You can add application-specific data like role, permissions, or email. Just avoid collisions with standard names.
Validating JWTs
When your server receives a JWT, it must validate three critical aspects:
1. Signature Verification
Check that the token was signed by your secret key (HMAC) or private key (RSA). If the signature doesn't match, someone tampered with it—reject immediately.
2. Expiration Check
Verify that the current time is before the exp claim. Expired tokens are invalid, even with valid signatures.
3. Issuer Verification
Confirm the iss claim matches your expected issuer. This prevents tokens from other systems being used maliciously.
Why This Matters
Without proper validation, attackers could forge tokens, replay expired ones, or use tokens meant for different systems. Always validate before trusting any claim data.
Key Takeaway: JWT claims carry user data, but you must verify the signature, expiration, and issuer on every request to ensure the token is authentic, current, and intended for your system.