$state, 'code_challenge' => $challenge, 'redirect_uri' => admin_url( 'admin-post.php?action=aivastark_callback' ), 'site_url' => home_url(), // Lets the approval page tell a 2.5.1-or-older plugin (no // plugin_version) to update before it connects. 'plugin_version' => AIVASTARK_WIDGET_VERSION, ) ); // External redirect (dashboard origin), so wp_redirect rather than the // host-restricted wp_safe_redirect. wp_redirect( $authorize_url ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect exit; } /** * Finish the pairing: validate state, exchange the code, persist the token. */ public static function callback() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to do this.', 'aivastark-widget' ) ); } $home = admin_url( 'admin.php?page=aivastark' ); // User denied, or the dashboard returned an error. if ( isset( $_GET['error'] ) ) { self::redirect_with( $home, 'denied' ); } $code = isset( $_GET['code'] ) ? sanitize_text_field( wp_unslash( $_GET['code'] ) ) : ''; $state = isset( $_GET['state'] ) ? sanitize_text_field( wp_unslash( $_GET['state'] ) ) : ''; if ( '' === $code || '' === $state ) { self::redirect_with( $home, 'missing' ); } // CSRF: the state must match a verifier we issued to this admin (single-use). $verifier = self::take_state( get_current_user_id(), $state ); if ( '' === $verifier ) { self::redirect_with( $home, 'state' ); } $result = Aivastark_Widget_Api::exchange( $code, $verifier, home_url() ); if ( is_wp_error( $result ) ) { self::redirect_with( $home, 'exchange' ); } Aivastark_Widget_Options::save_pairing( $result, home_url() ); self::redirect_with( $home, '', 'connected' ); } /** * Revoke the token and clear local pairing state. */ public static function disconnect() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to do this.', 'aivastark-widget' ) ); } check_admin_referer( 'aivastark_disconnect' ); Aivastark_Widget_Api::revoke(); // best-effort; ignore failures Aivastark_Widget_Options::clear_pairing(); self::redirect_with( admin_url( 'admin.php?page=aivastark' ), '', 'disconnected' ); } /** * Keep a Connect's verifier for this admin until the callback, or 10 minutes. * * @param int $user_id The admin who clicked Connect. * @param string $state The CSRF state sent to the dashboard. * @param string $verifier The PKCE verifier for that state. */ public static function remember_state( $user_id, $state, $verifier ) { $states = self::live_states( $user_id ); $states[ $state ] = array( 'verifier' => (string) $verifier, 'expires' => time() + self::STATE_TTL, ); $states = array_slice( $states, -self::STATE_MAX, null, true ); update_user_meta( $user_id, self::STATE_META_KEY, $states ); } /** * Return and forget the verifier issued to this admin for $state. * * @param int $user_id The admin returning from the dashboard. * @param string $state The state the dashboard sent back. * @return string The verifier, or '' when the state is unknown, used or expired. */ public static function take_state( $user_id, $state ) { $state = (string) $state; if ( '' === $state ) { return ''; } $verifier = ''; $states = self::live_states( $user_id ); if ( isset( $states[ $state ] ) ) { $verifier = $states[ $state ]['verifier']; unset( $states[ $state ] ); } if ( $states ) { update_user_meta( $user_id, self::STATE_META_KEY, $states ); } else { delete_user_meta( $user_id, self::STATE_META_KEY ); } // A Connect started on 2.5.1 or earlier kept its verifier in a transient. $transient_key = self::STATE_TRANSIENT_PREFIX . $state; $legacy = get_transient( $transient_key ); delete_transient( $transient_key ); if ( '' === $verifier && is_string( $legacy ) ) { $verifier = $legacy; } return $verifier; } /** * This admin's pending Connects that haven't expired, oldest first. * * @param int $user_id The admin. * @return array */ private static function live_states( $user_id ) { $raw = get_user_meta( $user_id, self::STATE_META_KEY, true ); if ( ! is_array( $raw ) ) { return array(); } $now = time(); $live = array(); foreach ( $raw as $state => $row ) { if ( is_array( $row ) && isset( $row['verifier'], $row['expires'] ) && is_string( $row['verifier'] ) && '' !== $row['verifier'] && (int) $row['expires'] > $now ) { $live[ (string) $state ] = $row; } } return $live; } /** * Redirect back to a plugin page with an error/notice query arg, then exit. */ private static function redirect_with( $base, $error = '', $notice = '' ) { $args = array(); if ( $error ) { $args['aivastark_error'] = $error; } if ( $notice ) { $args['aivastark_notice'] = $notice; } wp_safe_redirect( add_query_arg( $args, $base ) ); exit; } }