Implementing JWT Validation in Kong Plugins: Configuration & Routing
When designing secure API gateways, token verification must occur before upstream routing. Integrating JWT validation directly into Kong’s execution pipeline ensures consistent policy enforcement across microservices. This approach aligns with broader Middleware Chains & Request Transformation strategies, where authentication acts as the first gatekeeper in the request lifecycle. Proper binding to specific routes or services prevents unauthorized payload forwarding and reduces upstream compute overhead.
Declarative Configuration Syntax
The exact Kong configuration for JWT validation requires precise YAML formatting. Below is the production-ready declarative syntax for binding the jwt plugin to a specific route, including required claims and signature algorithm enforcement. Ensure key_claim_name matches the exact claim used by your identity provider to locate the public key or shared secret.
plugins:
- name: jwt
route: my-api-route
config:
key_claim_name: iss
claims_to_verify:
- exp
- nbf
secret_is_base64: false
run_on_preflight: false
maximum_expiration: 0
clock_skew: 30
algorithm: RS256
Configuration Directives:
key_claim_name: Defines the JWT claim used to fetch the credential (e.g.,iss,kid). Must exactly match a credential record in Kong’s database.claims_to_verify: Array of mandatory claims.expandnbfenforce token lifecycle boundaries.algorithm: Explicitly restrict validation toRS256orHS256to prevent algorithm-switching vulnerabilities.clock_skew: Tolerance in seconds for expiration validation. Set to30only to accommodate distributed NTP drift.run_on_preflight: Set tofalseto bypass signature checks onOPTIONSrequests, preserving standard CORS handshakes.
Narrow Routing Pattern & Execution Pipeline
JWT validation should be scoped to routes that require bearer token authentication, avoiding global application unless explicitly required. By attaching the plugin at the route level, you enable granular Authentication Proxying & Token Validation without impacting public health checks or webhook endpoints.
The routing pattern relies on Kong’s deterministic plugin execution order:
- Phase Execution: The
jwtplugin executes in theaccessphase, intercepting the request before upstream routing. - Token Extraction: Kong parses the
Authorization: Bearer <token>header. If missing, it immediately returns401 Unauthorized. - Signature & Claim Verification: The gateway verifies the cryptographic signature against the configured algorithm and validates all
claims_to_verify. - Pipeline Termination or Forwarding: Successful validation passes the request to the upstream service. Failure terminates the pipeline with a
401response and logs the specific validation error. - Scope Isolation: Route-level attachment ensures validation logic applies only to protected endpoints, maintaining low latency for public-facing routes.
Specific Failure Modes & Troubleshooting
Common implementation failures stem from misaligned key_claim_name values, expired tokens exceeding clock_skew, or encoding mismatches. When Kong returns 401 Unauthorized, inspect the gateway logs for jwt: invalid signature or jwt: token expired errors.
Resolution Workflow:
- Base64 Secret Mismatch (HS256): Verify
secret_is_base64matches the actual encoding of the stored secret. If the credential is raw UTF-8, set tofalse. If pre-encoded, set totrue. - Missing Claim Mapping: Ensure the
key_claim_nameexists in the JWT payload and exactly matches a stored credential. A typo here triggers immediate401responses. - PEM Formatting (RS256): Verify the public key uses standard PEM formatting with correct line breaks (
-----BEGIN PUBLIC KEY-----). Strip trailing whitespace or convert to DER if signature verification fails. - Clock Skew Adjustment: Increase
clock_skewincrementally (e.g.,30to60) only when distributed nodes exhibit minor NTP drift. Do not use this parameter to mask legitimately expired tokens. - Log Isolation: Run
tail -f /usr/local/kong/logs/error.log | grep jwtduring test requests to pinpoint exact validation failure stages (signature, expiration, or claim mismatch).