1111
1212namespace Mcp \Server \Transport \Middleware ;
1313
14+ use Firebase \JWT \BeforeValidException ;
15+ use Firebase \JWT \ExpiredException ;
1416use Firebase \JWT \JWK ;
1517use Firebase \JWT \JWT ;
18+ use Firebase \JWT \SignatureInvalidException ;
1619use Http \Discovery \Psr17FactoryDiscovery ;
1720use Http \Discovery \Psr18ClientDiscovery ;
21+ use Mcp \Exception \RuntimeException ;
1822use Psr \Http \Client \ClientInterface ;
1923use Psr \Http \Message \RequestFactoryInterface ;
2024use Psr \Http \Message \ServerRequestInterface ;
@@ -128,31 +132,16 @@ public function validate(ServerRequestInterface $request, string $accessToken):
128132 }
129133
130134 return AuthorizationResult::allow ($ attributes );
131- } catch (\Firebase \JWT \ExpiredException $ e ) {
132- return AuthorizationResult::unauthorized (
133- 'invalid_token ' ,
134- 'Token has expired. '
135- );
136- } catch (\Firebase \JWT \SignatureInvalidException $ e ) {
137- return AuthorizationResult::unauthorized (
138- 'invalid_token ' ,
139- 'Token signature verification failed. '
140- );
141- } catch (\Firebase \JWT \BeforeValidException $ e ) {
142- return AuthorizationResult::unauthorized (
143- 'invalid_token ' ,
144- 'Token is not yet valid. '
145- );
135+ } catch (ExpiredException ) {
136+ return AuthorizationResult::unauthorized ('invalid_token ' , 'Token has expired. ' );
137+ } catch (SignatureInvalidException ) {
138+ return AuthorizationResult::unauthorized ('invalid_token ' , 'Token signature verification failed. ' );
139+ } catch (BeforeValidException ) {
140+ return AuthorizationResult::unauthorized ('invalid_token ' , 'Token is not yet valid. ' );
146141 } catch (\UnexpectedValueException |\DomainException $ e ) {
147- return AuthorizationResult::unauthorized (
148- 'invalid_token ' ,
149- 'Token validation failed: ' . $ e ->getMessage ()
150- );
151- } catch (\Throwable $ e ) {
152- return AuthorizationResult::unauthorized (
153- 'invalid_token ' ,
154- 'Token validation error. '
155- );
142+ return AuthorizationResult::unauthorized ('invalid_token ' , 'Token validation failed: ' .$ e ->getMessage ());
143+ } catch (\Throwable ) {
144+ return AuthorizationResult::unauthorized ('invalid_token ' , 'Token validation error. ' );
156145 }
157146 }
158147
@@ -320,10 +309,9 @@ private function validateIssuer(array $claims): bool
320309 return false ;
321310 }
322311
323- $ tokenIssuer = $ claims ['iss ' ];
324312 $ expectedIssuers = \is_array ($ this ->issuer ) ? $ this ->issuer : [$ this ->issuer ];
325313
326- return \in_array ($ tokenIssuer , $ expectedIssuers , true );
314+ return \in_array ($ claims [ ' iss ' ] , $ expectedIssuers , true );
327315 }
328316
329317 /**
@@ -336,24 +324,24 @@ private function fetchJwks(string $jwksUri): array
336324
337325 $ response = $ this ->httpClient ->sendRequest ($ request );
338326
339- if ($ response ->getStatusCode () >= 400 ) {
340- throw new \ RuntimeException (sprintf (
327+ if (200 !== $ response ->getStatusCode ()) {
328+ throw new RuntimeException (sprintf (
341329 'Failed to fetch JWKS from %s: HTTP %d ' ,
342330 $ jwksUri ,
343331 $ response ->getStatusCode ()
344332 ));
345333 }
346334
347- $ body = (string )$ response ->getBody ();
335+ $ body = (string ) $ response ->getBody ();
348336
349337 try {
350338 $ data = json_decode ($ body , true , 512 , \JSON_THROW_ON_ERROR );
351339 } catch (\JsonException $ e ) {
352- throw new \ RuntimeException (sprintf ('Failed to decode JWKS: %s ' , $ e ->getMessage ()), 0 , $ e );
340+ throw new RuntimeException (sprintf ('Failed to decode JWKS: %s ' , $ e ->getMessage ()), 0 , $ e );
353341 }
354342
355343 if (!\is_array ($ data ) || !isset ($ data ['keys ' ])) {
356- throw new \ RuntimeException ('Invalid JWKS format: missing "keys" array. ' );
344+ throw new RuntimeException ('Invalid JWKS format: missing "keys" array. ' );
357345 }
358346
359347 /** @var array<string, mixed> $data */
0 commit comments