|null */ public static function resolve( string $requestPath ): ?array { if ( '' === $requestPath ) { return null; } $relative = self::stripHomePath( $requestPath ); $kind = self::candidates()[ $relative ] ?? null; if ( null === $kind ) { return null; } return self::DOC_PROTECTED_RESOURCE === $kind ? McpOAuthDiscovery::protectedResourceMetadata() : McpOAuthDiscovery::authorizationServerMetadata(); } /** * Every path this handler answers, mapped to which document it serves: * the two bare `/.well-known/...` paths, plus the endpoint-path-suffixed * variant {@see https://www.rfc-editor.org/rfc/rfc9728 RFC 9728} and * {@see https://www.rfc-editor.org/rfc/rfc8414 RFC 8414} describe for a * multi-tenant-style issuer path, and the `openid-configuration` alias. * * @return array */ private static function candidates(): array { $mcp_relative = self::stripHomePath( self::pathOf( McpOAuthConstants::mcpEndpointUrl() ) ); $issuer_relative = self::stripHomePath( self::pathOf( McpOAuthConstants::issuerUrl() ) ); return array( '/.well-known/oauth-protected-resource' => self::DOC_PROTECTED_RESOURCE, '/.well-known/oauth-protected-resource' . $mcp_relative => self::DOC_PROTECTED_RESOURCE, '/.well-known/oauth-authorization-server' => self::DOC_AUTHORIZATION_SERVER, '/.well-known/oauth-authorization-server' . $issuer_relative => self::DOC_AUTHORIZATION_SERVER, '/.well-known/openid-configuration' => self::DOC_AUTHORIZATION_SERVER, '/.well-known/openid-configuration' . $issuer_relative => self::DOC_AUTHORIZATION_SERVER, ); } /** * @param array $document Document to send. * @return void */ private static function sendJson( array $document ): void { if ( ! headers_sent() ) { header( 'Content-Type: application/json; charset=utf-8' ); header( 'Access-Control-Allow-Origin: *' ); header( 'Cache-Control: public, max-age=3600' ); } echo wp_json_encode( $document ); } /** * @return string */ private static function currentRequestPath(): string { if ( empty( $_SERVER['REQUEST_URI'] ) ) { return ''; } $uri = (string) wp_unslash( $_SERVER['REQUEST_URI'] ); return self::pathOf( $uri ); } /** * @param string $url Absolute URL, or a path-only value. * @return string */ private static function pathOf( string $url ): string { $path = parse_url( $url, PHP_URL_PATH ); return is_string( $path ) ? $path : ''; } /** * Strip the site's own `home_url()` path prefix from $path, so a request * for a subdirectory-installed site and the endpoint URLs this class * advertises are always compared on the same, site-root-relative basis. * * @param string $path Absolute request or endpoint path. * @return string */ private static function stripHomePath( string $path ): string { $home_path = rtrim( self::pathOf( home_url( '/' ) ), '/' ); if ( '' !== $home_path && 0 === strpos( $path, $home_path ) ) { $path = substr( $path, strlen( $home_path ) ); } return '' === $path ? '/' : $path; } }