| 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; |
| 15: | |
| 16: | use Nexus\Mcp\Core\Auth\ProtectedResourceMetadata; |
| 17: | use Nexus\Mcp\Core\Auth\ResourceIdentifier; |
| 18: | use Nexus\Mcp\Core\Auth\ScopeSet; |
| 19: | use Nexus\Mcp\Core\Http\HttpStatus; |
| 20: | use Psr\Http\Message\ResponseFactoryInterface; |
| 21: | use Psr\Http\Message\ResponseInterface; |
| 22: | use Psr\Http\Message\ServerRequestInterface; |
| 23: | use Psr\Http\Message\StreamFactoryInterface; |
| 24: | use Psr\Http\Server\RequestHandlerInterface; |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | final readonly class ProtectedResourceMetadataHandler implements RequestHandlerInterface |
| 36: | { |
| 37: | |
| 38: | |
| 39: | |
| 40: | private const string BEARER_METHOD_HEADER = 'header'; |
| 41: | |
| 42: | private const string WELL_KNOWN_PATH = '/.well-known/oauth-protected-resource'; |
| 43: | |
| 44: | private ProtectedResourceMetadata $document; |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | private array $paths; |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | public function __construct( |
| 60: | string $resource, |
| 61: | array $authorizationServers, |
| 62: | private ResponseFactoryInterface $responseFactory, |
| 63: | private StreamFactoryInterface $streamFactory, |
| 64: | array $scopesSupported = [], |
| 65: | ?string $resourceName = null, |
| 66: | ) { |
| 67: | $identifier = new ResourceIdentifier($resource); |
| 68: | $this->document = new ProtectedResourceMetadata( |
| 69: | $identifier, |
| 70: | $authorizationServers, |
| 71: | [] === $scopesSupported ? null : new ScopeSet($scopesSupported), |
| 72: | [self::BEARER_METHOD_HEADER], |
| 73: | $resourceName, |
| 74: | ); |
| 75: | |
| 76: | $path = rtrim((string) parse_url($identifier->value, \PHP_URL_PATH), '/'); |
| 77: | $this->paths = '' === $path |
| 78: | ? [self::WELL_KNOWN_PATH] |
| 79: | : [self::WELL_KNOWN_PATH.$path, self::WELL_KNOWN_PATH]; |
| 80: | } |
| 81: | |
| 82: | #[\Override] |
| 83: | public function handle(ServerRequestInterface $request): ResponseInterface |
| 84: | { |
| 85: | |
| 86: | |
| 87: | if (! \in_array($request->getUri()->getPath(), $this->paths, true)) { |
| 88: | return $this->responseFactory->createResponse(HttpStatus::NotFound->value); |
| 89: | } |
| 90: | |
| 91: | if ($request->getMethod() !== 'GET') { |
| 92: | return $this->responseFactory->createResponse(HttpStatus::MethodNotAllowed->value)->withHeader('Allow', 'GET'); |
| 93: | } |
| 94: | |
| 95: | return $this->responseFactory->createResponse(HttpStatus::Ok->value) |
| 96: | ->withHeader('Content-Type', 'application/json') |
| 97: | ->withBody($this->streamFactory->createStream( |
| 98: | json_encode($this->document->toArray(), \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES), |
| 99: | )) |
| 100: | ; |
| 101: | } |
| 102: | } |
| 103: | |