Skip to content

Authenticating callers with JWT

How-to guideThe steps for one task.

Use the jwt policy when callers already sign in with an identity provider (Keycloak, Auth0, Entra ID, Okta, Cognito, or your own) and present the access token it issued. The gateway verifies the token locally against the provider's published keys; the provider is not called per request.

Minimal configuration

policies:
  - type: jwt
    config:
      issuer: https://login.example.com/
      audiences: [orders-api]

What is checked: signature (RS256 and ES256 by default; none is always refused), iss, aud (when audiences is set), exp and nbf with 30 s leeway (leeway, at most 5 m). Keys are cached for cache_ttl (10 min) and refreshed on an unknown key id.

Where the keys come from

The keys must come from the identity provider that signs the tokens, never from the backend: whoever serves the keys can forge tokens the gateway accepts.

From the issuer (the default). With only issuer set, the gateway reads the provider's OpenID Connect discovery document at <issuer>/.well-known/openid-configuration (a trailing slash on the issuer is dropped first) and fetches the key set named in its jwks_uri. Every OpenID Connect provider publishes this document; typical issuers:

Provider issuer
Keycloak https://sso.example.com/realms/<realm>
Auth0 https://<tenant>.auth0.com/
Microsoft Entra ID https://login.microsoftonline.com/<tenant-id>/v2.0
Okta https://<org>.okta.com/oauth2/default
Amazon Cognito https://cognito-idp.<region>.amazonaws.com/<user-pool-id>

For discovery the issuer must be the provider's full https:// address (plain http:// only on this machine, such as a local Keycloak, or with allow_http: true on an isolated network), and the discovery document must name exactly that issuer, as the OpenID Connect specification requires. Copy the issuer value from the document if in doubt. The discovered jwks_uri follows the same https rule.

Nothing is fetched when the configuration is loaded: the document and the keys are fetched on the first request, then cached for cache_ttl and refreshed in the background. Discovery adds one fetch per cache period and nothing to the requests in between. If the document cannot be read on a later refresh, the address it named last stays in use.

The keys are fetched once for the gateway, not once per operation: every jwt policy that names the same provider the same way reads one copy, and a publish keeps it. A provider that is down at the moment you publish refuses nobody.

From a URL. Set jwks_url when the provider has no discovery document at that address, or its keys are served elsewhere. It wins over discovery:

config:
  issuer: https://login.example.com/
  jwks_url: https://login.example.com/.well-known/jwks.json

Inline or shared secret. jwks takes a pasted key set for providers without a reachable URL; hmac_secret is for shared-secret tokens (below). With either, the issuer can be any string the tokens carry.

Where the token is

Authorization: Bearer … by default. from: query or from: cookie with name handles callers that cannot set headers, such as browser downloads or EventSource.

Authorization by scope and claim

config:
  required_scopes: [orders:read]          # every listed scope, from scope or scp
  required_claims:
    tenant: [acme, globex]                # one of these values
    email_verified: true
    employee_id: []                       # present, any value

A token that fails these gets 403 APX-PL-0003 with the missing scope or claim in the details. Put coarse rules on the API and finer ones on operations: orders:read on the API, orders:write on POST and PUT operations.

What the backend receives

  • The token is stripped (strip: true) so a backend cannot replay it. Set strip: false if the backend verifies it too.
  • X-Apinx-Subject carries sub, X-Apinx-Auth: jwt. Spoofed identity headers from the caller are removed first.
  • Any claim can be forwarded as a header with transform: X-Tenant: ${claim.tenant}, X-Dept: ${claim.profile.dept}.

Several providers

Authentication policies bound together are alternatives: a request needs one credential that one of them accepts. Bind the policy twice with names; a token verifying against either is accepted. Each binding only looks at tokens whose iss is its issuer, so a token meant for the other one never makes it fetch keys:

policies:
  - type: jwt
    name: employees
    config: { issuer: https://login.corp.example/ }
  - type: jwt
    name: partners
    config: { issuer: https://partners.example/, jwks_url: https://partners.example/jwks }

Each finds its own keys: employees from its issuer's discovery document, partners from the given URL.

The same holds with the Apinx access token policy (oauth2), for tokens the gateway issues to your own applications, and with api-key: bind jwt and oauth2 together to accept employees signed in with your provider and partner apps with Apinx tokens on one API. A credential addressed to a policy and refused by it is refused at once; a request with none gets 401 with every WWW-Authenticate challenge.

Shared-secret tokens

For HS256 tokens from a legacy system, set hmac_secret (at least 32 characters, stored encrypted) and algorithms: [HS256]. Prefer asymmetric keys whenever the issuer supports them.

A token sent again

A caller sends the same token on every request until it expires. The gateway checks its signature once and remembers that it held, in the memory of each instance (10,000 tokens; 1,000 per environment in Apinx Cloud; nothing is written to Redis or to the store). A repeat costs a tenth of a first sight.

Nothing else changes. On every request the gateway still compares the token's exp and nbf with the clock, still asks the key set for the token's key, and still applies the policy's own audiences, required_scopes and required_claims. When the keys are fetched again, every token is verified again, so a key your provider withdrew stops working exactly when it did before. Only a token that passed is remembered, as its SHA-256, never the token itself; a refused token is checked in full each time. A token longer than 4 KB is verified on every request.

When the provider is unreachable

If the keys (or, without jwks_url, the discovery document) cannot be fetched and none are cached, the policy fails closed with 503 APX-PL-0009; cached keys keep serving through a provider outage. A provider that is down does not stop the configuration from loading, and the gateway tries again at most every 30 seconds. The gateway log names the address that failed and why, for example a discovery document that names another issuer. For providers without a reachable URL, paste the key set in jwks.

Trying it

Send test request on the API with an Authorization header shows the decision and the claims the policy saw, in the trace. A real token from your provider works; for a first try, the built-in demo identity provider (demo.enabled: true, /oidc on the demo port) issues tokens the console can use.