Availability
| Component | Editions |
|---|---|
| Tyk Gateway | Community and Enterprise |
Introduction
A JSON Web Token consists of three parts separated by dots:header.payload.signature. The signature verifies that the sender of the JWT is who it claims to be and that the message wasn’t altered along the way.
Tyk can validate the signature of incoming JWTs to ensure that they meet your security requirements before granting access to your APIs.
JWT Signature Fundamentals
The JWT signature serves three main purposes:- Integrity: If anyone modifies the header or payload, the signature will no longer match.
- Authenticity: Confirms that the token was issued by a trusted source.
- Security: Prevents malicious users from forging tokens or altering claims.
How JWT Signatures are Created
The JWT signature is created by taking the encoded header, the encoded payload, a secret, and the algorithm specified in the header. Example:Verification Process
When Tyk receives a JWT, it performs the following steps to validate the signature:- It extracts the header and payload.
- Recomputes the signature using its own secret/private key.
- Compares it with the token’s signature.
- If they match, the token is valid.
- If not, the token is rejected.
Supported Algorithms for Signature Validation
| Method | Cryptographic Style | Secret Type | Supported Locations for Secret | Supported Algorithms |
|---|---|---|---|---|
| HMAC | Symmetric | Shared secret | API definition | HS256, HS384, HS512 |
| RSA | Asymmetric | Public key | API definition, JWKS endpoint | RS256, RS384, RS512, PS256, PS384, PS512 |
| ECDSA | Asymmetric | Public key | API definition, JWKS endpoint | ES256, ES384, ES512 |
Configuration Options
You can configure JWT signature validation using the Dashboard UI or the Tyk API definition. To configure it, you must provide Tyk with the secret or key to validate incoming JWTs. Tyk supports three methods for referencing keys and secrets:- Locally Stored Keys: The key or secret is stored directly in the API definition.
- External Key Value Store: The key or secret is stored in an external key value store (e.g., Consul, Vault) and referenced in the API definition.
- Remotely Stored Keys: Tyk retrieves the key from a public JSON Web Key Set (JWKS) endpoint.
| Cryptographic Style | Locally Stored Keys | External Key Value Store | Remotely Stored Keys (JWKS) |
|---|---|---|---|
| Symmetric | Yes | Yes | No |
| Asymmetric | Yes | Yes | Yes |
Locally Stored Keys and Secrets
When storing the key or secret in the API definition, it is first base64 encoded and then configured inserver.authentication.securitySchemes.<jwtAuthScheme>.source (in Tyk Classic, this is jwt_source.
For example, the Tyk OAS fragment below configures the secret mysecret to validate the signatures of incoming JWTs. Note that the secret has been base64 encoded and then stored in source:
External Key Value Store
For improved separation of concerns and flexibility, the key/secret can be placed in an external key value store, with the appropriate reference configured in the API definition. For example, this fragment configures the JWT authentication middleware to use the secret atconsul://secrets/jwt-secret to validate the signatures of incoming JWTs. Note that the external KV store reference has been base64 encoded and then stored in source:
Remotely Stored Keys (JWKS endpoint)
Tyk can retrieve public keys from JSON Web Key Sets (JWKS) endpoints to validate the signature of incoming JWTs. Tyk supports configuring single or multiple JWKS endpoints with caching capabilities.Feature Compatibility Summary
| Feature | Tyk Classic APIs | Tyk OAS APIs | Available From |
|---|---|---|---|
| Single JWKS Endpoint Support | ✅ | ✅ | All versions |
| Multiple JWKS Endpoint Support | ❌ | ✅ | 5.9.0 |
Single JWKS endpoint
Before Tyk 5.9.0 and when using Tyk Classic APIs, Tyk can only retrieve a single JSON Web Key Set from a JWKS endpoint configured inserver.authentication.securitySchemes.<jwtAuthScheme>.source (in Tyk Classic, this is jwt_source). This field accepts the base64-encoded full URI (including the protocol) of the JWKS endpoint.
For example, the following Tyk OAS fragment configures the JWT authentication middleware to retrieve the JWKS from https://your-tenant.auth0.com/.well-known/jwks.json when validating the signatures of incoming JWTs. Note that the JWKS endpoint has been base64 encoded and then stored in source:
Multiple JWKS endpoints
From Tyk 5.9.0 onwards, Tyk OAS APIs can validate against multiple JWKS endpoints, allowing you to use different IdPs to issue JWTs for the same API. Multiple JWKS endpoints can be configured in the<jwtAuthScheme>.jwksURIs array. Tyk will retrieve the JSON Web Key Sets from each of these endpoints, which will be used to attempt to validate the received JWT.
Note
- The
<jwtAuthScheme>.jwksURIsURIs are not base64 encoded in the API definition and so are human-readable. - If both
<jwtAuthScheme>.sourceand<jwtAuthScheme>.jwksURIsare configured, the latter will take precedence. - Multiple JWKS endpoints and the
jwksURIsarray are not supported by Tyk Classic APIs.
JWKS caching
Tyk caches the JSON Web Key Set (JWKS) retrieved from JWKS endpoints to reduce the performance impact of contacting external services during request handling. A separate cache is maintained for each JWKS endpoint and API, with a default validity period of 240 seconds, after which the cache is refreshed when a new request is received. For example, the following fragment will configure the JWT authentication middleware to retrieve the JWKS from both Auth0 and Keycloak when validating the signature of incoming JWTs, assigning a 300 second validity period to the Auth0 JWKS and 180 second validity period for Keycloak:Feature Compatibility Summary
From Tyk 5.10.0 onwards, we have introduced enhanced JWKS caching for Tyk OAS APIs with the following improvements:- Configurable cache timeout - Set custom validity periods per JWKS endpoint
- Pre-fetch functionality - Automatically retrieve and cache all JWKS when the API loads to the Gateway, ensuring the first request doesn’t experience the latency of fetching keys from external endpoints
- Cache management API - New endpoints to manually invalidate JWKS caches
| Feature | Tyk Classic | Tyk OAS | Available From |
|---|---|---|---|
| Single JWKS endpoint | ✅ | ✅ | All versions |
| Multiple JWKS endpoints | ❌ | ✅ | Tyk 5.9.0+ |
| Configurable cache timeout | ❌ | ✅ | Tyk 5.10.0+ |
| Pre-fetch functionality | ❌ | ✅ | Tyk 5.10.0+ |
| Cache management API | ✅ | ✅ | Tyk 5.10.0+ |
Configuration Options
| Field | Type | Description | Default | Supported Formats |
|---|---|---|---|---|
url | string | JWKS endpoint URL | Required | Full URI including protocol |
cacheTimeout | string | Cache validity period | 240s | "300s", "5m", "1h", etc. |
NoteTyk Classic APIs continue to use the existing JWKS caching behavior with the 240-second default timeout. The enhanced caching features are available only for Tyk OAS APIs.
JWKS Cache Management
New Gateway API endpoints are available from Tyk 5.10.0 to manage JWKS caches programmatically. These endpoints work for both Tyk OAS and Tyk Classic APIs:| Endpoint | Method | Description |
|---|---|---|
/tyk/cache/jwks | DELETE | Invalidate JWKS caches for all APIs |
/tyk/cache/jwks/{apiID} | DELETE | Invalidate JWKS cache for a specific API |
FAQ
Can I use different signing methods for different APIs?
Can I use different signing methods for different APIs?
Yes, each API definition can have its own JWT configuration with different signing methods and keys.
How do I handle JWTs signed with different keys (e.g., from different issuers)?
How do I handle JWTs signed with different keys (e.g., from different issuers)?
You can use JWKS (JSON Web Key Sets) by configuring
JWTJwksURIs with the URLs of your JWKS endpoints. Tyk will automatically fetch and use the appropriate key based on the kid (Key ID) in the token header.What happens if the JWKS endpoint is temporarily unavailable?
What happens if the JWKS endpoint is temporarily unavailable?
Can I use JWKS endpoints with symmetric cryptography (HMAC)?
Can I use JWKS endpoints with symmetric cryptography (HMAC)?
No, JWKS endpoints are designed for asymmetric cryptography (RSA and ECDSA), where public keys are used for signature verification. Symmetric cryptography (HMAC) requires a shared secret, which cannot be retrieved from a JWKS endpoint.
How often does Tyk refresh the JWKS cache?
How often does Tyk refresh the JWKS cache?
By default, Tyk refreshes the JWKS cache every 240 seconds. However, you can customize the cache timeout for each JWKS endpoint in Tyk OAS APIs using the
cacheTimeout field in the API definition.Is JWKS Pre-fetching configurable?
Is JWKS Pre-fetching configurable?
The JWKS (JSON Web Key Set) pre-fetching functionality in Tyk Gateway is automatic and not configurable in terms of enabling/disabling it. When you configure JWKS URLs in your API definition, Tyk will automatically pre-fetch the keys when the API loads.