JwtTokenValidator implements AuthorizationTokenValidatorInterface
Validates JWT access tokens issued by one authorization server.
Checks, in order: the alg header against the allowlist, the typ header when
required, the signature and time claims, the presence of exp, the issuer, and the audience. The
audience must name this MCP server — accepting tokens minted for another
resource is the token passthrough the MCP specification forbids.
Requires: firebase/php-jwt
Tags
Table of Contents
Interfaces
- AuthorizationTokenValidatorInterface
- Validates bearer tokens for HTTP transports.
Properties
- $algorithms : array<string|int, mixed>
- $audiences : array<int, string>
- $issuer : string
- $keys : ArrayAccess|array<string|int, mixed>
- $keysForAlgorithm : callable(string): ArrayAccess<string, Key>|null
- $leeway : int
- $scopeClaim : string
- $tokenType : string|null
Methods
- __construct() : mixed
- fromIssuer() : self
- Builds a validator that discovers the issuer's JWKS URI and caches its keys.
- validate() : AuthorizationResult
- Validates an access token extracted from the Authorization header.
- decodeHeader() : array<string, mixed>|null
- extractScopes() : array<int, string>
- isExpectedType() : bool
Properties
$algorithms read-only
private
array<string|int, mixed>
$algorithms
= ['RS256']
$audiences read-only
private
array<int, string>
$audiences
$issuer read-only
private
string
$issuer
$keys read-only
private
ArrayAccess|array<string|int, mixed>
$keys
$keysForAlgorithm
private
callable(string): ArrayAccess<string, Key>|null
$keysForAlgorithm
= null
key set per alg, set by self::fromIssuer()
$leeway read-only
private
int
$leeway
= 0
$scopeClaim read-only
private
string
$scopeClaim
= 'scope'
$tokenType read-only
private
string|null
$tokenType
= null
Methods
__construct()
public
__construct(string $issuer, string|array<int, string> $audience, ArrayAccess<string, Key>|array<string, Key> $keys[, array<int, string> $algorithms = ['RS256'] ][, string $scopeClaim = 'scope' ][, string|null $tokenType = null ][, int $leeway = 0 ]) : mixed
Parameters
- $issuer : string
-
expected
issclaim - $audience : string|array<int, string>
-
accepted
audvalues, typically the resource identifier of this MCP server - $keys : ArrayAccess<string, Key>|array<string, Key>
-
verification keys by key id, e.g. a CachedKeySet
- $algorithms : array<int, string> = ['RS256']
-
accepted
algheader values - $scopeClaim : string = 'scope'
-
claim holding the granted scopes
- $tokenType : string|null = null
-
required
typheader, e.g.at+jwt(RFC 9068); null skips the check - $leeway : int = 0
-
tolerated clock skew in seconds
fromIssuer()
Builds a validator that discovers the issuer's JWKS URI and caches its keys.
public
static fromIssuer(string $issuer, string|array<int, string> $audience, CacheItemPoolInterface $cache[, ClientInterface|null $httpClient = null ][, RequestFactoryInterface|null $requestFactory = null ][, array<int, string> $algorithms = ['RS256'] ][, string $scopeClaim = 'scope' ][, string|null $tokenType = null ][, int $leeway = 0 ]) : self
Discovery happens on the first token, so an unreachable authorization server
does not break building the validator; a failed discovery is cached for a few
seconds, reporting validation as unavailable without retrying. Keys are refetched, rate limited, when a
token names an unknown key id, so key rotation does not lock clients out until
the cache expires. Issuer and JWKS URI must use https, unless they point to a
loopback host (localhost, *.localhost, 127.0.0.0/8 or ::1).
Parameters
- $issuer : string
- $audience : string|array<int, string>
- $cache : CacheItemPoolInterface
- $httpClient : ClientInterface|null = null
- $requestFactory : RequestFactoryInterface|null = null
- $algorithms : array<int, string> = ['RS256']
- $scopeClaim : string = 'scope'
- $tokenType : string|null = null
- $leeway : int = 0
Return values
selfvalidate()
Validates an access token extracted from the Authorization header.
public
validate(string $accessToken) : AuthorizationResult
Parameters
- $accessToken : string
-
The bearer token (without "Bearer " prefix)
Return values
AuthorizationResult —The result of the validation
decodeHeader()
private
decodeHeader(string $token) : array<string, mixed>|null
Parameters
- $token : string
Return values
array<string, mixed>|nullextractScopes()
private
extractScopes(array<string, mixed> $claims) : array<int, string>
Parameters
- $claims : array<string, mixed>
Return values
array<int, string>isExpectedType()
private
isExpectedType(mixed $type) : bool
Parameters
- $type : mixed