|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Mcp\Server\Authentication\Provider; |
| 6 | + |
| 7 | +use Mcp\Server\Authentication\AuthInfo; |
| 8 | +use Mcp\Server\Authentication\Contract\OAuthTokenVerifierInterface; |
| 9 | +use Mcp\Server\Authentication\Contract\TokenIntrospectionClientInterface; |
| 10 | +use Mcp\Server\Authentication\DefaultAuthInfo; |
| 11 | +use Mcp\Server\Authentication\Dto\UserProfile; |
| 12 | +use Mcp\Server\Authentication\Error\InvalidTokenError; |
| 13 | + |
| 14 | +/** |
| 15 | + * Generic OAuth token verifier supporting multiple providers. |
| 16 | + */ |
| 17 | +final readonly class GenericTokenVerifier implements OAuthTokenVerifierInterface |
| 18 | +{ |
| 19 | + public function __construct( |
| 20 | + private TokenIntrospectionConfig $config, |
| 21 | + private TokenIntrospectionClientInterface $client, |
| 22 | + ) {} |
| 23 | + |
| 24 | + /** |
| 25 | + * @throws InvalidTokenError |
| 26 | + */ |
| 27 | + public function verifyAccessToken(string $token): AuthInfo |
| 28 | + { |
| 29 | + // Verify token and get user profile |
| 30 | + $tokenData = $this->verifyToken($token); |
| 31 | + $userProfile = $this->getUserProfile($token, $tokenData); |
| 32 | + |
| 33 | + // Create AuthInfo |
| 34 | + return $this->createAuthInfo($token, $tokenData, $userProfile); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @return array<string, mixed> |
| 39 | + * @throws InvalidTokenError |
| 40 | + */ |
| 41 | + private function verifyToken(string $token): array |
| 42 | + { |
| 43 | + // Try introspection first if configured and enabled |
| 44 | + if ($this->config->useIntrospection && $this->config->introspectionUrl !== null) { |
| 45 | + try { |
| 46 | + return $this->client->introspectToken( |
| 47 | + $token, |
| 48 | + $this->config->introspectionUrl, |
| 49 | + $this->config->headers, |
| 50 | + ); |
| 51 | + } catch (\Throwable $e) { |
| 52 | + // If introspection fails and we have userinfo URL, fall back to it |
| 53 | + if ($this->config->userinfoUrl !== null) { |
| 54 | + // For userinfo-only flow, we'll get token info from the user profile |
| 55 | + return []; |
| 56 | + } |
| 57 | + |
| 58 | + throw $e; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // For providers without introspection, we'll validate through userinfo endpoint |
| 63 | + if ($this->config->userinfoUrl !== null) { |
| 64 | + // Token validation will happen when we call getUserInfo |
| 65 | + return []; |
| 66 | + } |
| 67 | + |
| 68 | + throw new InvalidTokenError('No token verification endpoint configured'); |
| 69 | + } |
| 70 | + |
| 71 | + private function getUserProfile(string $token, array $tokenData): UserProfile |
| 72 | + { |
| 73 | + if ($this->config->userinfoUrl === null) { |
| 74 | + // Try to build user profile from introspection data |
| 75 | + return $this->buildUserProfileFromIntrospection($tokenData); |
| 76 | + } |
| 77 | + |
| 78 | + // Get user info from userinfo endpoint |
| 79 | + $userInfo = $this->client->getUserInfo($token, $this->config->userinfoUrl); |
| 80 | + |
| 81 | + return $this->buildUserProfileFromUserInfo($userInfo); |
| 82 | + } |
| 83 | + |
| 84 | + private function buildUserProfileFromIntrospection(array $data): UserProfile |
| 85 | + { |
| 86 | + // Map introspection fields to standard profile fields |
| 87 | + $mappedData = $this->mapFields($data, $this->config->userFieldMapping); |
| 88 | + |
| 89 | + return new UserProfile( |
| 90 | + sub: $mappedData['sub'] ?? $data['sub'] ?? 'unknown', |
| 91 | + preferredUsername: $mappedData['preferred_username'] ?? $data['username'] ?? null, |
| 92 | + name: $mappedData['name'] ?? $data['name'] ?? null, |
| 93 | + email: $mappedData['email'] ?? $data['email'] ?? null, |
| 94 | + emailVerified: $mappedData['email_verified'] ?? $data['email_verified'] ?? null, |
| 95 | + givenName: $mappedData['given_name'] ?? $data['given_name'] ?? null, |
| 96 | + familyName: $mappedData['family_name'] ?? $data['family_name'] ?? null, |
| 97 | + picture: $mappedData['picture'] ?? $data['picture'] ?? null, |
| 98 | + extra: $data, |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + private function buildUserProfileFromUserInfo(array $data): UserProfile |
| 103 | + { |
| 104 | + // Map provider fields to standard profile fields |
| 105 | + $mappedData = $this->mapFields($data, $this->config->userFieldMapping); |
| 106 | + |
| 107 | + return new UserProfile( |
| 108 | + sub: $mappedData['sub'] ?? $data['id'] ?? (string)($data['user_id'] ?? 'unknown'), |
| 109 | + preferredUsername: $mappedData['preferred_username'] ?? $data['login'] ?? $data['username'] ?? null, |
| 110 | + name: $mappedData['name'] ?? $data['name'] ?? null, |
| 111 | + email: $mappedData['email'] ?? $data['email'] ?? null, |
| 112 | + emailVerified: $mappedData['email_verified'] ?? $data['email_verified'] ?? null, |
| 113 | + givenName: $mappedData['given_name'] ?? $data['given_name'] ?? null, |
| 114 | + familyName: $mappedData['family_name'] ?? $data['family_name'] ?? null, |
| 115 | + picture: $mappedData['picture'] ?? $data['avatar_url'] ?? $data['picture'] ?? null, |
| 116 | + extra: $data, |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @param array<string, mixed> $data |
| 122 | + * @param array<string, string> $mapping |
| 123 | + * @return array<string, mixed> |
| 124 | + */ |
| 125 | + private function mapFields(array $data, array $mapping): array |
| 126 | + { |
| 127 | + $mapped = []; |
| 128 | + |
| 129 | + foreach ($mapping as $sourceField => $targetField) { |
| 130 | + if (isset($data[$sourceField])) { |
| 131 | + $mapped[$targetField] = $data[$sourceField]; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return $mapped; |
| 136 | + } |
| 137 | + |
| 138 | + private function createAuthInfo(string $token, array $tokenData, UserProfile $userProfile): AuthInfo |
| 139 | + { |
| 140 | + // Extract client ID |
| 141 | + $clientId = $tokenData['client_id'] ?? $tokenData['aud'] ?? $userProfile->sub; |
| 142 | + |
| 143 | + // Extract scopes |
| 144 | + $scopes = []; |
| 145 | + if (isset($tokenData['scope'])) { |
| 146 | + $scopes = \is_array($tokenData['scope']) |
| 147 | + ? $tokenData['scope'] |
| 148 | + : \explode(' ', (string)$tokenData['scope']); |
| 149 | + } elseif (isset($tokenData['scopes'])) { |
| 150 | + $scopes = \is_array($tokenData['scopes']) ? $tokenData['scopes'] : [$tokenData['scopes']]; |
| 151 | + } |
| 152 | + |
| 153 | + // Extract expiration |
| 154 | + $expiresAt = $tokenData['exp'] ?? null; |
| 155 | + if ($expiresAt !== null && !\is_int($expiresAt)) { |
| 156 | + $expiresAt = (int)$expiresAt; |
| 157 | + } |
| 158 | + |
| 159 | + // Extract resource/audience |
| 160 | + $resource = $tokenData['aud'] ?? $tokenData['resource'] ?? null; |
| 161 | + |
| 162 | + // Combine all extra data |
| 163 | + $extra = \array_merge( |
| 164 | + ['user_profile' => $userProfile->jsonSerialize()], |
| 165 | + $tokenData, |
| 166 | + ); |
| 167 | + |
| 168 | + return new DefaultAuthInfo( |
| 169 | + token: $token, |
| 170 | + clientId: (string)$clientId, |
| 171 | + scopes: $scopes, |
| 172 | + expiresAt: $expiresAt, |
| 173 | + resource: $resource, |
| 174 | + extra: $extra, |
| 175 | + ); |
| 176 | + } |
| 177 | +} |
0 commit comments