`). A Bearer header is an OAuth * access token, else the static token; the static token may also come as * `?token=`. OAuth runs as the token's user, the static token as the site's * first administrator. A signed-in administrator with no token is a person, * not an agent. * * AI Engine keeps the result private, so the adapter checks the credential * as AI Engine does, read-only: the token's hash against its OAuth table, * the static token against `mwai_options`. The free plugin has no * WooCommerce tools: its store writes are raw meta, options and posts, so * every write tool is held as unclassified; its content reads are typed. */ final class AiEngineAdapter implements Adapter { /** * Content reads; its option, meta, user and plugin reads are not * listed. */ private const READS = array( 'wp_count_media', 'wp_count_posts', 'wp_count_terms', 'wp_get_comments', 'wp_get_media', 'wp_get_post', 'wp_get_posts', 'wp_get_post_terms', 'wp_get_post_types', 'wp_get_taxonomies', 'wp_get_terms', ); /** * {@inheritDoc} */ public function id(): string { return 'ai-engine'; } /** * {@inheritDoc} */ public function name(): string { return 'AI Engine'; } /** * {@inheritDoc} */ public function plugin_file(): string { return 'ai-engine/ai-engine.php'; } /** * {@inheritDoc} */ public function tested(): array { return array( '3.8.6', '3.9.0' ); } /** * {@inheritDoc} * * @param string $route The route. */ public function serves( string $route ): bool { if ( 1 === preg_match( '#^/mcp/v1/http/?$#i', $route ) ) { return true; } $token = self::static_token(); return '' !== $token && hash_equals( '/mcp/v1/' . $token, rtrim( $route, '/' ) ); } /** * {@inheritDoc} * * @param WP_REST_Request $request The request. */ public function presents_credential( WP_REST_Request $request ): bool { return null !== self::bearer( $request ) || '' !== (string) $request->get_param( 'token' ) || ! str_ends_with( strtolower( rtrim( $request->get_route(), '/' ) ), '/http' ); } /** * {@inheritDoc} * * @param WP_REST_Request $request The request. */ public function principal( WP_REST_Request $request ): ?Principal { $static = self::static_token(); $bearer = self::bearer( $request ); if ( null !== $bearer ) { $oauth = self::oauth( $bearer ); if ( null !== $oauth ) { return $oauth; } return '' !== $static && hash_equals( $static, $bearer ) ? $this->bearer_principal() : null; } if ( '' === $static ) { return null; } $query = (string) $request->get_param( 'token' ); if ( '' !== $query && hash_equals( $static, $query ) ) { return $this->bearer_principal(); } return hash_equals( '/mcp/v1/' . $static, rtrim( $request->get_route(), '/' ) ) ? $this->bearer_principal() : null; } /** * {@inheritDoc} */ public function principals(): array { global $wpdb; $found = array(); if ( '' !== self::static_token() ) { $found[] = array( 'id' => $this->id() . ':bearer', 'label' => __( 'Static bearer token', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), // AI Engine runs it as the site's first administrator. 'runs_as_admin' => true, ); } $table = $wpdb->prefix . 'mwai_mcp_oauth_clients'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- AI Engine's own table, read for the binding screen. if ( $table === $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- The plugin's own table, read for the binding screen. foreach ( (array) $wpdb->get_results( $wpdb->prepare( 'SELECT client_id, client_name FROM %i LIMIT 200', $table ) ) as $client ) { $found[] = array( 'id' => $this->id() . ':oauth:' . $client->client_id, /* translators: %s: OAuth client name. */ 'label' => sprintf( __( 'Connected app "%s"', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), (string) $client->client_name ), // An administrator, or an editor with Editor Access on. 'runs_as_admin' => null, ); } } return $found; } /** * {@inheritDoc} * * @param string $tool Tool name. * @param array $arguments Arguments. */ public function classify( string $tool, array $arguments ): ?Classification { return ToolMap::read( self::READS, $tool ); } /** * The static token as the site's first administrator. */ private function bearer_principal(): ?Principal { $admins = get_users( array( 'role' => 'administrator', 'number' => 1, 'fields' => 'ID', ) ); return array() === $admins ? null : new Principal( $this->id() . ':bearer', __( 'Static bearer token', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), (int) $admins[0] ); } /** * An OAuth access token's client and user, read as AI Engine reads it. * * @param string $token The token. */ private function oauth( string $token ): ?Principal { global $wpdb; $tokens = $wpdb->prefix . 'mwai_mcp_oauth_tokens'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- AI Engine's own table. if ( $tokens !== $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $tokens ) ) ) ) { return null; } $row = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- AI Engine's own table, read-only. $wpdb->prepare( // Revoked is 1; rotated grants still serve their token, as in AI Engine. 'SELECT client_id, user_id, access_expires FROM %i WHERE access_token_hash = %s AND revoked <> 1 LIMIT 1', $tokens, hash( 'sha256', $token ) ), ARRAY_A ); if ( ! is_array( $row ) || strtotime( $row['access_expires'] . ' UTC' ) < time() || ! self::may_authorize( (int) $row['user_id'] ) ) { return null; } return new Principal( $this->id() . ':oauth:' . $row['client_id'], (string) $row['client_id'], (int) $row['user_id'] ); } /** * Whether AI Engine still lets the token's user connect, as its * `user_can_authorize()` decides: an administrator, an editor when Editor * Access is on, and its filter. Checked here because the gate signs the * user in first, and AI Engine skips its own check for a signed-in * administrator. * * @param int $user_id The token's user. */ private static function may_authorize( int $user_id ): bool { $options = get_option( 'mwai_options', array() ); $allowed = $user_id > 0 && user_can( $user_id, 'manage_options' ); if ( ! $allowed && $user_id > 0 && is_array( $options ) && ! empty( $options['mcp_oauth_editors'] ) ) { $allowed = user_can( $user_id, 'edit_others_posts' ); } return (bool) apply_filters( 'mwai_mcp_oauth_user_can_authorize', $allowed, $user_id ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- AI Engine's own filter, applied as AI Engine applies it. } /** * AI Engine's static token, or empty. */ private static function static_token(): string { $options = get_option( 'mwai_options', array() ); return is_array( $options ) ? (string) ( $options['mcp_bearer_token'] ?? '' ) : ''; } /** * The Bearer credential, or null. * * @param WP_REST_Request $request The request. */ private static function bearer( WP_REST_Request $request ): ?string { $header = (string) $request->get_header( 'Authorization' ); return 1 === preg_match( '/Bearer\s+(.+)/i', $header, $match ) ? trim( $match[1] ) : null; } }