Overview
The Wacht Rust SDK provides robust JWT (JSON Web Token) validation for authenticating requests. The validation process ensures tokens are properly signed, not expired, and contain valid claims.Validation Process
1. Token Extraction
The SDK extracts tokens from theAuthorization header:
2. Algorithm Detection
The token header is decoded to determine the signing algorithm:3. Signature Verification
Based on the algorithm, the appropriate verification method is used:| Algorithm | Type | Key Type | Description |
|---|---|---|---|
| HS256 | HMAC | Symmetric | HMAC with SHA-256 |
| HS384 | HMAC | Symmetric | HMAC with SHA-384 |
| HS512 | HMAC | Symmetric | HMAC with SHA-512 |
| RS256 | RSA | Asymmetric | RSA Signature with SHA-256 |
| RS384 | RSA | Asymmetric | RSA Signature with SHA-384 |
| RS512 | RSA | Asymmetric | RSA Signature with SHA-512 |
| ES256 | ECDSA | Asymmetric | ECDSA using P-256 and SHA-256 |
| ES384 | ECDSA | Asymmetric | ECDSA using P-384 and SHA-384 |
4. Claims Validation
The SDK validates standard JWT claims:- exp (Expiration Time) - Token must not be expired
- nbf (Not Before) - Token must be valid for use
- iat (Issued At) - Token issue time
- iss (Issuer) - Optional issuer verification
Configuration Options
Clock Skew Tolerance
Allow for small time differences between servers:Disable Expiration Check
For testing or special cases:Require Specific Issuer
Ensure tokens come from a trusted source:Public Key Management
Automatic Fetching
By default, the SDK fetches the public key from your deployment:Manual Configuration
Provide the public key directly:Environment Variable
Set via environment:Token Structure
Header
Payload (Claims)
Validation Errors
Common Error Scenarios
| Error | HTTP Status | Description |
|---|---|---|
| Missing Authorization Header | 401 | No token provided |
| Invalid Token Format | 401 | Not a valid JWT |
| Expired Token | 401 | Token exp claim has passed |
| Invalid Signature | 401 | Token signature doesn’t match |
| Unsupported Algorithm | 401 | Algorithm not in allowed list |
| Invalid Public Key | 500 | Server configuration error |
Error Response Format
X-Auth-Error: Invalid token: ExpiredSignatureWWW-Authenticate: Bearer
Security Considerations
Algorithm Restrictions
The SDK only accepts specific algorithms to prevent attacks:Key Validation
Public keys are validated before use:Time-Based Security
Debugging JWT Issues
Enable Debug Logging
Common Issues and Solutions
Token Expired
Token Expired
Error: Solution: Request a new token or increase token lifetime
Invalid token: ExpiredSignatureDebug:Invalid Signature
Invalid Signature
Error:
Invalid token: InvalidSignatureCauses:- Wrong public key
- Token signed with different key
- Token tampered with
Unsupported Algorithm
Unsupported Algorithm
Error:
Unsupported algorithmSolution: Ensure your token uses a supported algorithm (HS256/384/512, RS256/384/512, ES256/384)Clock Skew
Clock Skew
Error: Token validation fails intermittentlySolution: Increase clock skew tolerance:
Testing JWT Validation
Create Test Tokens
Validate Custom Tokens
Best Practices
- Always Use HTTPS - Tokens can be intercepted over HTTP
- Short Token Lifetime - Reduce window for compromised tokens
- Validate All Claims - Don’t skip expiration or other checks
- Secure Key Storage - Protect private keys, rotate regularly
- Monitor Failures - Log and alert on validation failures
Next Steps
- Token Claims - Understanding claim structure
- Auth Layer - Implementing authentication
- Error Handling - Handling validation errors
