Codexta_Connect::site_uuid(), 'aud' => 'codexta', ); $user = wp_get_current_user(); if ( $user->exists() ) { $claims['sub'] = (string) $user->ID; $claims['email'] = $user->user_email; } $cart = self::cart_token(); if ( '' !== $cart ) { $claims['cart'] = $cart; } $claims['checkout'] = wc_get_checkout_url(); $claims['cartUrl'] = wc_get_cart_url(); $claims['locale'] = get_locale(); $claims['iat'] = $now; $claims['exp'] = $now + self::TTL; $claims['jti'] = bin2hex( random_bytes( 16 ) ); return self::sign( $claims, $secret ); } /** * The single-use token of the "Open Codexta" button: who the signed-in staff member is and what they may do, valid for a minute. * Codexta accepts it once, at its admin page only (aud codexta-admin), and answers with a session of its own. Empty when not connected. * * @return string */ public static function launch() { $secret = Codexta_Connect::shared_secret(); $user = wp_get_current_user(); if ( '' === $secret || ! $user->exists() ) { return ''; } $now = time(); return self::sign( array( 'iss' => Codexta_Connect::site_uuid(), 'aud' => 'codexta-admin', 'sub' => (string) $user->ID, 'email' => $user->user_email, 'caps' => array_values( array_filter( array( 'manage_options', 'manage_woocommerce' ), array( $user, 'has_cap' ) ) ), 'iat' => $now, 'exp' => $now + 60, 'jti' => bin2hex( random_bytes( 16 ) ), ), $secret ); } /** * An HS256 JWT of the claims. * * @param array $claims Claims. * @param string $secret The shared secret. * @return string */ private static function sign( $claims, $secret ) { $signing = self::b64( wp_json_encode( array( 'alg' => 'HS256', 'typ' => 'JWT' ) ) ) . '.' . self::b64( wp_json_encode( $claims ) ); return $signing . '.' . self::b64( hash_hmac( 'sha256', $signing, $secret, true ) ); } /** * The Store API Cart-Token of the visitor's own WooCommerce session, so the agent edits the cart the browser shows. * * @return string */ private static function cart_token() { if ( ! function_exists( 'WC' ) || ! WC()->session instanceof WC_Session_Handler ) { return ''; } // A guest has no session until something is in the cart: start one so the token and the browser cookie name the same cart. if ( ! WC()->session->has_session() ) { WC()->session->set_customer_session_cookie( true ); } $customer_id = (string) WC()->session->get_customer_id(); if ( '' === $customer_id ) { return ''; } if ( class_exists( '\Automattic\WooCommerce\StoreApi\Utilities\CartTokenUtils' ) ) { return \Automattic\WooCommerce\StoreApi\Utilities\CartTokenUtils::get_cart_token( $customer_id ); } // WooCommerce before CartTokenUtils: the same JWT built by JsonWebToken. if ( class_exists( '\Automattic\WooCommerce\StoreApi\Utilities\JsonWebToken' ) ) { return \Automattic\WooCommerce\StoreApi\Utilities\JsonWebToken::create( array( 'user_id' => $customer_id, 'exp' => time() + intval( apply_filters( 'wc_session_expiration', DAY_IN_SECONDS * 2 ) ), // phpcs:ignore WooCommerce.Commenting.CommentHooks, WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WooCommerce's own hook. 'iss' => 'store-api', ), '@' . wp_salt() ); } return ''; } /** * Base64url without padding. * * @param string $data Raw bytes. * @return string */ private static function b64( $data ) { return rtrim( strtr( base64_encode( $data ), '+/', '-_' ), '=' ); } }