|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Mcp\Server\Authentication\Contract; |
| 6 | + |
| 7 | +use Mcp\Server\Authentication\AuthInfo; |
| 8 | +use Mcp\Server\Authentication\AuthorizationParams; |
| 9 | +use Mcp\Server\Authentication\Dto\OAuthClientInformation; |
| 10 | +use Mcp\Server\Authentication\Dto\OAuthTokenRevocationRequest; |
| 11 | +use Mcp\Server\Authentication\Dto\OAuthTokens; |
| 12 | +use Psr\Http\Message\ResponseInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Implements an end-to-end OAuth server. |
| 16 | + */ |
| 17 | +interface OAuthServerProviderInterface |
| 18 | +{ |
| 19 | + /** |
| 20 | + * A store used to read information about registered OAuth clients. |
| 21 | + */ |
| 22 | + public function getClientsStore(): OAuthRegisteredClientsStoreInterface; |
| 23 | + |
| 24 | + /** |
| 25 | + * Begins the authorization flow, which can either be implemented by this server itself |
| 26 | + * or via redirection to a separate authorization server. |
| 27 | + * |
| 28 | + * This server must eventually issue a redirect with an authorization response or an |
| 29 | + * error response to the given redirect URI. Per OAuth 2.1: |
| 30 | + * - In the successful case, the redirect MUST include the `code` and `state` (if present) query parameters. |
| 31 | + * - In the error case, the redirect MUST include the `error` query parameter, and MAY include |
| 32 | + * an optional `error_description` query parameter. |
| 33 | + * |
| 34 | + * @throws \RuntimeException if authorization fails |
| 35 | + */ |
| 36 | + public function authorize( |
| 37 | + OAuthClientInformation $client, |
| 38 | + AuthorizationParams $params, |
| 39 | + ResponseInterface $response, |
| 40 | + ): ResponseInterface; |
| 41 | + |
| 42 | + /** |
| 43 | + * Returns the `codeChallenge` that was used when the indicated authorization began. |
| 44 | + * |
| 45 | + * @throws \RuntimeException if code challenge retrieval fails |
| 46 | + */ |
| 47 | + public function challengeForAuthorizationCode( |
| 48 | + OAuthClientInformation $client, |
| 49 | + string $authorizationCode, |
| 50 | + ): string; |
| 51 | + |
| 52 | + /** |
| 53 | + * Exchanges an authorization code for an access token. |
| 54 | + * |
| 55 | + * @throws \RuntimeException if token exchange fails |
| 56 | + */ |
| 57 | + public function exchangeAuthorizationCode( |
| 58 | + OAuthClientInformation $client, |
| 59 | + string $authorizationCode, |
| 60 | + ?string $codeVerifier = null, |
| 61 | + ?string $redirectUri = null, |
| 62 | + ?string $resource = null, |
| 63 | + ): OAuthTokens; |
| 64 | + |
| 65 | + /** |
| 66 | + * Exchanges a refresh token for an access token. |
| 67 | + * |
| 68 | + * @param string[] $scopes |
| 69 | + * |
| 70 | + * @throws \RuntimeException if token exchange fails |
| 71 | + */ |
| 72 | + public function exchangeRefreshToken( |
| 73 | + OAuthClientInformation $client, |
| 74 | + string $refreshToken, |
| 75 | + array $scopes = [], |
| 76 | + ?string $resource = null, |
| 77 | + ): OAuthTokens; |
| 78 | + |
| 79 | + /** |
| 80 | + * Verifies an access token and returns information about it. |
| 81 | + * |
| 82 | + * @throws \RuntimeException if token verification fails |
| 83 | + */ |
| 84 | + public function verifyAccessToken(string $token): AuthInfo; |
| 85 | + |
| 86 | + /** |
| 87 | + * Revokes an access or refresh token. If unimplemented, token revocation is not supported (not recommended). |
| 88 | + * |
| 89 | + * If the given token is invalid or already revoked, this method should do nothing. |
| 90 | + * |
| 91 | + * @throws \RuntimeException if token revocation fails |
| 92 | + */ |
| 93 | + public function revokeToken( |
| 94 | + OAuthClientInformation $client, |
| 95 | + OAuthTokenRevocationRequest $request, |
| 96 | + ): void; |
| 97 | + |
| 98 | + /** |
| 99 | + * Whether to skip local PKCE validation. |
| 100 | + * |
| 101 | + * If true, the server will not perform PKCE validation locally and will pass the |
| 102 | + * code_verifier to the upstream server. |
| 103 | + * |
| 104 | + * NOTE: This should only be true if the upstream server is performing the actual PKCE validation. |
| 105 | + */ |
| 106 | + public function skipLocalPkceValidation(): bool; |
| 107 | +} |
0 commit comments