Skip to main content

JWT Authentication

The JWT middleware verifies that a valid JWT token is provided in the Authorization header (Authorization: Bearer <JWT>). If the token can't be passed as an Authorization header, it can be given as form data or as a query parameter. See the tokenKey option for more information.

With no specific configuration, a JWT middleware only validates the signature of a JWT and checks the nbf, exp and iat standard claims (if they are present).
Custom claim validation can be configured with Custom Claims Validation.


Configuration Example

Validating claims and forwarding headers
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: test-jwt
spec:
plugin:
jwt:
signingSecret: my-secret
forwardHeaders:
Group: grp
Expires-At: exp
claims: Equals(`grp`, `admin`)

Configuration Options

FieldDescriptionDefaultRequired
signingSecretDefines the secret used for signing the JWT certificates.
It is then used by the middleware to verify incoming requests.
At least one of signingSecret, publicKey, jwksFile or jwksUrl options must be set. (More information here)
""No
signingSecretBase64EncodedDefines whether the signingSecret is base64-encoded.
If set to true, the signingSecret is base64-decoded before being used.
falseNo
publicKeyDefines the public key used to verify secret signature in incoming requests.
In that case, users should sign their token using a private key corresponding to the configured public key.
At least one of signingSecret, publicKey, jwksFile or jwksUrl options must be set.
""No
jwksFileDefines a set of JWK to be used to verify the signature of JWTs.
The option can either be a path to a file mounted on the API Gateway or directly the content of a JWK set file.
At least one of signingSecret, publicKey, jwksFile or jwksUrl options must be set. (More information here)
""No
jwksUrlDefines the URL of the host serving a JWK set.
The keys are cached if the HTTP Cache Control allows for caching.
At least one of signingSecret, publicKey, jwksFile or jwksUrl options must be set.
(More information here)
""No
forwardAuthorizationDefines whether the authorization header will be forwarded or stripped from a request after it has been approved by the middleware.falseNo
tokenKeyDefines the name of the query and form data parameter used for passing the JWT, for applications that can't pass it in the Authorization header.
The middleware always looks in the Authorization header first, even with this option enabled.
This option should only be enabled if the JWT cannot be passed as an Authorization header, as it is not recommended by the RFC.
""No
claimsDefines the claims to validate in order to authorize the request.
The claims option can only be used with JWT-formatted token. (More information here)
""No
usernameClaimDefines the claim that will be evaluated to populate the clientusername in the access logs.
The usernameClaim option can only be used with JWT-formatted token.
""No
forwardHeadersDefines the HTTP headers to add to requests and populates them with values extracted from the access token claims returned by the authorization server.
Claims to be forwarded that are not found in the JWT result in empty headers.
The forwardHeaders option can only be used with JWT-formatted token.
[]No
clientConfig.tls.caPEM-encoded certificate bundle or a URN referencing a secret containing the certificate bundle used to establish a TLS connection with the authorization server (More information here)""No
clientConfig.tls.certPEM-encoded certificate or a URN referencing a secret containing the certificate used to establish a TLS connection with the Vault server (More information here)""No
clientConfig.tls.keyPEM-encoded key or a URN referencing a secret containing the key used to establish a TLS connection with the Vault server. (More information here)""No
clientConfig.tls.insecureSkipVerifyDisables TLS certificate verification when communicating with the authorization server.
Useful for testing purposes but strongly discouraged for production. (More information here)
""No
clientConfig.timeoutSecondsDefines the time before giving up requests to the authorization server.5No
clientConfig.maxRetriesDefines the number of retries for requests to authorization server that fail.3No

claims

Syntax

The following functions are supported in claims:

FunctionDescriptionExample
EqualsValidates the equality of the value in key with value.Equals(`grp`, `admin`)
PrefixValidates the value in key has the prefix of value.Prefix(`referrer`, `http://example.com\`)
Contains (string)Validates the value in key contains value.Contains(`referrer`, `/foo/`)
Contains (array)Validates the key array contains the value.Contains(`areas`, `home`)
SplitContainsValidates the value in key contains the value once split by the separator.SplitContains(`scope`, ` `, `writer`)
OneOfValidates the key array contains one of the values.OneOf(`areas`, `office`, `lab`)

All functions can be joined by boolean operands. The supported operands are:

OperandDescriptionExample
&&Compares two functions and returns true only if both evaluate to true.Equals(`grp`, `admin`) && Equals(`active`, `true`)
||Compares two functions and returns true if either evaluate to true.Equals(`grp`, `admin`) || Equals(`active`, `true`)
!Returns false if the function is true, otherwise returns true.!Equals(`grp`, `testers`)

All examples will return true for the following data structure:

JSON
{
"active": true,
"grp": "admin",
"scope": "reader writer deploy",
"referrer": "http://example.com/foo/bar",
"areas": [
"office",
"home"
]
}

Nested Claims

Nested claims are supported by using a . between keys. For example:

Key
user.name
Claims
{
"active": true,
"grp": "admin",
"scope": "reader writer deploy",
"referrer": "http://example.com/foo/bar",
"areas": [
"office",
"home"
],
"user" {
"name": "John Snow",
"status": "undead"
}
}
Result
John Snow
Handling keys that contain a '.'

If the key contains a dot, the dot can be escaped using \.

Handling a key that contains a ''

If the key contains a \, it needs to be doubled \\.

clientConfig

Defines the configuration used to connect the API Gateway to a Third Party Software such as an Identity Provider.

clientConfig.tls

Storing secret values in Kubernetes secrets

When configuring the tls.ca, tls.cert, tls.key, it is possible to reference Kubernetes secrets defined in the same namespace as the Middleware.
The reference to a Kubernetes secret takes the form of a URN:

urn:k8s:secret:[name]:[valueKey]
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: test-jwt
spec:
plugin:
jwt:
clientConfig:
tls:
ca: "urn:k8s:secret:tls:ca"
cert: "urn:k8s:secret:tls:cert"
key: "urn:k8s:secret:tls:key"
insecureSkipVerify: true

jwksFile

JWT Header Key ID

If the JWT header contains a kid header, the middleware expects to find a JWK. If a JWK cannot be found, it returns a 401 Unauthorized error.

jwksUrl

JWT Header Key ID

If the JWT header contains a kid header, the middleware expects to find a JWK. If a JWK cannot be found, it returns a 401 Unauthorized error.

JWT Issuer Claim

If jwksUrl is set to a path and the iss property is missing in the JWT it's trying to verify, the middleware returns a 401 Unauthorized error.

signingSecret

Storing secret values in Kubernetes secrets

When configuring the signingSecret, it is possible to reference a Kubernetes secret defined in the same namespace as the Middleware. The reference to a Kubernetes secret takes the form of a URN:

urn:k8s:secret:[name]:[valueKey]