|null $adapters Adapters; the launch set by default. * @param (Closure(Adapter): string)|null $status Adapter state; Adapters::status() by default. * @param (Closure(int): string)|null $bind Agent binding; the parent's by default. */ public function __construct( private readonly PathDBindings $bindings, private readonly ?array $adapters = null, ?Closure $status = null, ?Closure $bind = null ) { $this->status = $status ?? static fn( Adapter $adapter ): string => Adapters::status( $adapter ); $this->bind = $bind ?? static fn( int $agent_id ): string => Api::bind_agent( $agent_id ); } /** * Hook in before the firewall's REST gatekeeper. */ public function register(): void { add_filter( 'rest_pre_dispatch', array( $this, 'identify' ), 5, 3 ); add_filter( 'adaf_unclaimed_credential', array( $this, 'unclaimed' ), 10, 1 ); add_filter( 'rest_request_after_callbacks', array( $this, 'finish' ), PHP_INT_MAX, 3 ); add_filter( 'rest_post_dispatch', array( $this, 'finish' ), PHP_INT_MAX, 3 ); } /** * The outermost request is done: the next one is identified afresh. * * @param mixed $response Response. * @param mixed $handler Handler or server (unused). * @param mixed $request The request. * @return mixed The response, unchanged. */ public function finish( $response, $handler, $request ) { unset( $handler ); if ( $request === $this->owner ) { $this->owner = null; } return $response; } /** * Forget everything: a new process-level request (tests, WP-CLI). */ public function reset(): void { $this->owner = null; $this->current = null; $this->unclaimed = false; } /** * Bind the agent of the credential an in-site MCP plugin will accept. * * @param mixed $result Dispatch result so far. * @param mixed $server REST server (unused). * @param WP_REST_Request $request The request. * @return mixed The result, or a 401 for a refused binding. */ public function identify( $result, $server, $request ) { unset( $server ); if ( ! $request instanceof WP_REST_Request ) { return $result; } // A request nested inside the one being dispatched (rest_do_request // from a tool): what the outer request was identified as stands - // an unclaimed credential stays unclaimed for every write it makes. if ( null !== $this->owner && $this->owner !== $request ) { return $result; } $this->owner = $request; $this->current = null; $this->unclaimed = false; if ( null !== $result || null !== Api::current_agent() ) { return $result; } $adapter = $this->adapter_for( $request->get_route() ); if ( null === $adapter ) { return $result; } if ( Adapters::COVERED !== $this->status_of( $adapter ) ) { $this->unclaimed = $adapter->presents_credential( $request ); return $result; } $principal = $adapter->principal( $request ); if ( null === $principal ) { // Not a credential the plugin accepts: it refuses the request. return $result; } if ( get_current_user_id() !== $principal->user_id ) { // The user the plugin itself is about to run it as. wp_set_current_user( $principal->user_id ); } return $this->bind_principal( $adapter, $principal, $result ); } /** * Tell the parent this request's credential is unclaimed. * * @param mixed $unclaimed Whether an unclaimed credential is in use. * @return mixed */ public function unclaimed( $unclaimed ) { return $this->unclaimed ? true : $unclaimed; } /** * The adapter governing this request's agent, or null. */ public function current(): ?Adapter { return $this->current; } /** * Bind the principal's agent. * * @param Adapter $adapter The plugin's adapter. * @param Principal $principal Who it authenticated. * @param mixed $result Dispatch result so far. * @return mixed */ private function bind_principal( Adapter $adapter, Principal $principal, $result ) { $agent = $this->bindings->agent_for( $principal->id ); if ( null === $agent ) { $this->unclaimed = true; return $result; } switch ( ( $this->bind )( $agent ) ) { case 'bound': $this->current = $adapter; return $result; case 'mismatch': case 'inactive': case 'privileged': return new WP_Error( 'adafwc_mcp_binding_refused', __( 'This MCP credential is bound to an agent that is disabled, linked to another user, or linked to an administrator without being confirmed as one. Ask a site administrator to fix the binding.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), array( 'status' => 401 ) ); default: // A deleted agent: the credential is unbound again. $this->unclaimed = true; return $result; } } /** * The adapter whose plugin serves a route, or null. * * @param string $route The route. */ private function adapter_for( string $route ): ?Adapter { foreach ( $this->adapters ?? Adapters::all() as $adapter ) { if ( $adapter->serves( $route ) && Adapters::ABSENT !== $this->status_of( $adapter ) ) { return $adapter; } } return null; } /** * An adapter's state. * * @param Adapter $adapter The adapter. */ private function status_of( Adapter $adapter ): string { return ( $this->status )( $adapter ); } }