๐Ÿ˜PHP

Integrating JWT Validation in Your Application

This guide walks you through integrating JWT validation within your application, ensuring secure communication with our Authopia services.

Prerequisites

Ensure you have guzzlehttp/guzzle and web-token/jwt-framework installed in your PHP project. If not, you can install them using Composer:

composer require guzzlehttp/guzzle web-token/jwt-framework

Steps for JWT Validation

1. Obtain Your Customer and Project ID

After successful login into the Admin Panel, note down your customer-id and project-id. These are essential for fetching the JWKS and validating tokens specific to your project.

2. Deserialize the JWT

Extract the JWT sent to your backend and deserialize it to access the headers and payload. This step is crucial for subsequent validation checks.

use Jose\Component\Signature\Serializer\CompactSerializer;

$serializer = new CompactSerializer(); 
$jws = $serializer->unserialize($jwtToken); 

3. Perform Header Checks

Ensure the JWT's header contains the necessary parameters like alg and kid for secure processing.

4. Validate Claims

Use the ClaimCheckerManager to validate standard claims such as issued at (iat), not before (nbf), expiration time (exp), audience (aud), and issuer (iss). Adjust the audience and issuer values according to your application's requirements.

5. Fetch the JWKS and Match the Correct Key

Retrieve the JWKS from Authopia and select the correct key using the kid found in the JWT's header.

Note: To enhance performance and reduce the load on the Authopia servers, implement a caching mechanism to store the JWKS. This way, you don't have to fetch the JWKS for every authentication attempt, only when the cache expires or when the key indicated by the JWT's kid is not found in the cache.

6. Verify the JWT's Signature

With the correct key in hand, verify the JWT's signature to ensure it was indeed issued by Authopia and has not been tampered with.

Verification Outcome

Based on the verification result, take the appropriate action within your application.

Full Example Code

Here's the complete code snippet for reference:

Follow these steps to securely validate JWTs in your application. Should you encounter any issues or have further questions, feel free to reach out to our support team.

Last updated