@@ -46,62 +46,70 @@ public function process(
4646 $ method = $ request ->getMethod ();
4747
4848 // Handle OAuth endpoints (no authentication required for these)
49- $ oauthResponse = match ([$ path , $ method ]) {
50- ['/.well-known/mcp-oauth-metadata ' , 'GET ' ] => $ this ->metadataHandler ->handleOAuthMetadata ($ request ),
49+ $ oauthResponse = match (true ) {
50+ [$ path , $ method ] === [
51+ '/.well-known/mcp-oauth-metadata ' ,
52+ 'GET ' ,
53+ ] => $ this ->metadataHandler ->handleOAuthMetadata ($ request ),
5154 \str_starts_with (
5255 $ path ,
5356 '/.well-known/oauth-protected-resource ' ,
5457 ) && $ method === 'GET ' => $ this ->metadataHandler ->handleProtectedResourceMetadata (
5558 $ request ,
5659 ),
57- ['/.well-known/oauth-authorization-server ' , 'GET ' ] => $ this ->metadataHandler ->handleOAuthMetadata (
60+ \str_starts_with (
61+ $ path ,
62+ '/.well-known/oauth-authorization-server ' ,
63+ ) && $ method === 'GET ' => $ this ->metadataHandler ->handleOAuthMetadata (
5864 $ request ,
5965 ),
60- ['/oauth2/authorize ' , 'GET ' ] => $ this ->authorizeHandler ->handle ($ request ),
61- ['/oauth2/token ' , 'POST ' ] => $ this ->tokenHandler ->handle ($ request ),
62- ['/oauth2/revoke ' , 'POST ' ] => $ this ->revokeHandler ->handle ($ request ),
63- ['/oauth2/register ' , 'POST ' ] => $ this ->registerHandler ->handle ($ request ),
66+ [$ path , $ method ] == [ '/oauth2/authorize ' , 'GET ' ] => $ this ->authorizeHandler ->handle ($ request ),
67+ [$ path , $ method ] == [ '/oauth2/token ' , 'POST ' ] => $ this ->tokenHandler ->handle ($ request ),
68+ [$ path , $ method ] == [ '/oauth2/revoke ' , 'POST ' ] => $ this ->revokeHandler ->handle ($ request ),
69+ [$ path , $ method ] == [ '/oauth2/register ' , 'POST ' ] => $ this ->registerHandler ->handle ($ request ),
6470 default => null ,
6571 };
6672
6773 if ($ oauthResponse !== null ) {
6874 return $ oauthResponse ;
6975 }
7076
71- // For all other requests, require authentication if token verifier is configured
72- if ( $ this -> tokenVerifier !== null ) {
73- try {
77+ try {
78+ // For all other requests, require authentication if token verifier is configured
79+ if ( $ this -> tokenVerifier !== null ) {
7480 $ authInfo = $ this ->extractAndVerifyToken ($ request );
7581
7682 // Check required scopes
77- if (!empty ($ this ->requiredScopes )) {
78- $ this ->validateScopes ($ authInfo ->getScopes (), $ this ->requiredScopes );
79- }
83+ if ($ authInfo !== null ) {
84+ if (!empty ($ this ->requiredScopes )) {
85+ $ this ->validateScopes ($ authInfo ->getScopes (), $ this ->requiredScopes );
86+ }
8087
81- // Add authenticated user to request attributes
82- $ request = $ request ->withAttribute ('auth ' , $ authInfo );
83- } catch (InvalidTokenError ) {
84- return $ this ->createAuthErrorResponse (401 , 'invalid_token ' , 'Authentication required ' );
85- } catch (InsufficientScopeError $ e ) {
86- return $ this ->createAuthErrorResponse (403 , 'insufficient_scope ' , $ e ->getMessage ());
88+ // Add authenticated user to request attributes
89+ $ request = $ request ->withAttribute ('auth ' , $ authInfo );
90+ }
8791 }
88- }
8992
90- return $ handler ->handle ($ request );
93+ return $ handler ->handle ($ request );
94+ } catch (InvalidTokenError $ e ) {
95+ return $ this ->createAuthErrorResponse (401 , 'invalid_token ' , 'Authentication required ' );
96+ } catch (InsufficientScopeError $ e ) {
97+ return $ this ->createAuthErrorResponse (403 , 'insufficient_scope ' , $ e ->getMessage ());
98+ }
9199 }
92100
93- private function extractAndVerifyToken (ServerRequestInterface $ request ): AuthInfo
101+ private function extractAndVerifyToken (ServerRequestInterface $ request ): ? AuthInfo
94102 {
95103 // Extract Bearer token from Authorization header
96104 $ authHeader = $ request ->getHeaderLine ('Authorization ' );
97105 if (!$ authHeader || !\str_starts_with (\strtolower ($ authHeader ), 'bearer ' )) {
98- throw new InvalidTokenError ( ' Missing or invalid Authorization header ' ) ;
106+ return null ;
99107 }
100108
101109 $ token = \substr ($ authHeader , 7 ); // Remove "Bearer " prefix
102110
103111 if (empty ($ token )) {
104- throw new InvalidTokenError ( ' Empty bearer token ' ) ;
112+ return null ;
105113 }
106114
107115 // Verify token
0 commit comments