|
3 | 3 | namespace Bigcommerce\Api; |
4 | 4 |
|
5 | 5 | use \Exception as Exception; |
| 6 | +use Firebase\JWT\JWT; |
6 | 7 |
|
7 | 8 | /** |
8 | 9 | * Bigcommerce API Client. |
@@ -60,6 +61,7 @@ class Client |
60 | 61 | static private $client_id; |
61 | 62 | static private $store_hash; |
62 | 63 | static private $auth_token; |
| 64 | + static private $client_secret; |
63 | 65 | static private $stores_prefix = '/stores/%s/v2'; |
64 | 66 | static private $api_url = 'https://api.bigcommerce.com'; |
65 | 67 | static private $login_url = 'https://login.bigcommerce.com'; |
@@ -106,6 +108,9 @@ public static function configureOAuth($settings) |
106 | 108 | self::$client_id = $settings['client_id']; |
107 | 109 | self::$auth_token = $settings['auth_token']; |
108 | 110 | self::$store_hash = $settings['store_hash']; |
| 111 | + |
| 112 | + self::$client_secret = isset($settings['client_secret']) ? $settings['client_secret'] : null; |
| 113 | + |
109 | 114 | self::$api_path = self::$api_url . sprintf(self::$stores_prefix, self::$store_hash); |
110 | 115 | self::$connection = false; |
111 | 116 | } |
@@ -413,6 +418,32 @@ public static function getAuthToken($object) |
413 | 418 | return $connection->post(self::$login_url . '/oauth2/token', $context); |
414 | 419 | } |
415 | 420 |
|
| 421 | + public static function getCustomerLoginToken($id, $redirectUrl = '', $requestIp = '') |
| 422 | + { |
| 423 | + if (empty(self::$client_secret)) { |
| 424 | + throw new Exception('Cannot sign customer login tokens without a client secret'); |
| 425 | + } |
| 426 | + |
| 427 | + $payload = array( |
| 428 | + 'iss' => self::$client_id, |
| 429 | + 'iat' => time(), |
| 430 | + 'jti' => bin2hex(random_bytes(32)), |
| 431 | + 'operation' => 'customer_login', |
| 432 | + 'store_hash' => self::$store_hash, |
| 433 | + 'customer_id' => $id |
| 434 | + ); |
| 435 | + |
| 436 | + if (!empty($redirectUrl)) { |
| 437 | + $payload['redirect_to'] = $redirectUrl; |
| 438 | + } |
| 439 | + |
| 440 | + if (!empty($requestIp)) { |
| 441 | + $payload['request_ip'] = $requestIp; |
| 442 | + } |
| 443 | + |
| 444 | + return JWT::encode($payload, self::$client_secret, 'HS256'); |
| 445 | + } |
| 446 | + |
416 | 447 | /** |
417 | 448 | * Pings the time endpoint to test the connection to a store. |
418 | 449 | * |
|
0 commit comments