*/ public static function roles(): array { require_once ABSPATH . 'wp-admin/includes/user.php'; $roles = array(); foreach ( get_editable_roles() as $slug => $role ) { if ( Privilege::role( (array) $role['capabilities'] ) ) { continue; } $roles[] = array( 'slug' => (string) $slug, 'name' => translate_user_role( (string) $role['name'] ), ); } return $roles; } /** * Make or pick the user, give it an application password, create the * agent and bind the password to it. * * @param string $name The agent's name. * @param int|null $wp_user_id An existing user to link, or null to make one. * @param string $login The new user's login, when making one. * @param string $role The new user's role, when making one. * @param string $email The new user's email, optional. * @return array{agent: Agent, login: string, password: string}|WP_Error */ public function connect( string $name, ?int $wp_user_id, string $login, string $role, string $email ): array|WP_Error { if ( ! wp_is_application_passwords_available() ) { return self::error( 'adaf_app_passwords_unavailable', __( 'Application passwords are switched off on this site. WordPress offers them only over HTTPS, unless a plugin or the site configuration turned them off. Use a token for this agent instead, from the Agents screen.', 'agentic-daisy-ai-agent-firewall' ), 400 ); } $user = null === $wp_user_id ? $this->new_user( $name, $login, $role, $email ) : self::existing_user( $wp_user_id ); if ( $user instanceof WP_Error ) { return $user; } $created_user = null === $wp_user_id; if ( ! wp_is_application_passwords_available_for_user( $user ) ) { self::undo( $user->ID, $created_user, null ); return self::error( 'adaf_app_passwords_unavailable_for_user', __( 'WordPress does not allow application passwords for this user. Pick another user, or use a token for this agent instead.', 'agentic-daisy-ai-agent-firewall' ), 400 ); } $made = \WP_Application_Passwords::create_new_application_password( $user->ID, array( /* translators: %s: agent name. */ 'name' => sprintf( __( 'Agent Firewall: %s', 'agentic-daisy-ai-agent-firewall' ), $name ), ) ); if ( $made instanceof WP_Error ) { self::undo( $user->ID, $created_user, null ); return $made; } list( $password, $item ) = $made; $uuid = (string) $item['uuid']; // Created already bound, in one insert: there is no moment, and no // second write to fail, at which the agent exists without its // password or the password without its agent. $agent = $this->agents->create( $this->unique_slug( $name ), $name, $user->ID, null, new DateTimeImmutable( 'now' ), array(), null, $uuid ); if ( 0 === $agent->id ) { self::undo( $user->ID, $created_user, $uuid ); return self::error( 'adaf_agent_not_created', __( 'The agent could not be saved, so the user and password made for it were removed again. If the plugin was just updated, reload the page so its database upgrade can finish, then try again.', 'agentic-daisy-ai-agent-firewall' ), 500 ); } return array( 'agent' => $agent, 'login' => $user->user_login, 'password' => $password, ); } /** * An existing user the agent may be linked to. * * @param int $wp_user_id User id. */ private static function existing_user( int $wp_user_id ): \WP_User|WP_Error { $user = get_user_by( 'id', $wp_user_id ); if ( ! $user instanceof \WP_User ) { return self::error( 'adaf_invalid_user', __( 'The linked WordPress user does not exist.', 'agentic-daisy-ai-agent-firewall' ), 400 ); } // What core's own application password screen asks: minting a // credential for a user is editing that user. if ( ! current_user_can( 'edit_user', $user->ID ) ) { return self::error( 'adaf_cannot_edit_user', __( 'You are not allowed to edit that user, so you cannot give it an application password.', 'agentic-daisy-ai-agent-firewall' ), 403 ); } // A network's users span its sites, and an application password signs // in on every one of them: only a member of this site may be linked. if ( is_multisite() && ! is_user_member_of_blog( $user->ID ) ) { return self::error( 'adaf_user_not_on_site', __( 'That user is not a member of this site. Add them to this site first, or make a new user for the agent.', 'agentic-daisy-ai-agent-firewall' ), 400 ); } if ( Privilege::user( $user->ID ) ) { return self::error( 'adaf_privileged_user', __( 'Agents cannot be linked to an administrator, or to a user who could make themselves one. An agent inherits its user\'s capabilities, which would let it manage the firewall itself. Pick a user with only the capabilities the agent needs, or make a new one.', 'agentic-daisy-ai-agent-firewall' ), 400 ); } return $user; } /** * Make the agent's user, after checking everything that could refuse it. * * @param string $name Agent name, used as the display name. * @param string $login Login. * @param string $role Role slug. * @param string $email Email, or empty. */ private function new_user( string $name, string $login, string $role, string $email ): \WP_User|WP_Error { if ( '' === $login ) { return self::error( 'adaf_missing_user', __( 'Pick an existing user for the agent, or give a username for a new one.', 'agentic-daisy-ai-agent-firewall' ), 400 ); } if ( ! current_user_can( 'create_users' ) ) { return self::error( 'adaf_cannot_create_users', __( 'You are not allowed to create users on this site. Pick an existing user for the agent instead.', 'agentic-daisy-ai-agent-firewall' ), 403 ); } if ( ! in_array( $role, array_column( self::roles(), 'slug' ), true ) ) { return self::error( 'adaf_role_not_allowed', __( 'That role cannot be given to an agent\'s user. Roles that can manage the site\'s options, or reach that by promoting or editing users or by installing or editing plugins, are never offered: an agent inherits its user\'s capabilities.', 'agentic-daisy-ai-agent-firewall' ), 400 ); } if ( sanitize_user( $login, true ) !== $login || ! validate_username( $login ) ) { return self::error( 'adaf_invalid_login', __( 'That username is not allowed. Use letters, numbers, spaces and the characters _ . - @ only.', 'agentic-daisy-ai-agent-firewall' ), 400 ); } // WordPress signs in an email-shaped login by the email address // first, so it could resolve to a different user than this one. if ( is_email( $login ) ) { return self::error( 'adaf_email_login', __( 'Use a username that is not an email address.', 'agentic-daisy-ai-agent-firewall' ), 400 ); } if ( false !== username_exists( $login ) ) { return self::error( 'adaf_login_taken', __( 'That username is taken. Choose another, or pick the existing user instead.', 'agentic-daisy-ai-agent-firewall' ), 409 ); } if ( '' !== $email && ! is_email( $email ) ) { return self::error( 'adaf_invalid_email', __( 'That email address is not valid.', 'agentic-daisy-ai-agent-firewall' ), 400 ); } if ( '' !== $email && false !== email_exists( $email ) ) { return self::error( 'adaf_email_taken', __( 'Another user already has that email address.', 'agentic-daisy-ai-agent-firewall' ), 409 ); } // The user signs in only through its application password, so its // login password is long, random and never shown to anyone. $id = wp_insert_user( array( 'user_login' => $login, 'user_pass' => wp_generate_password( 64, true, true ), 'user_email' => $email, 'display_name' => $name, 'role' => $role, ) ); $user = $id instanceof WP_Error ? false : get_user_by( 'id', $id ); if ( ! $user instanceof \WP_User ) { return self::error( 'adaf_user_not_created', __( 'WordPress could not save the new user, so nothing was created. Try again, or pick an existing user.', 'agentic-daisy-ai-agent-firewall' ), 500 ); } return $user; } /** * Remove what a failed connect made: the password, and the user when * this call created it. An existing user is never deleted. * * @param int $wp_user_id User id. * @param bool $created_user Whether this call made the user. * @param string|null $uuid The password made for it, if any. */ private static function undo( int $wp_user_id, bool $created_user, ?string $uuid ): void { if ( null !== $uuid ) { \WP_Application_Passwords::delete_application_password( $wp_user_id, $uuid ); } if ( ! $created_user ) { return; } require_once ABSPATH . 'wp-admin/includes/user.php'; if ( is_multisite() ) { require_once ABSPATH . 'wp-admin/includes/ms.php'; wpmu_delete_user( $wp_user_id ); return; } wp_delete_user( $wp_user_id ); } /** * A slug from the agent's name, unique among agents. * * @param string $name Agent name. */ private function unique_slug( string $name ): string { $base = trim( (string) preg_replace( '/[^a-z0-9]+/', '-', strtolower( remove_accents( $name ) ) ), '-' ); $base = '' === $base ? 'agent' : substr( $base, 0, self::SLUG_MAX - 4 ); $slug = $base; $n = 2; while ( null !== $this->agents->find_by_key( $slug ) ) { $slug = $base . '-' . $n; ++$n; } return $slug; } /** * A WP_Error carrying an HTTP status. * * @param string $code Error code. * @param string $message Message. * @param int $status HTTP status. */ private static function error( string $code, string $message, int $status ): WP_Error { return new WP_Error( $code, $message, array( 'status' => $status ) ); } }