-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathAuthorizationMiddleware.php
More file actions
182 lines (149 loc) · 6.07 KB
/
AuthorizationMiddleware.php
File metadata and controls
182 lines (149 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Server\Transport\Http\Middleware;
use Http\Discovery\Psr17FactoryDiscovery;
use Mcp\Exception\RuntimeException;
use Mcp\Server\Transport\Http\OAuth\AuthorizationResult;
use Mcp\Server\Transport\Http\OAuth\AuthorizationTokenValidatorInterface;
use Mcp\Server\Transport\Http\OAuth\ProtectedResourceMetadata;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
/**
* Enforces MCP HTTP authorization requirements.
*
* This middleware:
* - Validates Bearer tokens via the configured validator
* - Returns 401 with WWW-Authenticate header on missing/invalid tokens
* - Returns 403 on insufficient scope
*
* @see https://modelcontextprotocol.io/specification/2025-11-25/basic/authorization
*
* @author Volodymyr Panivko <sveneld300@gmail.com>
*/
final class AuthorizationMiddleware implements MiddlewareInterface
{
private ResponseFactoryInterface $responseFactory;
/**
* @param AuthorizationTokenValidatorInterface $validator Token validator implementation
* @param ProtectedResourceMetadata $resourceMetadata Protected resource metadata object used for challenge hints
* @param ResponseFactoryInterface|null $responseFactory PSR-17 response factory (auto-discovered if null)
*/
public function __construct(
private AuthorizationTokenValidatorInterface $validator,
private ProtectedResourceMetadata $resourceMetadata,
?ResponseFactoryInterface $responseFactory = null,
) {
$this->responseFactory = $responseFactory ?? Psr17FactoryDiscovery::findResponseFactory();
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$authorization = $request->getHeaderLine('Authorization');
if ('' === $authorization) {
return $this->buildErrorResponse($request, AuthorizationResult::unauthorized());
}
$accessToken = $this->parseBearerToken($authorization);
if (null === $accessToken) {
return $this->buildErrorResponse(
$request,
AuthorizationResult::badRequest('invalid_request', 'Malformed Authorization header.'),
);
}
$result = $this->validator->validate($accessToken);
if (!$result->isAllowed()) {
return $this->buildErrorResponse($request, $result);
}
return $handler->handle($this->applyAttributes($request, $result->getAttributes()));
}
private function buildErrorResponse(ServerRequestInterface $request, AuthorizationResult $result): ResponseInterface
{
$response = $this->responseFactory->createResponse($result->getStatusCode());
$header = $this->buildAuthenticateHeader($request, $result);
$response = $response->withHeader('WWW-Authenticate', $header);
return $response;
}
private function buildAuthenticateHeader(ServerRequestInterface $request, AuthorizationResult $result): string
{
$parts = [];
$parts[] = 'resource_metadata="'.$this->escapeHeaderValue($this->resolveResourceMetadataUrl($request)).'"';
$scopes = $this->resolveScopes($result);
if (null !== $scopes) {
$parts[] = 'scope="'.$this->escapeHeaderValue(implode(' ', $scopes)).'"';
}
if (null !== $result->getError()) {
$parts[] = 'error="'.$this->escapeHeaderValue($result->getError()).'"';
}
if (null !== $result->getErrorDescription()) {
$parts[] = 'error_description="'.$this->escapeHeaderValue($result->getErrorDescription()).'"';
}
return 'Bearer '.implode(', ', $parts);
}
/**
* @return list<string>|null
*/
private function resolveScopes(AuthorizationResult $result): ?array
{
$scopes = $this->normalizeScopes($result->getScopes());
if (null !== $scopes) {
return $scopes;
}
return $this->normalizeScopes($this->resourceMetadata->getScopesSupported());
}
/**
* @param list<string>|null $scopes
*
* @return list<string>|null
*/
private function normalizeScopes(?array $scopes): ?array
{
if (null === $scopes) {
return null;
}
$normalized = array_values(array_filter(array_map('trim', $scopes), static function (string $scope): bool {
return '' !== $scope;
}));
return [] === $normalized ? null : $normalized;
}
private function resolveResourceMetadataUrl(ServerRequestInterface $request): string
{
$metadataPath = $this->resourceMetadata->getPrimaryMetadataPath();
$uri = $request->getUri();
$scheme = $uri->getScheme();
$authority = $uri->getAuthority();
if ('' === $scheme || '' === $authority) {
throw new RuntimeException('Cannot resolve resource metadata URL: request URI must have scheme and authority');
}
return $scheme.'://'.$authority.$metadataPath;
}
/**
* @param array<string, mixed> $attributes
*/
private function applyAttributes(ServerRequestInterface $request, array $attributes): ServerRequestInterface
{
foreach ($attributes as $name => $value) {
$request = $request->withAttribute($name, $value);
}
return $request;
}
private function parseBearerToken(string $authorization): ?string
{
if (!preg_match('/^Bearer\\s+(.+)$/i', $authorization, $matches)) {
return null;
}
$token = trim($matches[1]);
return '' === $token ? null : $token;
}
private function escapeHeaderValue(string $value): string
{
return str_replace(['\\', '"'], ['\\\\', '\\"'], $value);
}
}