How to handle JWT in PHP
Everything you need to know to implement and validate JWTs securely in PHP: from signing to verifying with JWKS, with code examples and best practices for both vanilla PHP and Laravel.
PHP powers a vast portion of the web, and the JWT story in PHP is refreshingly simple: one library dominates the ecosystem, it builds on PHP's native OpenSSL extension, and it integrates cleanly with the PSR standards that modern PHP applications are built on. Whether you are writing a Laravel API, a Symfony service, or a framework-free microservice, the fundamentals are the same.
This guide walks through everything you need to know to safely consume, validate, and work with JWTs in PHP, including HS256 and RS256 verification, JWKS with caching, Laravel middleware integration, the CachedKeySet class for key rotation, and common pitfalls. Let's dive right in.
!!Need to inspect a JWT? Use the WorkOS JWT Debugger to decode and inspect your JWTs directly in the browser. It's a quick way to verify your token's iss, aud, sub, and other claims while debugging.!!
JWT 101
A JSON Web Token is a compact, URL-safe token format used to securely transmit information between systems. At a high level, a JWT lets one system make a signed statement about a user or service, and lets another system verify that statement without needing to look anything up in a database.
They are typically used to indicate a user's identity and/or assert permissions and roles.
A JWT is composed of three parts, each Base64URL-encoded and separated by dots:
Header
The header contains metadata about the token, most importantly the signing algorithm used to create the signature (e.g., HMAC, RSA, or ECDSA). This tells the verifier how the token was signed and how it should be validated.
A typical header before encoding:
In this example, alg is set to RS256, representing RSA with SHA-256, and typ identifies this as a JWT.
Payload
The payload contains the actual data the token encodes. These data points are called claims.
Claims are pieces of information about the subject of the token and additional context about how it should be used. Some claims are registered and standardized, like iss, sub, aud, and exp (for the full list check the JWT claims registry). Others are custom and application-specific.
Example payload:
It is important to note that the payload is not encrypted. Anyone who has the token can decode it and read the claims. Do not put passwords, secrets, or high-risk PII in JWT payloads.
Signature
The signature ensures the token's integrity and confirms that it was issued by a trusted source. It is created by hashing the Base64URL-encoded header and payload with a secret key (for symmetric algorithms like HS256) or a private key (for asymmetric algorithms like RS256). The resulting hash is then Base64URL-encoded and appended to the token.
When a JWT is received, the verifier recomputes the signature using the appropriate key and compares it to the signature included in the token. If they do not match, the token has been tampered with and must be rejected.
JWTs are protected via JSON Web Signature (JWS). JWS is used to share data between parties when confidentiality is not required, because the claims within a JWS can be read by anyone (they are simply Base64URL-encoded). The signature provides authentication, not encryption. Some of the cryptographic algorithms JWS uses are HMAC, RSA, and ECDSA.
JWT library for PHP
The firebase/php-jwt library is the standard for handling JWTs in PHP. With over 460 million Composer installs, nearly 10,000 GitHub stars, and 2,300+ dependent packages, it is the clear choice. It is actively maintained (v7.0.5 released April 2026), requires PHP 8.0+, and provides support for HMAC, RSA, ECDSA, and EdDSA signing algorithms through PHP's native OpenSSL extension.
The library also provides built-in JWKS support through two classes: JWK for parsing key sets from JSON, and CachedKeySet for fetching and caching JWKS from a remote endpoint with automatic key rotation and rate limiting.
Install it with Composer:
For JWKS endpoint fetching with caching, you will also need a PSR-7/PSR-17 HTTP client and a PSR-6 cache implementation. If you are using Laravel, these are already available. For vanilla PHP:
Generating your keys
First, you need a set of cryptographic keys to sign your tokens.
In this tutorial, we will be using RS256. This asymmetric algorithm requires two keys: a private key to sign the token and a public key to verify it. If you already have them, move along to the next section.
!!Asymmetric algorithms use a pair of public and private keys to sign and verify the tokens. They are more secure, scalable, and better for distributed systems but also more resource-intensive and complex. For more on the various algorithms see Which algorithm should you use to sign JWTs?!!
There are many ways to generate your keys. You could generate them using OpenSSL and save them as raw PEM files that your code would read:
However, this is not a best practice. Instead, you should use JSON Web Key Sets (JWKS), especially in distributed or cloud environments.
!!JWKS vs PEM: JWKS simplifies key rotation by allowing services to fetch the latest keys from a central endpoint, making updates easier and reducing the risk of errors. PEM files require manual distribution and updates, which can be cumbersome in large systems. JWKS centralizes key distribution, ensuring that all services or clients always have the correct keys without constant manual updates.!!
If you are using a third-party identity provider (like WorkOS), they automatically generate and expose a JWKS endpoint for you. This allows clients to dynamically fetch the public keys needed for JWT verification without you having to manage the keys manually. WorkOS offers a public JWKS endpoint:
The response looks like this:
Clients and APIs can use this endpoint to retrieve the public keys needed to validate JWTs signed by WorkOS. Key rotation, expiration, and distribution are handled automatically by the provider.
If you are not using a third-party identity provider and want to create and manage your own JWKS in Java, you will need to:
- Generate a key pair (public and private keys). Java's
java.security.KeyPairGeneratorprovides this natively through the JCA. - Create a JWKS endpoint. Expose the public keys at a well-known URL (
/.well-known/jwks.json) that clients and services can use to validate JWTs. - Handle key rotation and management. Periodically generate new key pairs and update the JWKS. Use a key identifier (
kid) to distinguish between active and retired keys. - Secure your private keys. Never expose private keys through your API or any public endpoint. Store them in a secure EKM like WorkOS Vault, an HSM, or at minimum an encrypted file with restricted access.
!!If you need something fast for a proof-of-concept, you can use a tool like mkjwk.org to generate a JWK.!!
Generating an RSA key pair in PHP
PHP's OpenSSL extension provides key generation natively:
Store your private key outside the web root with restricted file permissions (e.g., chmod 600). In Laravel, the standard approach is to store PEM content in your .env file or use php artisan env:encrypt for encrypted environment files. We will cover this in the Laravel section.
PHP and newlines in PEM keys
This catches many PHP developers off guard. If you store a PEM key in an environment variable, the \n characters must be interpreted as actual newlines. In PHP, this means using double quotes, not single quotes:
This is one of the most frequently reported issues on the firebase/php-jwt GitHub repository. If your key looks correct but verification fails, check the newlines first.
Creating a JWT with RS256
Once you have your RSA keys, you can create and sign a token using the private key:
The JWT::encode() method takes the payload, the signing key, the algorithm, an optional key ID (fourth parameter, or null to pass it in headers), and optional header claims. The kid in the header is important for key rotation, which we will cover later.
Creating a JWT with HS256
For simpler use cases where a shared secret is acceptable:
For HS256, make sure your secret key is long enough. A key shorter than 256 bits (32 bytes) is insecure.
Sending the token as a Bearer token
Once the client has the JWT, it sends it in the Authorization header as a Bearer token. The Bearer prefix tells the API that whoever bears this token can use it:
Or with Guzzle (which you likely already have if you are using JWKS):
On the server side, you extract the token from this header before validating it.
Adding standard and custom claims
JWT claims fall into two categories: standard and custom.
Standard claims
Common registered claims include:
sub(subject): what the token is about, typically the user's unique identifier.iss(issuer): who issued the token.aud(audience): who the token is intended for.exp(expiration time): when the token expires, in seconds since the Unix epoch.iat(issued at): when the token was issued.nbf(not before): when the token becomes valid.jti(JWT ID): a unique identifier for the token, useful for revocation.
In PHP, these are simply keys in the payload array:
Custom claims
Custom claims are application-specific data added alongside the standard claims:
PHP arrays can hold any JSON-serializable type (strings, numbers, booleans, nested arrays and objects), so you can structure custom claims however you need. Be careful not to include sensitive information, since JWT payloads are encoded, not encrypted.
Decoding a JWT
Decoding a JWT without verifying it can be useful for debugging and logging, but it should never be used for authorization decisions.
The firebase/php-jwt library deliberately does not provide a "decode without verification" method. The README explicitly states that decoding headers without verification is not recommended. If you need to inspect a token for debugging, you can do so manually:
The library takes this stance because any value from an unverified token could have been tampered with. Do not use unverified claims for authorization, routing, or database lookups.
About the kid claim
The kid (key ID) appears in the JWT header, not the payload:
It tells your application which public key (from a set of keys) should be used to verify the signature. This is essential when your authentication provider uses key rotation, publishing multiple public keys at a JWKS endpoint and including kid in the JWT header to indicate which key was used to sign it.
When your app receives a JWT, it extracts the kid from the header, looks up the matching public key in the JWKS, and uses that key to verify the signature. The firebase/php-jwt library handles this automatically through JWK::parseKeySet() and CachedKeySet, which we will use in the next section.
Verifying a JWT
Verification ensures three things: the signature is valid, the token has not expired, and the claims match your expectations.
Verifying with a local public key
If you have the public key available locally:
The Key class is how firebase/php-jwt enforces algorithm specificity. By wrapping the key with new Key($publicKey, 'RS256'), you tell the library to only accept RS256-signed tokens verified with that key. This prevents algorithm confusion attacks, where an attacker could send a token signed with HS256 using your public key as the shared secret and your verifier might accept it.
The return type is stdClass
JWT::decode() returns a PHP stdClass object, not an associative array. Access claims with arrow syntax ($decoded->sub), not array syntax ($decoded['sub']). If you prefer arrays:
Verifying with a JWKS endpoint
This is the recommended approach for production. Your identity provider publishes its public keys at a JWKS URL, and the CachedKeySet class fetches and caches them automatically:
The CachedKeySet class handles several important concerns:
- Key caching. The JWKS response is fetched once and stored in the PSR-6 cache. Subsequent verifications use the cached keys without making HTTP requests.
- Automatic key rotation. If a token arrives with a
kidthat is not in the cache,CachedKeySetre-fetches the JWKS from the endpoint. This handles key rotation transparently. - Rate limiting. When the sixth parameter is
true, the class limits outbound requests to 10 per second, preventing an attacker from flooding your server with tokens containing unknownkidvalues and forcing excessive JWKS fetches.
Using CachedKeySet in Laravel
In Laravel, you can use the framework's built-in cache and HTTP client instead of installing Guzzle and phpfastcache separately. Laravel's cache implements the PSR-6 interface through a wrapper:
This uses whatever cache driver you have configured in Laravel (Redis, Memcached, file, etc.) and shares the cache with the rest of your application.
Adding leeway for clock skew
If your token issuer and your verifier run on different servers, their clocks may be slightly out of sync. The library provides a static property to handle this:
Set this before calling decode(). Thirty seconds is a reasonable default. Do not set it higher than a few minutes.
Validating issuer and audience
The firebase/php-jwt library validates exp, nbf, and iat automatically but does not validate iss or aud for you. You need to check these yourself after decoding:
This is a common oversight. The library verifies the cryptographic signature and time-based claims, but application-level claims like iss and aud are your responsibility.
Exception handling
The library throws specific exception types that you can catch individually:
All JWT-related exceptions extend UnexpectedValueException, so you can catch that as a blanket handler. LogicException covers environmental issues like missing extensions or malformed keys.
Handling custom claims
Once verified, custom claims are available as properties on the returned stdClass object:
For nested claims:
Remember that JWT::decode returns objects, not arrays. Nested structures like feature_flags are also stdClass objects. Use arrow syntax or cast to arrays if you prefer.
Integrating with Laravel
Laravel is the most popular PHP framework, and JWT authentication in Laravel typically follows one of two patterns: a dedicated middleware or a custom authentication guard.
Middleware approach
For API-only applications, a middleware is the simplest integration:
Register the middleware in your application:
Apply it to routes:
Service provider approach
For cleaner architecture, register the CachedKeySet as a singleton in a service provider so you do not create a new instance on every request:
Then inject it into your middleware:
Configuration
Store your JWT configuration in config/services.php:
And in your .env:
JWT best practices (PHP edition)
JWTs are simple in structure, but security lives in the details you enforce. Here are the practices that matter most in production Ruby applications.
- Always verify the signature. Do not trust a token just because it decodes cleanly. Only use claims for authorization decisions after verification succeeds. With the
jwtgem, always passtrueas the third argument toJWT.decode, or callverify!onJWT::EncodedToken. - Enforce the expected algorithm. Always pass
algorithm: 'RS256'(or whichever algorithm you expect) in the decode options. "Accept whatever the header says" is how algorithm confusion attacks happen. Thejwtgem's README explicitly warns about this. - Validate critical standard claims. At minimum, validate
exp(expiration),iss(issuer), andaud(audience). Setverify_iss: true,verify_aud: true, andverify_expiration: truein your decode options. If you deal with clock drift between systems, set a smallleeway(30 to 60 seconds) rather than loosening validation. - Use a JWKS endpoint when possible. If your tokens are issued by an identity provider, verify against their JWKS so you can automatically select the right public key by
kid. Use the lambda-basedjwksloader for caching and automatic refresh. - Plan for key rotation. If you manage your own keys, publish new keys at your JWKS endpoint before you start signing with them, keep old keys available until tokens signed with them expire, and use
kidto distinguish active from retired keys. Thejwtgem'sjwkslambda handles the verifier side of rotation automatically by re-fetching on unknownkid. - Enforce Bearer token format. Require tokens in the
Authorizationheader in this exact format:Authorization: Bearer <jwt>. Treat tokens in query parameters as a problem, because they leak into logs, browser history, and referrer headers. - Keep access tokens short-lived. Short
expvalues (5 to 15 minutes) reduce the blast radius of a leaked token. If you need long sessions, use refresh tokens and rotate them. - Handle verification errors explicitly. The
jwtgem raises specific exception classes:JWT::ExpiredSignature,JWT::InvalidIssuerError,JWT::InvalidAudError,JWT::IncorrectAlgorithm,JWT::VerificationError, andJWT::DecodeErroras a catch-all. Map these to clean HTTP responses: 401 for missing, invalid, or expired tokens; 403 for valid tokens that lack required permissions. - Use HTTPS everywhere. JWTs are bearer credentials. If someone can intercept the request, they can replay the token.
- Centralize JWT logic. Put verification in a Rack middleware or a Rails controller concern so every protected endpoint enforces the same checks. Do not scatter partial verification logic across individual actions.
- Log failures carefully. Log high-level context (like
kid,iss, and the reason verification failed) and never log full tokens or entire payloads. In Rails, useRails.loggerwith structured tags. - Store secrets in Rails credentials. Do not put private keys or JWT secrets in environment variables if you can avoid it. Rails encrypted credentials (
rails credentials:edit) are the idiomatic approach for managing secrets in Rails 7 and 8. - Test with bad tokens. Make sure your test suite covers expired tokens, tokens with wrong
issoraud, tokens signed with the wrong key, tokens with tampered payloads, missing required claims, and wrong algorithm ornoneedge cases. Thejwtgem makes it easy to generate test tokens with specific properties.
Let WorkOS handle the heavy lifting
While handling JWTs with firebase/php-jwt is often necessary at the API layer, it is worth stepping back and looking at the bigger picture: how those tokens are issued in the first place.
If you are building authentication flows, especially ones that involve Single Sign-On (SSO), SCIM provisioning, or multi-tenant identity, there is a lot more to solve than signing and verifying tokens. You need to support different identity providers, manage users and directories, rotate keys safely, and issue tokens that downstream services can trust.
WorkOS provides a modern API for enterprise-ready authentication features, letting you integrate SSO (SAML, OIDC, and more), manage users and directories, and issue secure tokens without building and maintaining a full auth stack from scratch. WorkOS has both a PHP SDK and a dedicated Laravel SDK that handle the OAuth flow, token exchange, and user management. It is especially useful if you need to support enterprise customers or want to offer a "Login with your company" experience. And it is free for up to 1,000,000 monthly active users.
If you are already running a Laravel app, you may also want to check out the existing guide on building authentication in Laravel, which covers the full authentication story beyond just JWTs.