By NHI Mgmt Group Editorial TeamPublished 2026-05-11Domain: Best PracticesSource: WorkOS

TL;DR: Secure JWT handling in PHP depends on signature verification, strict claim validation, and JWKS-backed key rotation, with firebase/php-jwt providing the standard library path for HS256, RS256, and cached remote keys according to WorkOS. The governance point is simple: unverified tokens are bearer credentials, not trusted identity assertions, and access logic must never depend on payloads before verification.


At a glance

What this is: This is a practical guide to implementing and validating JWTs securely in PHP, with emphasis on signature verification, claim checks, and JWKS-based key rotation.

Why it matters: It matters because JWT handling sits at the boundary between authentication, API access, and identity governance, so weak verification can undermine both human IAM and machine identity controls.

By the numbers:

👉 Read WorkOS's guide on securely handling JWTs in PHP


Context

JWTs are compact bearer tokens used to carry identity and permission claims between systems, but they are only trustworthy after the signature and standard claims have been validated. In PHP, that means the application must treat the token as untrusted input until the issuer, audience, expiry, and algorithm checks all pass.

The governance gap is not token parsing. It is the assumption that a decodable token is already a safe identity assertion. For IAM teams, that assumption affects API authorisation, session handling, and service-to-service trust in the same way, whether the subject is a human user, a workload, or an automated service.


Key questions

Q: How should security teams validate JWTs in PHP applications?

A: Security teams should verify the signature first, then validate the expected issuer, audience, expiry, and algorithm in one central path. They should treat decoded claims as untrusted until verification succeeds and should reject any token that does not match the configured key set. This prevents authorization from being based on tampered or misrouted identity data.

Q: Why do JWTs create governance risk even when they decode successfully?

A: Because decoding only proves the token is structurally readable, not that it was issued by a trusted source or is still valid. A decoded token can still be forged, expired, or issued for a different audience. Governance risk appears when teams use those unverified claims for routing, access, or lookups before verification completes.

Q: What breaks when JWT key rotation is handled manually?

A: Manual key rotation makes verification fragile because different services can keep using stale public keys after the issuer has moved on. That causes validation failures during normal lifecycle changes and can tempt teams to weaken checks temporarily. It also increases the chance that retired keys remain trusted longer than intended.

Q: When should organisations prefer JWKS over static PEM files for JWT verification?

A: Organisations should prefer JWKS whenever multiple services need to verify tokens from the same issuer, or whenever key rotation is expected. JWKS lets verifiers select the correct key by kid, cache responses, and refresh automatically. Static PEM files are manageable only in very small environments with tightly controlled distribution.


Technical breakdown

RS256 verification and algorithm binding

JWT verification in PHP is only meaningful when the verifier binds the token to a specific algorithm and key. With RS256, the private key signs and the public key verifies, which supports safer distribution than shared-secret models. The risky pattern is accepting whatever the header advertises, because that enables algorithm confusion if a token is validated against the wrong expectation. The firebase/php-jwt library addresses this by requiring explicit key and algorithm pairing through the Key class. That makes verification deterministic rather than permissive.

Practical implication: lock every verifier to the expected algorithm and reject any token that does not match the configured signing method.

JWKS, kid, and key rotation

JWKS replaces manually distributed public keys with a published key set that verifiers can fetch and cache. The kid header identifies which key signed the token, allowing clients to look up the matching public key and verify against the current set. CachedKeySet adds caching and refresh behaviour so rotated keys do not break existing services. This model reduces operational drift, but it also means the application must trust the endpoint, cache expiry, and key selection logic as part of the authentication path.

Practical implication: use JWKS for distributed systems and test rotation paths before retiring old keys.

Claim validation and unverified-token handling

A JWT payload is Base64URL-encoded, not encrypted, so claims can be read but not trusted until verification succeeds. That distinction matters because exp, iss, aud, and sub are often used in downstream authorisation and routing decisions. The library guidance in the article is to avoid using unverified claims for anything beyond debugging. In practice, the safest pattern is to fail closed, validate required claims centrally, and treat verification errors as authentication failures rather than application exceptions.

Practical implication: never route, authorise, or query data based on unverified claims, even if the token decodes cleanly.


Read our 52 NHI Breaches Analysis report for a comprehensive view of breaches impacting Non-Human Identities including AI Agents.


NHI Mgmt Group analysis

JWT verification is an identity control, not a parsing exercise. A PHP application that decodes a token without binding it to the expected issuer, audience, algorithm, and key set is making an identity decision on unreadable assumptions. That is a governance failure because the token becomes a bearer credential whose trust boundary was never actually enforced. For IAM practitioners, the control question is whether verification is centralised and deterministic enough to survive real production drift.

