Overview
Ironclad uses JSON Web Tokens (JWT) for stateless authentication. Tokens are generated upon registration or login and must be included in theAuthorization header for protected endpoints.
JWT Structure
JWT tokens contain three parts separated by dots:Example Token
Claims Structure
The payload contains user claims:user.rs:152-159.
Decoded Payload Example
Token Generation
Tokens are generated using thejsonwebtoken crate with HMAC-SHA256 signing:
jwt.rs:7-26.
Token Generation Flow
- Get current timestamp - Used for
iat(issued at) - Calculate expiration - Add configured expiration seconds to current time
- Create claims - Build Claims struct with user data and timestamps
- Encode token - Sign with HMAC-SHA256 using secret key
- Return JWT string - Base64-encoded token ready for use
Usage in Authentication
Tokens are generated after successful registration or login:auth_service.rs:79-84.
Token Validation
Tokens are validated on every request to protected endpoints:jwt.rs:28-36.
Validation Process
- Decode token - Extract header, payload, and signature
- Verify signature - Recompute signature using secret key
- Check expiration - Ensure token hasn’t expired
- Return claims - Extract user information from payload
Automatic Validation
Validation happens automatically in authorization extractors:authentication.rs:135-148.
JWT Configuration
Configure JWT settings in your.env file:
Configuration Structure
Environment-Based Validation
JWT configuration is validated on startup:validators.rs:80-94.
Production Requirements
Recommended Expiration Times
- Short-lived (Recommended)
- Medium-lived
- Long-lived
Using JWT Tokens
Obtaining a Token
Get a token by registering or logging in:Making Authenticated Requests
Include the token in theAuthorization header with the Bearer scheme:
Header Format
The Authorization header must follow this exact format:Authorization- Header nameBearer- Authentication scheme (note the capital B)<token>- Your JWT token (no quotes)- Important: There must be exactly one space between “Bearer” and the token
Token Lifecycle
Token Creation
- User registers or logs in
- Credentials are verified
- JWT token is generated with claims
- Token is returned to client
Token Usage
- Client stores token (localStorage, cookie, etc.)
- Client includes token in Authorization header
- Server extracts and validates token
- Server grants access based on claims
Token Expiration
- Token expiration time is checked on every request
- Expired tokens are rejected with 401 Unauthorized
- Client must obtain a new token (re-login or refresh)
Claims Usage
Access user information from JWT claims in your handlers:Get User ID
Get User Email
Get User Role
Error Handling
Common JWT Errors
Invalid Token Format
Invalid Token Format
Error:
ApiError::UnauthorizedCause: Token is malformed or not a valid JWTSolution: Ensure token has three parts separated by dotsInvalid Signature
Invalid Signature
Error:
ApiError::JwtError("InvalidSignature")Cause: Token was signed with a different secret keySolution: Verify JWT_SECRET matches between token generation and validationToken Expired
Token Expired
Error:
ApiError::JwtError("ExpiredSignature")Cause: Token’s exp claim is in the pastSolution: Obtain a new token by logging in againMissing Authorization Header
Missing Authorization Header
Invalid Bearer Format
Invalid Bearer Format
Error:
ApiError::UnauthorizedCause: Authorization header doesn’t start with “Bearer ”Solution: Ensure header format: Bearer <token> with spaceError Response Format
Security Best Practices
Secret Key Management
Use Different Secrets Per Environment
Use different secrets for development, staging, and production:
Token Storage
Token Transmission
- Always use HTTPS in production
- Never include tokens in URL query parameters
- Never log tokens in application logs
- Use Authorization header instead of custom headers
Expiration Strategy
Testing JWT Tokens
Test token generation and validation:Related Documentation
Authentication
Complete authentication flow with registration and login
Authorization
Role-based access control with custom extractors