WP_REST_Server::CREATABLE, 'callback' => array( $this, 'handle' ), 'permission_callback' => '__return_true', ) ); } /** * @param WP_REST_Request $request Current request. * @return WP_REST_Response */ public function handle( WP_REST_Request $request ): WP_REST_Response { if ( ! McpTransportSecurity::isAllowed() ) { return self::addNoStoreHeaders( new WP_REST_Response( array(), 403 ) ); } $rate = AtomicRateLimiter::checkAndIncrement( McpOAuthConstants::tokenRateLimitBucketKey( self::clientIpHash() ), McpOAuthConstants::TOKEN_RATE_LIMIT_PER_IP, McpOAuthConstants::TOKEN_RATE_LIMIT_WINDOW ); if ( ! $rate['allowed'] ) { $response = new WP_REST_Response( array( 'error' => 'invalid_request', 'error_description' => __( 'Too many requests. Please slow down.', 'ai-product-tools' ), ), 429 ); $response->header( 'Retry-After', (string) max( 1, (int) $rate['reset_in'] ) ); return self::addNoStoreHeaders( $response ); } try { $this->revoke( self::parseBody( $request ) ); } catch ( Throwable $exception ) { } return self::addNoStoreHeaders( new WP_REST_Response( new \stdClass(), 200 ) ); } /** * @param array $body Parsed request body. * @return void */ private function revoke( array $body ): void { $raw_token = isset( $body['token'] ) ? (string) $body['token'] : ''; $client_id = isset( $body['client_id'] ) ? (string) $body['client_id'] : ''; if ( '' === $raw_token ) { return; } $token_store = new TokenStore(); $token = $token_store->findByHash( TokenHasher::hash( $raw_token ) ); if ( null === $token ) { return; } if ( ! in_array( $token['token_type'], array( TokenStore::TOKEN_TYPE_OAUTH_ACCESS, TokenStore::TOKEN_TYPE_OAUTH_REFRESH ), true ) ) { return; } if ( '' !== $client_id && ! self::clientMatchesConnection( $client_id, (int) $token['connection_id'] ) ) { return; } $now = gmdate( 'Y-m-d H:i:s' ); if ( TokenStore::TOKEN_TYPE_OAUTH_REFRESH === $token['token_type'] ) { $this->revokeWholeConnection( (int) $token['connection_id'], $now ); return; } if ( $token_store->revoke( (int) $token['id'], TokenStore::REVOKE_REASON_REVOKED, $now ) ) { ( new AuditLogStore() )->write( array( 'connection_id' => (int) $token['connection_id'], 'event_type' => AuditLogStore::EVENT_TOKEN_REVOKED, 'outcome' => AuditLogStore::OUTCOME_SUCCESS, 'now' => $now, ) ); } } /** * @param string $clientId Presented client_id. * @param int $connectionId Connection the token belongs to. * @return bool Whether $clientId is the OAuth client that owns $connectionId. */ private static function clientMatchesConnection( string $clientId, int $connectionId ): bool { $connection = ( new ConnectionStore() )->find( $connectionId ); if ( null === $connection ) { return false; } $client = ( new OAuthClientStore() )->findByClientId( $clientId ); return null !== $client && (int) $client['id'] === (int) $connection['oauth_client_id']; } /** * @param int $connectionId Connection to revoke. * @param string $now Current UTC datetime. * @return void */ private function revokeWholeConnection( int $connectionId, string $now ): void { $revoked = ( new ConnectionStore() )->revoke( $connectionId, ConnectionStore::REVOKED_REASON_USER, 0, $now ); ( new TokenStore() )->revokeForConnection( $connectionId, TokenStore::REVOKE_REASON_CONNECTION_REVOKED, $now ); if ( $revoked ) { ( new AuditLogStore() )->write( array( 'connection_id' => $connectionId, 'event_type' => AuditLogStore::EVENT_CONNECTION_REVOKED, 'outcome' => AuditLogStore::OUTCOME_SUCCESS, 'now' => $now, ) ); } } /** * @param WP_REST_Request $request Current request. * @return array */ private static function parseBody( WP_REST_Request $request ): array { $json = $request->get_json_params(); if ( is_array( $json ) && ! empty( $json ) ) { return $json; } $body_params = $request->get_body_params(); if ( is_array( $body_params ) && ! empty( $body_params ) ) { return $body_params; } $params = $request->get_params(); return is_array( $params ) ? $params : array(); } /** * @param WP_REST_Response $response Response to decorate. * @return WP_REST_Response */ /** * Hashed requester IP for the rate-limit bucket ('' when unknown). * * @return string */ private static function clientIpHash(): string { if ( empty( $_SERVER['REMOTE_ADDR'] ) ) { return ''; } $ip = sanitize_text_field( wp_unslash( (string) $_SERVER['REMOTE_ADDR'] ) ); if ( ! filter_var( $ip, FILTER_VALIDATE_IP ) ) { return ''; } return hash_hmac( 'sha256', $ip, (string) wp_salt( 'auth' ) ); } private static function addNoStoreHeaders( WP_REST_Response $response ): WP_REST_Response { $response->header( 'Cache-Control', 'no-store' ); $response->header( 'Pragma', 'no-cache' ); $response->header( 'Access-Control-Allow-Origin', '*' ); return $response; } }