Overview
These endpoints demonstrate KrakenD’s JWT validation and authorization capabilities using two different token providers: Keycloak and a custom JWT signer. Endpoints:GET /private/moderate- Requires Keycloak JWT with moderator roleGET /private/custom- Requires custom JWT with specific roles
What It Demonstrates
- JWT Validation: Verifies token signatures using JWK
- Role-Based Access Control (RBAC): Enforces role requirements
- Multiple Auth Providers: Supports different JWT issuers
- Nested Claims: Handles complex JWT claim structures
GET /private/moderate
Description
A protected endpoint that requires a valid JWT token from Keycloak with themoderator role.
HTTP Method: GETEndpoint:
/private/moderateCache TTL: 1 second
Authentication
Required: JWT token inAuthorization header
Format:
moderator (in realm_access.roles)
Request Example
Expected Response (Success)
Status:200 OK
Error Responses
Missing Token:401 Unauthorized
Invalid Token:
401 Unauthorized
Insufficient Permissions:
403 Forbidden
Backend Service
- Host:
http://fake_api(inherited from global config) - URL Pattern:
/user/1.json - Purpose: Returns user data after successful authentication
KrakenD Configuration
Key Configuration Options
Algorithm:"playground" in the aud claim.
Nested Roles:
moderator role to access this endpoint.
JWK URL:
GET /private/custom
Description
A protected endpoint that requires a custom JWT token signed with HMAC-SHA256, containing specific roles. HTTP Method:GETEndpoint:
/private/custom
Authentication
Required: JWT token inAuthorization header
Format:
role_a AND role_c (user must have both)
Request Example
Expected Response (Success)
Status:200 OK
Error Responses
Same as/private/moderate.
Backend Service
- Host:
http://fake_api(inherited from global config) - URL Pattern:
/user/1.json - Purpose: Returns user data after successful authentication
KrakenD Configuration
Key Configuration Options
Algorithm:https://krakend.io (checked via the iss claim).
Audience Validation:
http://api.example.com.
Flat Roles Structure:
role_a AND role_c to access this endpoint.
JWT Token Structure
Keycloak Token Example
Custom Token Example
Obtaining Tokens
Get Keycloak Token
Get Custom Token
Use the/token endpoint (see token.mdx):
Comparison
| Feature | /private/moderate | /private/custom |
|---|---|---|
| Algorithm | RS256 (Asymmetric) | HS256 (Symmetric) |
| Provider | Keycloak | KrakenD JOSE |
| Roles Key | realm_access.roles | roles |
| Issuer | Keycloak realm | https://krakend.io |
| Audience | playground | http://api.example.com |
| Required Roles | moderator | role_a AND role_c |
Use Cases
/private/moderate
- Enterprise SSO with Keycloak
- Multi-application authentication
- OIDC-compliant systems
- Complex role hierarchies
/private/custom
- Internal microservices
- Custom authentication systems
- Lightweight JWT validation
- API-to-API communication
Security Considerations
JWK Security
- Remove
disable_jwk_securityor set tofalse - Use HTTPS for JWK URLs
- Implement JWK caching
- Use proper certificate validation
Algorithm Selection
- RS256 (Keycloak): More secure, public/private key pair, rotate keys easily
- HS256 (Custom): Faster, but shared secret must be protected