> ## Documentation Index
> Fetch the complete documentation index at: https://tyk.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# JWT Signature Validation

> How to validate JWT signatures in Tyk API Gateway.

## 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:

1. **Integrity:**
   If anyone modifies the header or payload, the signature will no longer match.

2. **Authenticity:**
   Confirms that the token was issued by a trusted source.

3. **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**:

```
HMACSHA256(
  base64UrlEncode(header) + "." + base64UrlEncode(payload),
  secret
)
```

Or, for asymmetric algorithms like RSA or ECDSA:

```
RSASHA256(
  base64UrlEncode(header) + "." + base64UrlEncode(payload),
  private_key
)
```

### Verification Process

When Tyk receives a JWT, it performs the following steps to validate the signature:

1. It extracts the header and payload.
2. Recomputes the signature using its own secret/public key.
3. 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:

1. **Locally Stored Keys**: The key or secret is stored directly in the API definition.
2. **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.
3. **Remotely Stored Keys**: Tyk retrieves the key from a public JSON Web Key Set (JWKS) endpoint.

Depending on the cryptographic style used, the options vary:

| 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 in `server.authentication.securitySchemes.<jwtAuthScheme>.source` (in Tyk Classic, this is [jwt\_source](/5.12/api-management/gateway-config-tyk-classic#configuring-authentication-for-tyk-classic-apis).

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`:

```yaml theme={null}
x-tyk-api-gateway:
  server:
    authentication:
      securitySchemes:
        jwtAuth:
          source: bXlzZWNyZXQ=
```

Refer to the [Tyk OAS API Definition](/5.12/api-management/gateway-config-tyk-oas#jwt) reference for details.

### External Key Value Store

For improved separation of concerns and flexibility, the key/secret can be placed in an [external key value store](/5.12/tyk-configuration-reference/kv-store), with the appropriate reference configured in the API definition.

For example, this fragment configures the JWT authentication middleware to use the secret at `consul://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`:

```yaml theme={null}
x-tyk-api-gateway:
  server:
    authentication:
      securitySchemes:
        jwtAuth:
          source: Y29uc3VsOi8vc2VjcmV0cy9qd3Qtc2VjcmV0
```

### 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](/5.12/#single-jwks-endpoint) or [multiple](/5.12/#multiple-jwks-endpoints) JWKS endpoints with [caching](/5.12/#jwks-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 in `server.authentication.securitySchemes.<jwtAuthScheme>.source` (in Tyk Classic, this is [jwt\_source](/5.12/api-management/gateway-config-tyk-classic#configuring-authentication-for-tyk-classic-apis)). 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`:

```yaml theme={null}
x-tyk-api-gateway:
  server:
    authentication:
      securitySchemes:
        jwtAuth:
          source: aHR0cHM6Ly95b3VyLXRlbmFudC5hdXRoMC5jb20vLndlbGwta25vd24vandrcy5qc29u
```

#### 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>
  **Note**

  * The `<jwtAuthScheme>.jwksURIs` URIs are not base64 encoded in the API definition and so are human-readable.
  * If both `<jwtAuthScheme>.source` and `<jwtAuthScheme>.jwksURIs` are configured, the latter will take precedence.
  * Multiple JWKS endpoints and the `jwksURIs` array are not supported by Tyk Classic APIs.
</Note>

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:

```yaml theme={null}
x-tyk-api-gateway:
  server:
    authentication:
      securitySchemes:
        jwtAuth:
          jwksURIs:
            - url: https://your-tenant.auth0.com/.well-known/jwks.json
            - url: http://your-keycloak-host/realms/tyk-demo/protocol/openid-connect/certs
```

## JWKS caching

Tyk caches the [JSON Web Key Set](https://datatracker.ietf.org/doc/html/rfc7517) (JWKS) retrieved from JWKS endpoints to reduce the performance impact of contacting external services during request handling.

### Feature Compatibility Summary

From **Tyk 5.10.0** onwards, we have introduced enhanced JWKS caching for [Tyk OAS APIs](/5.12/api-management/gateway-config-tyk-oas) 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

The table below summarizes the availability of JWKS caching features between Tyk Classic and Tyk OAS APIs:

| Feature                    | Tyk Classic | Tyk OAS | Available From |
| -------------------------- | ----------- | ------- | -------------- |
| Single JWKS endpoint       | ✅           | ✅       | All versions   |
| Multiple JWKS endpoints    | ❌           | ✅       | Tyk 5.9.0+     |
| Configurable cache timeout | ✅           | ✅       | Tyk 5.12.0+    |
| Per-API cache timeout      | ❌           | ✅       | Tyk 5.10.0+    |
| Pre-fetch functionality    | ❌           | ✅       | Tyk 5.10.0+    |
| Cache management API       | ✅           | ✅       | Tyk 5.10.0+    |

### Configuration Options

#### Gateway Config

Unless otherwise configured, all JWKS cache entries expire after a period of *240 seconds* after which the cache is refreshed when a new request is received.

From **Tyk 5.12.0**, this default JWKS cache timeout is configurable from the Gateway config file (`tyk.conf`) or the equivalent environment variable:

```json theme={null}
{
  "jwks": {
    "cache": {
      "timeout": 90
    }
  }
}
```

The [`timeout`](/5.12/tyk-oss-gateway/configuration#jwks-cache-timeout) is given in seconds. If set, this will override the default period of 240 seconds.

#### Tyk OAS API

When using Tyk OAS APIs, from **Tyk 5.10**, a more granular configuration is available with separate validity periods per JWKS endpoint. If configured, these will override the Gateway-level configuration for the specified JWKS endpoints.

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:

```yaml theme={null}
x-tyk-api-gateway:
  server:
    authentication:
      securitySchemes:
        jwtAuth:
          jwksURIs:
            - url: https://your-tenant.auth0.com/.well-known/jwks.json
              cacheTimeout: "300s"  # 5 minutes
            - url: http://your-keycloak-host/realms/tyk-demo/protocol/openid-connect/certs
              cacheTimeout: "3m"    # 3 minutes (alternative format)  
```

| 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. |

For more details, refer to the [Tyk OAS API definition reference](/5.12/api-management/gateway-config-tyk-oas#jwk).

<Note>
  Tyk Classic APIs do not support granular cache validity periods and use the timeout set in the Gateway config (from Tyk 5.12.0) or the default (24o seconds). The enhanced caching features are available only for Tyk OAS APIs.
</Note>

### JWKS Cache Management

Tyk Gateway and Dashboard APIs expose endpoints to manage JWKS caches programmatically for both Tyk OAS and Tyk Classic APIs:

| Endpoint                  | Method   | Description                                                        | Availability |
| ------------------------- | -------- | ------------------------------------------------------------------ | ------------ |
| `/tyk/cache/jwks`         | `DELETE` | Invalidate JWKS caches for all APIs                                | Tyk 5.10.0+  |
| `/tyk/cache/jwks/{apiID}` | `DELETE` | Invalidate JWKS cache for a specific API                           | Tyk 5.10.0+  |
| `/api/cache/jwks/{apiID}` | `DELETE` | Invalidate JWKS cache for a specific API on all connected Gateways | Tyk 5.11.0+  |

<Note>
  The Dashboard API endpoint is restricted to users with `admin` privileges and can only be used to flush the cache for APIs in the user's [Organisation](/5.12/tyk-dashboard-api#organisations-apis-and-users).
</Note>

**Example usage:**

```bash theme={null}
# Flush all JWKS caches
curl -X DELETE http://your-gateway:8080/tyk/cache/jwks \
  -H "x-tyk-authorization: your-gateway-secret"

# Flush JWKS cache for specific API
curl -X DELETE http://your-gateway:8080/tyk/cache/jwks/your-api-id \
  -H "x-tyk-authorization: your-gateway-secret"

# Flush JWKS cache for specific API on all connected Gateways
curl -X DELETE http://your-dashboard:8080/api/cache/jwks/your-api-id \
  -H "authorization: your-dashboard-secret"
```

## FAQ

<AccordionGroup>
  <Accordion title="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.
  </Accordion>

  <Accordion title="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.
  </Accordion>

  <Accordion title="What happens if the JWKS endpoint is temporarily unavailable?">
    Tyk caches JWKS responses. You can configure the cache timeout using the CacheTimeout parameter in the `JWTJwksURIs` configuration. This ensures that temporary JWKS endpoint outages don't affect API availability.
  </Accordion>

  <Accordion title="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.
  </Accordion>

  <Accordion title="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.
  </Accordion>

  <Accordion title="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.
  </Accordion>
</AccordionGroup>
