Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4ef9e50

Browse files
author
bapo-sherman
committed
- added customer login token API
1 parent 8ce983a commit 4ef9e50

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
}
1313
],
1414
"require": {
15-
"php": ">=5.3.0"
15+
"php": ">=5.3.0",
16+
"firebase/php-jwt": "~3.0"
1617
},
1718
"require-dev": {
1819
"phpunit/phpunit": "4.0.15",

src/Bigcommerce/Api/Client.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Bigcommerce\Api;
44

5+
use Firebase\JWT\JWT;
56
use \Exception as Exception;
67

78
/**
@@ -16,6 +17,7 @@ class Client
1617
static private $resource;
1718
static private $path_prefix = '/api/v2';
1819
static private $client_id;
20+
static private $client_secret;
1921
static private $auth_token;
2022
static private $store_hash;
2123
static private $stores_prefix = '/stores/%s/v2';
@@ -94,6 +96,9 @@ public static function configureOAuth($settings)
9496
self::$client_id = $settings['client_id'];
9597
self::$auth_token = $settings['auth_token'];
9698
self::$store_hash = $settings['store_hash'];
99+
100+
self::$client_secret = isset($settings['client_secret']) ? $settings['client_secret'] : null;
101+
97102
self::$api_path = self::$api_url . sprintf(self::$stores_prefix, self::$store_hash);
98103
self::$connection = false;
99104
}
@@ -392,6 +397,41 @@ public static function getAuthToken($object)
392397
return $connection->post(self::$login_url . '/oauth2/token', $context);
393398
}
394399

400+
/**
401+
* generate login token
402+
*
403+
* @param int $id
404+
* @param string $redirectUrl
405+
* @param string $requestIp
406+
* @return mixed
407+
* @throws Exception
408+
*/
409+
public static function getCustomerLoginToken($id, $redirectUrl = '', $requestIp = '')
410+
{
411+
if (empty(self::$client_secret)) {
412+
throw new Exception('Cannot sign customer login tokens without a client secret');
413+
}
414+
415+
$payload = array(
416+
'iss' => self::$client_id,
417+
'iat' => time(),
418+
'jti' => bin2hex(random_bytes(32)),
419+
'operation' => 'customer_login',
420+
'store_hash' => self::$store_hash,
421+
'customer_id' => $id
422+
);
423+
424+
if (!empty($redirectUrl)) {
425+
$payload['redirect_to'] = $redirectUrl;
426+
}
427+
428+
if (!empty($requestIp)) {
429+
$payload['request_ip'] = $requestIp;
430+
}
431+
432+
return JWT::encode($payload, self::$client_secret, 'HS256');
433+
}
434+
395435
/**
396436
* Pings the time endpoint to test the connection to a store.
397437
*

0 commit comments

Comments
 (0)