JWT Authentication
Function Description
jwt-auth
plugin implements authentication and authorization based on JWT (JSON Web Tokens). It supports extracting JWT from HTTP request URL parameters, request headers, and Cookie fields, while verifying whether the Token has the necessary permissions to access the resource.
The difference between this plugin and the JWT authentication in Security Capability -> Authentication and Authorization
is that it additionally provides the capability of identifying the caller’s identity, supporting different JWT credentials for different callers.
Runtime Properties
Plugin execution phase: Authentication Phase
Plugin execution priority: 340
Configuration Fields
Note:
- In one rule, authentication configuration and authorization configuration cannot coexist.
- For requests authenticated through authentication and authorization, the request header will be augmented with an
X-Mse-Consumer
field to identify the caller’s name.
Authentication Configuration
Name | Data Type | Requirements | Default Value | Description |
---|---|---|---|---|
global_auth | bool | Optional (instance-level configuration only) | - | Can only be configured at the instance level. If set to true, it will globally enable the authentication mechanism; if set to false, it will only apply to the configured domain names and routes. If not configured, it will only globally take effect when no domain names and routes are configured (to be compatible with old user habits). |
consumers | array of object | Required | - | Configure service consumers for request authentication |
The configuration fields for each item in consumers
are as follows:
Name | Data Type | Requirements | Default Value | Description |
---|---|---|---|---|
name | string | Required | - | The name of the consumer |
jwks | string | Required | - | JSON format string specified by https://www.rfc-editor.org/rfc/rfc7517, consisting of the public key (or symmetric key) used to verify the JWT signature. |
issuer | string | Required | - | The issuer of the JWT, must match the iss field in the payload. |
claims_to_headers | array of object | Optional | - | Extract the specified fields from the JWT payload and set them in the specified request headers to forward to the backend. |
from_headers | array of object | Optional | {“name”:“Authorization”,“value_prefix”:“Bearer “} | Extract JWT from the specified request headers. |
from_params | array of string | Optional | access_token | Extract JWT from the specified URL parameters. |
from_cookies | array of string | Optional | - | Extract JWT from the specified cookies. |
clock_skew_seconds | number | Optional | 60 | The allowed clock skew when validating the exp and iat fields of the JWT, measured in seconds. |
keep_token | bool | Optional | true | Whether to retain the JWT when forwarding to the backend. |
Note:
- The default values will only be used when
from_headers
,from_params
, andfrom_cookies
are not all configured. The configuration fields for each item infrom_headers
are as follows: | Name | Data Type | Requirements | Default Value | Description | | --------------- | ---------------- | ------------ | ------------- | ----------------------------------------------- | |name
| string | Required | - | Extract JWT from the request header. | |value_prefix
| string | Required | - | Remove the prefix from the request header value, with the remaining part serving as the JWT. |
The configuration fields for each item in claims_to_headers
are as follows:
Name | Data Type | Requirements | Default Value | Description |
---|---|---|---|---|
claim | string | Required | - | The specified field in the JWT payload, must be a string or unsigned integer type. |
header | string | Required | - | The value of the field extracted from the payload is set to this request header and forwarded to the backend. |
override | bool | Optional | true | If true, existing headers with the same name will be overridden; if false, they will be appended. |
Authorization Configuration (Optional)
Name | Data Type | Requirements | Default Value | Description |
---|---|---|---|---|
allow | array of string | Optional (not instance-level configuration) | - | Can only be configured on fine-grained rules such as routes or domain names, allowing specified consumers to access matching requests for fine-grained permission control. |
Configuration Examples
Global Configuration for Authentication and Route-Level Authorization
Note: If a JWT matches multiple jwks
, the first matching consumer will be applied according to the configuration order.
Configure the plugin at the instance level as follows:
Configure the following for routes route-a
and route-b
:
Configure the following for domain names *.example.com
and test.com
:
Explanation:
The specified route-a
and route-b
refer to the routing names filled in when creating the gateway route. When these two routes are matched, the caller with the name consumer1 will be allowed access, while others will not be permitted.
The specified *.example.com
and test.com
are used to match the request domain names. When a matching domain name is found, the caller with the name consumer2 will be allowed access, while others will not.
Based on this configuration, the following requests will be allowed:
Suppose the following request matches the route-a. Setting the JWT in URL parameters
Setting the JWT in HTTP request headers
After successful authentication and authorization, the request’s header will include an X-Mse-Consumer
field, in this example with the value consumer1
, to identify the caller’s name.
The following requests will be denied: Request without JWT returns 401
Caller matching from the provided JWT has no access permission, returning 403
Enable at Gateway Instance Level
The following configuration will enable JWT Auth authentication at the instance level, requiring all requests to be authenticated before accessing.
Common Error Codes
HTTP Status Code | Error Message | Reason Description |
---|---|---|
401 | Jwt missing | The request header did not provide a JWT |
401 | Jwt expired | The JWT has expired |
401 | Jwt verification fails | JWT payload verification failed, such as mismatched iss |
403 | Access Denied | No permission to access the current route |
Detailed Description
1. Token-based Authentication
1.1 Introduction
Many open APIs need to identify the requester’s identity and determine whether the requested resource can be returned. A token is a mechanism used for identity verification. With this mechanism, applications do not need to retain user authentication information or session information on the server, allowing for stateless, distributed web application authorization, facilitating application scaling.
1.2 Process Description
The above image shows the entire business process sequence diagram for gateway authentication using JWT. Below, we will detail the steps indicated in the diagram:
- The client initiates an authentication request to the API gateway, generally carrying the terminal user’s username and password.
- The gateway forwards the request directly to the backend service.
- The backend service reads the verification information in the request (such as username and password) for validation. Upon successful verification, it generates a standard token using a private key and returns it to the gateway.
- The gateway returns a response containing the token to the client, who must cache this token locally.
- The client sends a business request to the API gateway, carrying the token in the request.
- The gateway validates the token using the user’s set public key. Upon successful validation, it forwards the request to the backend service.
- The backend service processes the business and responds.
- The gateway returns the business response to the client.
Throughout this process, the gateway utilizes the token authentication mechanism, enabling the user to authorize their API using their user system. Next, we will introduce the structured token used by the gateway for token authentication: JSON Web Token (JWT).
1.3 JWT
1.3.1 Introduction
JSON Web Token (JWT) is an open standard (RFC7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. JWT can be used as a stand-alone authentication token, capable of containing user identity, user roles, permissions, and other information, aiding in resource retrieval from resource servers and adding any additional claims required by business logic, particularly suitable for login scenarios for distributed sites.
1.3.2 JWT Structure
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
As shown above, JWT is a string composed of three parts:
- Header
- Payload
- Signature
Header
The header of the JWT carries two pieces of information:
- Token type, which is JWT
- Signing algorithm
The supported signing algorithms by the gateway are as follows:
The complete header looks like the following JSON:
Then the header is Base64 encoded (this encoding is symmetrically decodable), forming the first part.
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9
Payload
The payload is where valid information is stored. Its details are defined as follows:
Custom fields necessary for the user system can also be added, for example, the following example adds a nickname name
:
Then encode it in Base64 to obtain the second part of the JWT:
JTdCJTBBJTIwJTIwJTIyc3ViJTIyJTNBJTIwJTIyMTIzNDU2Nzg5MCUyMiUyQyUwQSUyMCUyMCUyMm5hbWUlMjIlM0ElMjAlMjJKb2huJTIwRG9lJTIyJTBBJTdE
Signature
This part requires the Base64-encoded Header and Base64-encoded Payload to be connected by a period, and then encrypted using the signing method declared in the Header (where $secret
represents the user’s private key) to form the third part of the JWT.
Connecting these three parts with a period creates a complete string that forms the JWT example at the beginning of section 1.3.2.
1.3.3 Validity Period
The gateway will validate the exp
field in the token. Once this field expires, the gateway will consider this token invalid and directly reject the request. The expiration time must be set.
1.3.4 Characteristics of JWT
- JWT is not encrypted by default; do not write secret data into JWT.
- JWT can be used for both authentication and information exchange. Effectively using JWT can reduce the number of queries to the database on the server.
- The biggest drawback of JWT is that since the server does not maintain session state, it cannot revoke a token during use or change the permissions of said token. In other words, once a JWT is issued, it will remain valid until expiration, unless the server implements additional logic.
- JWT itself contains authentication information, and once leaked, anyone can gain all permissions of that token. To minimize theft, the validity period of JWT should be set to be relatively short. For some critical permissions, users should be re-authenticated.
- To reduce theft, JWT should not be transmitted in plain text over HTTP but should use HTTPS for transmission.
2. How User Systems Apply the JWT Plugin to Protect APIs
2.1 Generating a Pair of JWKs (JSON Web Keys)
Method 1: Online Generation:
Users can generate the private and public keys used for token generation and verification at this site https://mkjwk.org. The private key is used by the authorization service to issue JWTs, and the public key is configured into the JWT plugin for the gateway to verify requests. Pay attention to the jwks format configuration used by the gateway. In the image below, the Public Key should be placed into the keys structure, for example: {"keys":[{"kty":"RSA","e":"AQAB",...}]}
Method 2: Local Generation:
This article demonstrates using Java; users of other languages can find related tools to generate key pairs. Create a new Maven project and include the following dependency:
Use the following code to generate a pair of RSA keys:
2.2 Using the Private Key in JWK to Implement the Token Issuance Authentication Service
You will need to use the Keypair JSON string (the first inside the three boxes) generated online in section 2.1 or the locally generated privateKeyString JSON string as the private key to issue tokens for authorizing trusted users to access protected APIs. The specific implementation can refer to the example below. The form of issuing tokens to customers can be determined by the user based on the specific business scenario; it can be deployed in the production environment, configured to be a normal API that visitors can access through username and password, or tokens can be generated locally and directly copied for specified users to use.