| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Order\Services; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Signed key that lets a guest open their own order without an account. |
| 11 |
* |
| 12 |
* Derived from the order id and number with a site salt, so it cannot be |
| 13 |
* guessed from either value and needs nothing stored alongside the order. |
| 14 |
* |
| 15 |
* @package Booktics/Order |
| 16 |
*/ |
| 17 |
class Order_Key { |
| 18 |
|
| 19 |
/** |
| 20 |
* Build the view key for an order. |
| 21 |
* |
| 22 |
* @param int $order_id Order id. |
| 23 |
* @param string $order_no Order number. |
| 24 |
* |
| 25 |
* @return string |
| 26 |
*/ |
| 27 |
public static function make( $order_id, $order_no ): string { |
| 28 |
return hash_hmac( 'sha256', (string) $order_id . '|' . $order_no, wp_salt( 'booktics_order_view' ) ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Check a key against an order. |
| 33 |
* |
| 34 |
* @param int $order_id Order id. |
| 35 |
* @param string $order_no Order number. |
| 36 |
* @param string $provided_key Key supplied by the caller. |
| 37 |
* |
| 38 |
* @return bool |
| 39 |
*/ |
| 40 |
public static function verify( $order_id, $order_no, $provided_key ): bool { |
| 41 |
if ( '' === $provided_key || strlen( $provided_key ) > 128 ) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
return hash_equals( self::make( $order_id, $order_no ), $provided_key ); |
| 46 |
} |
| 47 |
} |
| 48 |
|