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.
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.
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>.
defparse_header(authz_header): parts = authz_header.split(" ")iflen(parts)!=2:return scheme = parts[0]if scheme.lower()!="bearer":returnreturn parts[1]
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.
from flask import requestimport jwtfrom jwt import PyJWKClient@app.route("/hello")defhello(): authz_header = request.headers.get("Authorization")ifnot authz_header:return{"message":"authz header not found"}# get jwt token from Authorization header token =parse_header(authz_header)if token:try:# fetch jwks_uri from the Authgear Discovery Endpoint jwks_uri =fetch_jwks_uri(base_address)# Reuse PyJWKClient for better performance jwks_client =PyJWKClient(jwks_uri) signing_key = jwks_client.get_signing_key_from_jwt(token) user_data = jwt.decode( token, signing_key.key, algorithms=["RS256"], audience=base_address, options={"verify_exp": True}, )return{"message":"Hello!","user_data": user_data}except:return{"message":"JWT decode failed"}else:return{"message":"no token"}
Step 1: Install dependencies
npminstall--saveaxiosjwks-rsajsonwebtoken
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).
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:
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.