'array', 'sanitize_callback' => array( __CLASS__, 'sanitize_options' ), 'default' => agentid_sso_default_options(), ) ); } /** * Roles that an agent account may be provisioned with. * * Any role granting a site-administering capability — the set in * AgentID_Guards::hard_denied_caps(): manage_options, plugin and * theme management, user promotion, unfiltered HTML, store * management (e.g. WooCommerce), and the rest — is refused: an agent identity must never be able to * administer the site, even between sign-ins. * * @return array role slug => label */ public static function provisionable_roles() { $roles = array(); $denied = AgentID_Guards::hard_denied_caps(); foreach ( wp_roles()->roles as $slug => $role ) { $caps = isset( $role['capabilities'] ) && is_array( $role['capabilities'] ) ? $role['capabilities'] : array(); foreach ( $denied as $denied_cap ) { if ( ! empty( $caps[ $denied_cap ] ) ) { continue 2; } } $roles[ $slug ] = translate_user_role( $role['name'] ); } return $roles; } /** * A fallback provisioning role, always validated against the * provisionable set: the least-privileged of the stock roles an * agent may hold, or null when none of them is available (a role * editor granted Subscriber a hard-denied capability, or the * role was removed). Callers must treat null as "no safe role * exists" rather than inventing one. * * @return string|null */ public static function fallback_role() { $roles = self::provisionable_roles(); foreach ( array( 'subscriber', 'contributor', 'author' ) as $candidate ) { if ( array_key_exists( $candidate, $roles ) ) { return $candidate; } } return null; } /** * Sanitize the options array. * * @param mixed $input Raw input. * @return array */ public static function sanitize_options( $input ) { $existing = get_option( 'agentid_sso_options', array() ); if ( ! is_array( $existing ) ) { $existing = array(); } if ( ! is_array( $input ) ) { $input = array(); } $out = agentid_sso_default_options(); // The Settings API hands this callback values that options.php // has already unslashed, so do NOT unslash again (a second pass // eats literal backslashes). Credentials are opaque byte // strings: sanitize_text_field() would also strip percent // sequences and tags from them, silently changing a valid // secret into a wrong one. Accept strings verbatim apart from // surrounding whitespace, which is never part of a credential. $out['client_id'] = isset( $input['client_id'] ) && is_string( $input['client_id'] ) ? trim( $input['client_id'], " \t\n\r\0\x0B" ) : ''; // An empty secret field means "keep the stored secret". $submitted_secret = isset( $input['client_secret'] ) && is_string( $input['client_secret'] ) ? trim( $input['client_secret'], " \t\n\r\0\x0B" ) : ''; if ( '' !== $submitted_secret ) { $out['client_secret'] = $submitted_secret; } elseif ( ! empty( $input['clear_secret'] ) ) { $out['client_secret'] = ''; } else { $out['client_secret'] = isset( $existing['client_secret'] ) ? (string) $existing['client_secret'] : ''; } $scopes = isset( $input['scopes'] ) && is_string( $input['scopes'] ) ? sanitize_text_field( $input['scopes'] ) : ''; $parts = array_filter( array_map( 'trim', explode( ' ', $scopes ) ) ); if ( ! in_array( 'openid', $parts, true ) ) { array_unshift( $parts, 'openid' ); } $allowed_scopes = array( 'openid', 'email', 'profile', 'owner_profile', 'owner_email' ); $parts = array_values( array_intersect( $parts, $allowed_scopes ) ); $out['scopes'] = implode( ' ', $parts ); $role = isset( $input['default_role'] ) && is_string( $input['default_role'] ) ? sanitize_key( $input['default_role'] ) : ''; if ( ! array_key_exists( $role, self::provisionable_roles() ) ) { // Never substitute an unvalidated role: use the validated // fallback, or keep the requested value when no safe role // exists at all (provisioning then fails with a clear // error instead of assigning a rejected role). $fallback = self::fallback_role(); if ( null !== $fallback ) { $role = $fallback; } } $out['default_role'] = $role; $out['link_by_owner_email'] = ! empty( $input['link_by_owner_email'] ) ? 1 : 0; $out['show_login_button'] = ! empty( $input['show_login_button'] ) ? 1 : 0; $hours = isset( $input['session_hours'] ) ? absint( $input['session_hours'] ) : 12; $out['session_hours'] = max( 1, min( 72, $hours ) ); // Configuration changed: drop cached discovery / JWKS. delete_transient( 'agentid_sso_discovery' ); delete_transient( 'agentid_sso_jwks' ); return $out; } /** * Enqueue admin assets on our page only. * * @param string $hook Current admin page hook. */ public static function enqueue( $hook ) { if ( 'settings_page_' . self::PAGE_SLUG !== $hook ) { return; } wp_enqueue_script( 'agentid-sso-admin', AGENTID_SSO_URL . 'assets/js/admin.js', array(), AGENTID_SSO_VERSION, true ); wp_localize_script( 'agentid-sso-admin', 'agentidSsoAdmin', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'agentid_test_connection' ), 'testing' => __( 'Testing…', 'agentkey-sign-in-agentid' ), 'copied' => __( 'Copied', 'agentkey-sign-in-agentid' ), 'copy' => __( 'Copy', 'agentkey-sign-in-agentid' ), 'copyFailed' => __( 'Copy failed — select the text and copy it manually.', 'agentkey-sign-in-agentid' ), 'testFailed' => __( 'Test failed.', 'agentkey-sign-in-agentid' ), ) ); } /** * Notice when the plugin is active but not configured. */ public static function not_configured_notice() { if ( ! current_user_can( 'manage_options' ) || agentid_sso_is_configured() ) { return; } $screen = get_current_screen(); if ( $screen && 'settings_page_' . self::PAGE_SLUG === $screen->id ) { return; } printf( '

%s %s

', esc_html__( 'AgentKey Sign-In for AgentID is active but no AgentID application is connected yet.', 'agentkey-sign-in-agentid' ), esc_url( admin_url( 'options-general.php?page=' . self::PAGE_SLUG ) ), esc_html__( 'Finish setup', 'agentkey-sign-in-agentid' ) ); } /** * AJAX: test discovery + JWKS against the live issuer. */ public static function ajax_test_connection() { check_ajax_referer( 'agentid_test_connection' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Not allowed.', 'agentkey-sign-in-agentid' ) ), 403 ); } $discovery = AgentID_OIDC::discovery( true ); if ( is_wp_error( $discovery ) ) { wp_send_json_error( array( 'message' => $discovery->get_error_message() ) ); } $jwks = AgentID_OIDC::jwks( true, $discovery ); if ( is_wp_error( $jwks ) ) { wp_send_json_error( array( 'message' => $jwks->get_error_message() ) ); } wp_send_json_success( array( 'message' => sprintf( /* translators: 1: issuer, 2: number of signing keys */ __( 'Connected. Issuer %1$s responded and published %2$d signing key(s).', 'agentkey-sign-in-agentid' ), $discovery['issuer'], count( $jwks['keys'] ) ), 'issuer' => $discovery['issuer'], 'keys' => count( $jwks['keys'] ), ) ); } /** * Render the settings page: a 3-step setup wizard plus options. */ public static function render_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $options = get_option( 'agentid_sso_options', array() ); $has_secret = is_array( $options ) && ! empty( $options['client_secret'] ); ?>

agentid.com · github.com/agentmail-to

  1. console.agentid.com