Unverified JWT claims create a trust gap that spans human and machine identity. The same anti-pattern appears when APIs trust claims before signature validation, regardless of whether the subject is a user session or a service account. That is why JWT handling belongs in the same governance conversation as API access policy, service identity validation, and session assurance. The practitioner conclusion is that claim trust must follow verification, never precede it.

Key rotation is part of the identity lifecycle for API credentials. JWKS and kid are not implementation details, they are the operational layer that keeps verification aligned with key change. When services still depend on static PEM distribution, rotation becomes slow, manual, and error prone. The implication for identity teams is to treat JWT signing keys like governed credentials, not like code constants.

Bearer tokens increase blast radius when lifetime and transport are loose. Short-lived access tokens, strict HTTPS transport, and header-only presentation reduce exposure, but only when the application also refuses to use tokens from query strings or logs. This is the same blast-radius logic that underpins broader NHI governance. The practitioner takeaway is to reduce both token lifetime and token visibility wherever possible.

From our research:

  • The average estimated time to remediate a leaked secret is 27 days, despite 75% of organisations expressing strong confidence in their secrets management capabilities, according to The State of Secrets in AppSec.
  • Only 44% of developers are reported to follow security best practices for secrets management, exposing a significant developer behaviour gap.
  • That control and behaviour gap is why readers should also review Analysis of Claude Code Security for the adjacent AI-to-code trust boundary.

What this signals

JWT handling is increasingly a governance problem disguised as a coding problem. When teams centralise verification and treat JWKS as part of the identity lifecycle, they reduce the chance that key rotation or claim drift silently breaks access policy. For teams building around zero trust, NIST SP 800-207 Zero Trust Architecture remains the right reference point.

Token trust debt: organisations accumulate it whenever they allow decodable JWTs to stand in for verified identities. That debt shows up later as emergency patches, broken rotations, and uncertain ownership of bearer credential handling. The practical signal is to measure how many services still trust claims before verification and reduce that count first.


For practitioners

  • Bind verification to one expected algorithm Configure every PHP verifier to accept only the signing algorithm you intend, then fail closed on any mismatch. Do not infer trust from the header alone, and do not allow fallback to alternate algorithms in production.
  • Use JWKS for distributed key management Publish and consume public keys through JWKS so verifiers can resolve kid values and refresh rotated keys automatically. Keep old keys available until all signed tokens they issued have expired.
  • Centralise claim validation in middleware Validate iss, aud, exp, and signature in one shared middleware path so every protected endpoint enforces the same rules. Treat any failure as an authentication rejection, not a downstream application error.
  • Keep bearer tokens out of logs and URLs Require Authorization header use only, reject tokens in query parameters, and scrub full tokens from logs and error traces. That limits replay risk if a token is copied, forwarded, or exposed in debugging output.

Key takeaways

  • JWTs are safe only when verification is centralised, algorithm-bound, and applied before any claim is trusted.
  • JWKS and kid solve real operational rotation problems, but only if old keys, cache behaviour, and expiry handling are governed together.
  • For IAM and NHI programmes, bearer token handling is lifecycle work, not just developer plumbing.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP Non-Human Identity Top 10 address the attack and risk surface, while NIST CSF 2.0 and NIST Zero Trust (SP 800-207) set the governance and control requirements practitioners need to meet.

FrameworkControl / ReferenceRelevance
OWASP Non-Human Identity Top 10NHI-03JWT signing keys and rotation are governed credential controls.
NIST CSF 2.0PR.AC-4Claims and signatures control access decisions through authenticated identities.
NIST Zero Trust (SP 800-207)AC-4Zero Trust requires continuous verification of bearer credentials and claims.

Track token signing keys as governed NHI credentials and rotate before old keys expire.


Key terms

  • Json Web Token: A JSON Web Token is a compact bearer credential that carries claims between systems. It is only trustworthy after signature verification and claim validation confirm the issuer, audience, and expiry, because the payload is readable by anyone holding the token.
  • Jwks: A JSON Web Key Set is a published set of public keys that verifiers use to validate signed tokens. In production, JWKS reduces manual key distribution and supports key rotation through key identifiers, caching, and controlled refresh behaviour.
  • Kid: kid is the key identifier in a JWT header that tells a verifier which key from a JWKS should be used for validation. It matters most when issuers rotate keys, because the application must select the right public key without trusting the token’s payload first.
  • Bearer Token: A bearer token is a credential that grants access to whoever presents it. That makes transport, storage, and logging decisions critical, because possession alone is enough to replay the token unless the system tightly limits lifetime and exposure.

Deepen your knowledge

JWT validation, JWKS rotation, and claim assurance are core topics in our NHI Foundation Level course, the industry's only accredited NHI security programme. If your team is standardising API identity controls, it is worth exploring.

This post draws on content published by WorkOS: How to handle JWT in PHP. Read the original.

NHIMG Editorial Note
Published by the NHIMG editorial team on 2026-05-11.
NHI Mgmt Group — the independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org