AgenticCheckoutUtils.php
4 months ago
ArrayUtils.php
2 years ago
CartController.php
1 month ago
CartTokenUtils.php
1 year ago
CheckoutTrait.php
1 month ago
DraftOrderTrait.php
1 year ago
JsonWebToken.php
11 months ago
LocalPickupUtils.php
5 months ago
NoticeHandler.php
1 year ago
OrderAuthorizationTrait.php
6 months ago
OrderController.php
1 month ago
Pagination.php
2 years ago
PaymentUtils.php
1 year ago
ProductItemTrait.php
1 month ago
ProductLinksTrait.php
3 months ago
ProductQuery.php
2 months ago
ProductQueryFilters.php
11 months ago
QuantityLimits.php
11 months ago
RateLimits.php
1 year ago
SanitizationUtils.php
2 years ago
ValidationUtils.php
2 years ago
CartTokenUtils.php
84 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Cart token utility functions. |
| 4 | */ |
| 5 | |
| 6 | declare(strict_types=1); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\StoreApi\Utilities; |
| 9 | |
| 10 | use Automattic\WooCommerce\StoreApi\Authentication; |
| 11 | use Automattic\WooCommerce\StoreApi\Utilities\JsonWebToken; |
| 12 | |
| 13 | /** |
| 14 | * Cart token utility functions. |
| 15 | */ |
| 16 | class CartTokenUtils { |
| 17 | /** |
| 18 | * Generate a cart token. |
| 19 | * |
| 20 | * @param string $customer_id The customer ID. |
| 21 | * @return string |
| 22 | */ |
| 23 | public static function get_cart_token( string $customer_id ): string { |
| 24 | return JsonWebToken::create( |
| 25 | array( |
| 26 | 'user_id' => $customer_id, |
| 27 | 'exp' => self::get_cart_token_expiration(), |
| 28 | 'iss' => 'store-api', |
| 29 | ), |
| 30 | self::get_cart_token_secret() |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Validate the cart token. |
| 36 | * |
| 37 | * @param string $cart_token The cart token. |
| 38 | * @return bool |
| 39 | */ |
| 40 | public static function validate_cart_token( string $cart_token ): bool { |
| 41 | return JsonWebToken::validate( $cart_token, self::get_cart_token_secret() ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the cart token payload. |
| 46 | * |
| 47 | * @param string $cart_token The cart token. |
| 48 | * @return array |
| 49 | */ |
| 50 | public static function get_cart_token_payload( string $cart_token ): array { |
| 51 | $parts = JsonWebToken::get_parts( $cart_token )->payload; |
| 52 | |
| 53 | return array( |
| 54 | 'user_id' => $parts->user_id ?? '', |
| 55 | 'exp' => $parts->exp ?? 0, |
| 56 | 'iss' => $parts->iss ?? '', |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get the cart token secret. |
| 62 | * |
| 63 | * @return string |
| 64 | */ |
| 65 | private static function get_cart_token_secret(): string { |
| 66 | return '@' . wp_salt(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Gets the expiration of the cart token. Defaults to 48h. |
| 71 | * |
| 72 | * @return int |
| 73 | */ |
| 74 | private static function get_cart_token_expiration(): int { |
| 75 | /** |
| 76 | * Filters the session expiration. |
| 77 | * |
| 78 | * @since 5.0.0 |
| 79 | * @param int $expiration Expiration in seconds. |
| 80 | */ |
| 81 | return time() + intval( apply_filters( 'wc_session_expiration', DAY_IN_SECONDS * 2 ) ); |
| 82 | } |
| 83 | } |
| 84 |