What Are JWT Standard Claims?
JWT (JSON Web Token) standard claims are predefined fields that help define the purpose and security rules of a token. These claims act as a shared language between services to verify identity and permissions. The JWT standard claims reference includes commonly used fields like issuer (iss), expiration time (exp), and subject (sub).
Common JWT Standard Claims
The IETF specification defines several mandatory and optional claims. Here are the most important ones:
- **iss (Issuer)**: Identifies the entity that issued the token
- **exp (Expiration Time)**: Sets the timestamp when the token becomes invalid
- **sub (Subject)**: Represents the user or client associated with the token
- **aud (Audience)**: Specifies which recipients can accept the token
- **nbf (Not Before)**: Defines the earliest time the token can be used
These claims form the foundation of secure API authentication. For example, the exp claim prevents token misuse after its expiration time.
How to Use Standard Claims
When creating JWT tokens, you must include relevant standard claims from the JWT standard claims reference. Here's a practical example:
1. Set `iss` to your authentication service identifier
2. Define `exp` using a timestamp (e.g., current time + 24 hours)
3. Add `sub` with the user ID or client application name
4. Specify `aud` to limit token usage to specific services
Most JWT libraries provide methods to automatically handle timestamp calculations for claims like exp and nbf.
Custom Claims vs Standard Claims
While standard claims provide universal compatibility, you can add custom claims for application-specific needs. For instance, you might include:
- `user_role`: Define user permissions
- `company_id`: Restrict access by organization
However, always follow these best practices:
1. Prefix custom claims with a unique namespace to avoid conflicts
2. Keep the total token size under 3KB
3. Validate all claims on the receiving end
Troubleshooting JWT Claims Issues
If you encounter authentication errors, check these common problems:
- **Expired tokens**: Verify the exp claim matches your system's clock
- **Invalid audience**: Confirm the aud claim matches your API's identifier
- **Missing required claims**: Ensure all mandatory claims from the JWT standard claims reference are present
For better security, always sign your tokens with strong algorithms like HS256 or RS256 and store sensitive data on the server instead of in the token itself.