Validate JWT in your backend
Authenticate the incoming HTTP requests by validating JWT in your application server
In this section, we will go through how to decode the JWT token to obtain the currently logged-in user.
Before we start, make sure the option Issue JWT as access token is enabled in your Application settings in the Portal.

With the Issue JWT as access token option turned on in your application, Authgear will issue JWT as access tokens. The incoming HTTP requests should include the access token in their Authorization headers. Without setting the reverse proxy, your backend server can use your Authgear JWKS to verify the request and decode user information from the JWT access token.
Payload of the JWT access token
See the claims in the access token in this reference: JWT Access Token. Learn more about Add custom fields to a JWT Access Token or ID Token for adding claims into the JWT.
Find the JSON Web Key Sets (JWKS) endpoint
This Discovery endpoint serves as a JSON document containing the OpenID Connect configuration of your app. It includes the authorization endpoint, the token endpoint, and the JWKS endpoint.
https://<YOUR_AUTHGEAR_ENDPOINT>/.well-known/openid-configuration
The JSON Web Key Sets (JWKS) endpoint can be found in jwks_uri in the configuration.
OpenID Connect Configuration JSON Example
Here is an example of how it looks.
Decode user from an access token
Follow this step-by-step example to verify and decode the JWT token.
Step 1: Install packages
Step 2: Find the JSON Web Key Sets (JWKS) endpoint
Define a function to find the JWKS endpoint from the OpenID Connect configuration. Use your Authgear endpoint as the base_address
Step 3: Get the JWT token from the Authorization header
Define a function to extract the access token from the Authorization header in the incoming request. It should look like Authorization: Bearer <access_token>.
Step 4: Verify and decode the JWT token
Here we show an example of using the Flask web framework to guard a path. You may need to adjust some of the codes to suit your technologies.
Step 1: Install dependencies
Step 2: Find the JWKS Endpoint
Use the following method to get the JWKS URI (you'll need to URI to extract the public signing key from a JWT).
Step 3: Extract JWT from Request Header
Use the following code to extract only the token part from a Bearer [token] authorization header in your Express app:
Step 4: Decode Access Token
Next, decode the access token so that you can extract the JWT kid from the result. You'll need this `kid to get the public signing key. Use the following code to decode the JWT:
Step 5: Get JWT Signing Keys and Verify the JWT
Use the following code to extract the JWT public keys then verify the JWT using the keys:
Here's what your Express app should look like after putting the code in all the steps together:
Use your Authgear endpoint as base_address
The following example uses Spring Boot.
Step 1: Install dependencies
Add the following dependencies to your build.gradle file:
Then add the following imports to the top of your controller file:
Step 2: Get JWKS Endpoint
Implement the following method to fetch the JWKS URI:
Step 3: Get Signing Key
Get the signing key from the JWK using the following method:
Step 4: Validate JWT
To demonstrate how to validate a JWT, we'll implement a validateJWT endpoint in a Spring Boot application. The endpoint will read access tokens from the bearer authorization header.
It will call the fetchJwksUri() and getSigningKeyFromJwks() from steps 1 and 2 to get the JWK URI and signing key required to parse the JWT.
Step 1: Install Packages
First, install the dependencies required by running these com
Step 2: Find the JWKS Endpoint
Create a function that finds the JWKS endpoint from your Authgear application endpoint using the following code:
Step 3: Get Signing Key
Add the following code to your application to get the JWT signing key:
Step 4: Extract the JWT From the Request Header
To extract the access token from the HTTP request use the following code:
Step 5: Validate and Decode JWT
Finally, decode the JWT signing key.
Step 1: Install NuGet packages
then add these imports to the top of your program.cs file:
Step 2: Configure JWT Authentication
This tells ASP.NET Core to use JWT Bearer tokens for authentication
Step 3: Add authorization
Step 4: Configure middleware pipeline
Order is important! Authentication must come before Authorization
Step 5: Create a protected endpoint
The following example uses the Minimal API model
For Controller-based APIs, simply add [Authorize] to your controller class or individual action methods to protect them
Check the validity of JWT
The auth_time claim in an OIDC ID token represents the time when the user authentication occurred. Extract the auth_time claim from the token, which should represent the time of the original authentication in seconds. If the difference between the current time and auth_time exceeds your threshold (for example, 5 minutes), initiate the re-authentication process.
See an example of how to verify the signature of the ID token, and then validate the claims auth_time inside here.
Decode user from cookies
Validating JWT in your application server is currently only available for Token-based authentication.
Last updated
Was this helpful?