Overview
This endpoint demonstrates KrakenD’s JWT signing capability. It fetches token data from a backend service and signs the tokens using HMAC-SHA256 before returning them to the client. HTTP Method:POSTEndpoint:
/token
What It Demonstrates
- JWT Signing: Signs JWTs using the
auth/signercomponent - Token Generation: Creates access and refresh tokens
- HMAC Algorithm: Uses HS256 for symmetric signing
- Key Management: References local JWK files for signing keys
Request Example
Expected Response
Token Payload
When decoded, the access token contains:Header
Payload (Claims)
aud- Audience (who the token is intended for)exp- Expiration time (Unix timestamp)iat- Issued at (Unix timestamp)iss- Issuer (who created the token)jti- JWT ID (unique token identifier)roles- User roles (custom claim)sub- Subject (user identifier)
Backend Service
- Host:
http://fake_api(inherited from global config) - URL Pattern:
/token.json - Purpose: Returns unsigned token data that KrakenD will sign
Backend Response Format
The backend returns token data in this format:access_token and refresh_token objects and converts them to JWT strings.
KrakenD Configuration
Key Configuration Options
auth/signer
Configures JWT signing behavior:
Algorithm (alg)
HS256- HMAC with SHA-256 (symmetric key)- Other options:
HS384,HS512,RS256,RS384,RS512,ES256, etc.
Key ID (kid)
Keys to Sign (keys_to_sign)
- Extracts the object from each field
- Signs it as a JWT
- Replaces the object with the JWT string
JWK Local Path (jwk_local_path)
Disable JWK Security
- Remove this option or set to
false - Use proper key management
- Rotate keys regularly
- Protect JWK files with appropriate permissions
Using the Token
Once you receive the token, use it to access protected endpoints:Token Lifecycle
1. Generate Token
2. Use Access Token
3. Token Expires
Afterexpires_in seconds (3600 = 1 hour), the access token expires.
4. Refresh Token
Use the refresh token to obtain a new access token (requires additional endpoint configuration).Supported Algorithms
Symmetric (HMAC)
HS256- HMAC with SHA-256HS384- HMAC with SHA-384HS512- HMAC with SHA-512
Asymmetric (RSA)
RS256- RSA with SHA-256RS384- RSA with SHA-384RS512- RSA with SHA-512
Asymmetric (ECDSA)
ES256- ECDSA with P-256 and SHA-256ES384- ECDSA with P-384 and SHA-384ES512- ECDSA with P-521 and SHA-512
Security Best Practices
1. Use Strong Secrets
For HS256, use a strong random secret (at least 256 bits):2. Rotate Keys Regularly
Update the JWK file periodically and use thekid to support multiple keys during rotation.
3. Set Appropriate Expiration
- Access tokens: Short-lived (15 minutes - 1 hour)
- Refresh tokens: Longer-lived (days to weeks)
4. Validate Tokens Properly
Useauth/validator on protected endpoints to verify:
- Signature validity
- Expiration time
- Issuer
- Audience
- Required claims
5. Protect JWK Files
Use Cases
- Authentication Gateway: Central token issuance for microservices
- OAuth2 Token Endpoint: Generate OAuth2-compliant access tokens
- Session Management: Issue session tokens after login
- API Key Rotation: Generate time-limited API keys
- Service-to-Service Auth: Create tokens for internal service communication
Integration with /private/custom
This endpoint works seamlessly with/private/custom:
- Client calls
POST /tokento get a signed JWT - Client uses the
access_tokento callGET /private/custom - KrakenD validates the token using
auth/validator - If valid, the request proceeds to the backend
Troubleshooting
Token Not Signing
Issue: Backend response contains objects, not JWT strings Solution: Verifykeys_to_sign matches the backend response field names.
Invalid Signature
Issue: Token validation fails on protected endpoints Solution: Ensure the same JWK file is used for both signing and validation.Key Not Found
Issue: Error: “kid ‘sim2’ not found in JWK” Solution: Verify thekid value matches an entry in the JWK file.