source
field rather than configuring jwksURIs
) in step 3. You’ll need to generate a JWT for the request, but otherwise everything stays the same.Now back to the tutorial…your-tenant.auth0.com
)https://your-tenant.auth0.com/.well-known/jwks.json
JWT Auth Policy
scope
or permissions
)RSA Public Key
sub
pol
JWT Auth Policy
(the policy you created previously)10
)header
Authorization
Enabled
Authorization
header; Tyk will validate the JWT using the JWKS that it retrieves from your JWKS URIkid
(Key ID) from the token headerkid
policyFieldName
)defaultPolicy
will be usedHTTP 403 Forbidden
response with Key not authorized: no matching policy
. Tyk Gateway will also log the error: Policy ID found is invalid!
.
components.securitySchemes
object using the type: http
, scheme: bearer
and bearerFormat: jwt
:
Authorization
header, but Tyk is configurable, via the Tyk Vendor Extension, to support custom header keys and credential passing via query parameter or cooke.
authentication.stripAuthorizationData
field (Tyk Classic: strip_auth_data
).
With the JWT method selected, you’ll need to configure Tyk to handle the specific configuration of JSON Web Tokens that clients will be providing. All of the JWT specific configuration is performed within the authentication.jwt object in the Tyk Vendor Extension.
securitySchemes
mechanism that lets you define where the JWT should be located, for example in the request header. However, in practice, different clients may supply the token in different locations, such as a query parameter.
While OAS does not support this natively, the Tyk Vendor Extension does this by allowing configuration of alternative locations in the JWT entry in server.authentication.securitySchemes
. Building on the previous example, we can add optional query and cookie locations as follows:
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 |
server.authentication.securitySchemes.<jwtAuthScheme>.source
. 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 will configure the JWT authentication middleware to use the secret located at consul://secrets/jwt-secret
to validate the signature of incoming JWTs. Note that the external KV store reference has been base64 encoded and then stored in source
:
<jwtAuthScheme>.source
.
For example, the following fragment will configure the JWT authentication middleware to retrieve the JWKS from https://your-tenant.auth0.com/.well-known/jwks.json
when validating the signature of incoming JWTs. Note that the JWKS endpoint has been base64 encoded and then stored in source
:
<jwtAuthScheme>.jwksURIs
array. Note that these URIs are not base64 encoded in the API definition and so are human-readable. Tyk will retrieve the JSON Web Key Sets from each of these endpoints and these will be used to attempt validation of the received JWT.
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:
jwksURIs
array are not supported by Tyk Classic APIs.<jwtAuthScheme>.source
and <jwtAuthScheme>.jwksURIs
are configured, the latter will take precedence.IssueAt
, ExpireAt
and NotBefore
.
If, on receipt of the JWT, Tyk determines that the token is not valid then it will reject the request with the “Token is not valid yet” error.
Due to the nature of distributed systems it is expected that, despite best efforts, you can end up in a situation with clock skew between the party issuing the JWT (your IdP) and the validating party (Tyk). This might occur due to the clock on the Tyk server being behind or ahead of the clock on the Identity Provider server even with all servers synchronised from the same NTP server.
You can optionally configure a maximum permissable difference between the timestamp in the token’s validity claims and Tyk’s clock, to allow for these scenarios. Each can be independently configured or omitted from the API configuration as required. The permissable skews are configured in seconds.
kid
) in the JWT (unless skipKid
is enabled)identityBaseField
(which allows API administrators to designate any JWT claim as the identity source (e.g., user_id, email, etc.).sub
(which is the JWT specification’s recommended claim for subject identity)Alias
fieldkeyID
fieldTykJWTSessionID
skipKid
has been set to true
, so Tyk checks the identityBaseField
and determines that the value in the custom claim user_id
within the JWT should be used as the identity for the session object.
pol
. You must instruct Tyk where to look for the policy claim by configuring the policyFieldName
field in the API definition.
In this example, Tyk has been configured to check the pol
claim in the JWT to find the Policy Ids for the policies to be applied to the session object:
defaultPolicies
field in the API definition, which accepts a list of policy Ids.
scopes.claimName
to instruct Tyk where to look for the scopes and then you declare a mapping of scopes to policies within the scopes.scopeToPolicyMapping
object.
accessScopes
claim within the incoming JWT and apply the appropriate policy if that claim contains the value read:users
or write:users
. If neither scope is declared in the claim, or the claim is missing, then the default policy will be applied.
Multiple scopes can be declared within a JWT by setting the value of the claim in any of four configurations:
"permissions": "read:users write:users"
"permissions": ["read:users", "write:users"]
"permissions": { "access": "read:users write:users" }
"permissions": { "access": ["read:users", "write:users"] }
claimName
so, for the first two examples above, the claimName
should be set to permissions
whilst for the the two nested examples you would use permissions.access
.
This example of a fragment of a JWT, if provided to an API with the configuration above, will cause Tyk to apply both policies to the session object:
policyFieldName
scopeToPolicyMapping
based upon scopes in the JWTdefaultPolicies
per_api
to true
in each policy. This ensures that the policy’s settings only apply to the specific APIs listed in that policy, not to all APIs globally.API ID
in multiple policies with conflicting settings. Instead, create distinct policies with complementary settings that can be safely combined.use_jwt
option. Tyk Classic APIs do not natively support multiple JWKS endpoints, though a custom authentication plugin could be used to implement this functionality.
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJlbWFpbCI6ImhlbGxvQHdvcmxkLmNvbSJ9
EwIaRgq4go4R2M2z7AADywZ2ToxG4gDMoG4SQ1X3GJ0
/token
endpoint is configured on Tyk from which the client should request the access token