WP_REST_Server::READABLE, 'callback' => array( $this, 'list' ), 'permission_callback' => array( self::class, 'allowed' ), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'bind' ), 'permission_callback' => array( self::class, 'allowed' ), 'args' => array( 'key_id' => array( 'type' => 'integer', 'minimum' => 1, 'required' => true, ), 'agent_id' => array( 'type' => 'integer', 'minimum' => 1, 'required' => true, ), ), ), ) ); register_rest_route( self::NAMESPACE, '/key-bindings/(?P\d+)', array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'unbind' ), 'permission_callback' => array( self::class, 'allowed' ), ) ); } /** * Administrators, never an agent. */ public static function allowed(): bool { return current_user_can( 'manage_options' ) && null === Api::current_agent(); } /** * The store's keys and the agent each is bound to. */ public function list(): WP_REST_Response { $bindings = KeyBindings::all(); $keys = array(); foreach ( KeyBindings::keys() as $key ) { $agent = isset( $bindings[ $key['key_id'] ] ) ? Api::agent( $bindings[ $key['key_id'] ] ) : null; $keys[] = $key + array( 'agent' => null === $agent ? null : array( 'id' => $agent['id'], 'name' => $agent['name'], 'status' => $agent['status'], ), ); } return new WP_REST_Response( $keys, 200 ); } /** * Bind a key to an agent linked to the key's own user. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function bind( $request ) { $key_id = (int) $request->get_param( 'key_id' ); $agent_id = (int) $request->get_param( 'agent_id' ); $key = KeyBindings::key( $key_id ); if ( null === $key ) { return new WP_Error( 'adafwc_key_not_found', __( 'There is no WooCommerce API key with that id.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), array( 'status' => 404 ) ); } $agent = Api::agent( $agent_id ); if ( null === $agent ) { return new WP_Error( 'adafwc_agent_not_found', __( 'There is no agent with that id.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), array( 'status' => 404 ) ); } // The key authenticates as its owner: only an agent linked to that // user can act through it, as only that user's application // passwords can be bound to their agent. if ( $agent['user_id'] !== $key['user_id'] ) { return new WP_Error( 'adafwc_key_user_mismatch', __( 'This key belongs to another user than the agent is linked to. Bind it to an agent linked to the key\'s user.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), array( 'status' => 400 ) ); } $bound = $this->bindings->agent_for( $key_id ); if ( null !== $bound && $bound !== $agent_id ) { return new WP_Error( 'adafwc_key_already_bound', __( 'This key is already bound to another agent. Unbind it first.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), array( 'status' => 409 ) ); } $this->bindings->bind( $key_id, $agent_id ); return new WP_REST_Response( array( 'key_id' => $key_id, 'agent_id' => $agent_id, ), 200 ); } /** * Unbind a key. * * @param WP_REST_Request $request Request. */ public function unbind( $request ): WP_REST_Response { $this->bindings->unbind( (int) $request->get_param( 'key_id' ) ); return new WP_REST_Response( null, 204 ); } }