| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Server\Transport\Http\Middleware; |
| 15: | |
| 16: | use Nexus\Mcp\Core\Auth\ResourceIdentifier; |
| 17: | use Nexus\Mcp\Core\Auth\ScopeSet; |
| 18: | use Nexus\Mcp\Core\Auth\VerifiedAccessToken; |
| 19: | use Nexus\Mcp\Core\Auth\WwwAuthenticateChallenge; |
| 20: | use Nexus\Mcp\Core\Http\HttpStatus; |
| 21: | use Nexus\Mcp\Server\Auth\AccessTokenValidatorInterface; |
| 22: | use Psr\Http\Message\ResponseFactoryInterface; |
| 23: | use Psr\Http\Message\ResponseInterface; |
| 24: | use Psr\Http\Message\ServerRequestInterface; |
| 25: | use Psr\Http\Server\MiddlewareInterface; |
| 26: | use Psr\Http\Server\RequestHandlerInterface; |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | final readonly class BearerAuthenticationMiddleware implements MiddlewareInterface |
| 40: | { |
| 41: | private const string BEARER_PREFIX = WwwAuthenticateChallenge::BEARER_SCHEME.' '; |
| 42: | |
| 43: | private ResourceIdentifier $resource; |
| 44: | private ScopeSet $requiredScopes; |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | public function __construct( |
| 52: | private AccessTokenValidatorInterface $validator, |
| 53: | string $resource, |
| 54: | private string $resourceMetadataUrl, |
| 55: | private ResponseFactoryInterface $responseFactory, |
| 56: | array $requiredScopes = [], |
| 57: | ) { |
| 58: | $this->resource = new ResourceIdentifier($resource); |
| 59: | $this->requiredScopes = new ScopeSet($requiredScopes); |
| 60: | } |
| 61: | |
| 62: | #[\Override] |
| 63: | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 64: | { |
| 65: | $headers = $request->getHeader('Authorization'); |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | if (! self::presentsBearerScheme($headers)) { |
| 71: | return $this->challenge(HttpStatus::Unauthorized, null); |
| 72: | } |
| 73: | |
| 74: | $presented = self::readBearerToken($headers); |
| 75: | |
| 76: | if (null === $presented) { |
| 77: | return $this->challenge(HttpStatus::BadRequest, 'invalid_request'); |
| 78: | } |
| 79: | |
| 80: | $token = $this->validator->validate($presented); |
| 81: | |
| 82: | if (null === $token) { |
| 83: | return $this->challenge(HttpStatus::Unauthorized, 'invalid_token'); |
| 84: | } |
| 85: | |
| 86: | |
| 87: | if (! $this->resource->matchesAudience($token->audience)) { |
| 88: | return $this->challenge(HttpStatus::Unauthorized, 'invalid_token'); |
| 89: | } |
| 90: | |
| 91: | if (! new ScopeSet($token->scopes)->containsAll($this->requiredScopes)) { |
| 92: | return $this->challenge(HttpStatus::Forbidden, 'insufficient_scope'); |
| 93: | } |
| 94: | |
| 95: | return $handler->handle($request->withAttribute(VerifiedAccessToken::REQUEST_ATTRIBUTE, $token)); |
| 96: | } |
| 97: | |
| 98: | private function challenge(HttpStatus $status, ?string $error): ResponseInterface |
| 99: | { |
| 100: | $parameters = ['resource_metadata' => $this->resourceMetadataUrl]; |
| 101: | |
| 102: | if (null !== $error) { |
| 103: | $parameters['error'] = $error; |
| 104: | } |
| 105: | |
| 106: | $scope = $this->requiredScopes->toParameter(); |
| 107: | |
| 108: | if (null !== $scope) { |
| 109: | $parameters['scope'] = $scope; |
| 110: | } |
| 111: | |
| 112: | return $this->responseFactory->createResponse($status->value) |
| 113: | ->withHeader('WWW-Authenticate', new WwwAuthenticateChallenge( |
| 114: | WwwAuthenticateChallenge::BEARER_SCHEME, |
| 115: | $parameters, |
| 116: | )->toHeaderValue()) |
| 117: | ; |
| 118: | } |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | private static function presentsBearerScheme(array $headers): bool |
| 129: | { |
| 130: | foreach ($headers as $header) { |
| 131: | |
| 132: | if (strcasecmp(explode(' ', $header)[0], WwwAuthenticateChallenge::BEARER_SCHEME) === 0) { |
| 133: | return true; |
| 134: | } |
| 135: | } |
| 136: | |
| 137: | return false; |
| 138: | } |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | private static function readBearerToken(array $headers): ?string |
| 144: | { |
| 145: | |
| 146: | |
| 147: | if (\count($headers) !== 1) { |
| 148: | return null; |
| 149: | } |
| 150: | |
| 151: | |
| 152: | |
| 153: | $token = trim(substr(reset($headers), \strlen(self::BEARER_PREFIX))); |
| 154: | |
| 155: | return '' === $token ? null : $token; |
| 156: | } |
| 157: | } |
| 158: | |