Skip to content

Commit 03fccfc

Browse files
committed
removing basic auth from the client in favor of oauth. including removing no longer used properties
1 parent 913c33a commit 03fccfc

3 files changed

Lines changed: 85 additions & 138 deletions

File tree

src/Bigcommerce/Api/Client.php

Lines changed: 18 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,6 @@
99
*/
1010
class Client
1111
{
12-
/**
13-
* Full Store URL to connect to
14-
*
15-
* @var string
16-
*/
17-
static private $store_url;
18-
19-
/**
20-
* Username to connect to the store API with
21-
*
22-
* @var string
23-
*/
24-
static private $username;
25-
26-
/**
27-
* API key
28-
*
29-
* @var string
30-
*/
31-
static private $api_key;
32-
3312
/**
3413
* Connection instance
3514
*
@@ -45,41 +24,37 @@ class Client
4524
static private $resource;
4625

4726
/**
48-
* API path prefix to be added to store URL for requests
27+
* Full URL path to the configured store API.
4928
*
5029
* @var string
5130
*/
52-
static private $path_prefix = '/api/v2';
53-
31+
static public $api_path;
5432
/**
55-
* Full URL path to the configured store API.
33+
* Client ID from app installation
5634
*
5735
* @var string
5836
*/
59-
static public $api_path;
6037
static private $client_id;
6138
static private $store_hash;
6239
static private $auth_token;
40+
/**
41+
* Appended to API URL to create API endpoints
42+
*
43+
* @var string
44+
*/
6345
static private $stores_prefix = '/stores/%s/v2';
64-
static private $api_url = 'https://api.bigcommerce.com';
65-
static private $login_url = 'https://login.bigcommerce.com';
66-
6746
/**
68-
* Configure the API client with the required settings to access
69-
* the API for a store.
47+
* Base URL for the Bigcommerce API
7048
*
71-
* Accepts both OAuth and Basic Auth credentials
49+
* @var string
50+
*/
51+
static private $api_url = 'https://api.bigcommerce.com';
52+
/**
53+
* Endpoint for getting tokens
7254
*
73-
* @param array $settings
55+
* @var string
7456
*/
75-
public static function configure($settings)
76-
{
77-
if (isset($settings['client_id'])) {
78-
self::configureOAuth($settings);
79-
} else {
80-
self::configureBasicAuth($settings);
81-
}
82-
}
57+
static private $login_url = 'https://login.bigcommerce.com';
8358

8459
/**
8560
* Configure the API client with the required OAuth credentials.
@@ -93,7 +68,7 @@ public static function configure($settings)
9368
* @param array $settings
9469
* @throws \Exception
9570
*/
96-
public static function configureOAuth($settings)
71+
public static function configure($settings)
9772
{
9873
if (!isset($settings['auth_token'])) {
9974
throw new Exception("'auth_token' must be provided");
@@ -110,39 +85,6 @@ public static function configureOAuth($settings)
11085
self::$connection = false;
11186
}
11287

113-
/**
114-
* Configure the API client with the required credentials.
115-
*
116-
* Requires a settings array to be passed in with the following keys:
117-
*
118-
* - store_url
119-
* - username
120-
* - api_key
121-
*
122-
* @param array $settings
123-
* @throws \Exception
124-
*/
125-
public static function configureBasicAuth(array $settings)
126-
{
127-
if (!isset($settings['store_url'])) {
128-
throw new Exception("'store_url' must be provided");
129-
}
130-
131-
if (!isset($settings['username'])) {
132-
throw new Exception("'username' must be provided");
133-
}
134-
135-
if (!isset($settings['api_key'])) {
136-
throw new Exception("'api_key' must be provided");
137-
}
138-
139-
self::$username = $settings['username'];
140-
self::$api_key = $settings['api_key'];
141-
self::$store_url = rtrim($settings['store_url'], '/');
142-
self::$api_path = self::$store_url . self::$path_prefix;
143-
self::$connection = false;
144-
}
145-
14688
/**
14789
* Configure the API client to throw exceptions when HTTP errors occur.
14890
*
@@ -214,11 +156,7 @@ private static function connection()
214156
{
215157
if (!self::$connection) {
216158
self::$connection = new Connection();
217-
if (self::$client_id) {
218-
self::$connection->authenticateOauth(self::$client_id, self::$auth_token);
219-
} else {
220-
self::$connection->authenticateBasic(self::$username, self::$api_key);
221-
}
159+
self::$connection->authenticate(self::$client_id, self::$auth_token);
222160
}
223161

224162
return self::$connection;

src/Bigcommerce/Api/Connection.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,12 @@ public function failOnError($option = true)
152152
}
153153

154154
/**
155-
* Sets the HTTP basic authentication.
156-
*
157-
* @param string $username
158-
* @param string $password
159-
*/
160-
public function authenticateBasic($username, $password)
161-
{
162-
curl_setopt($this->curl, CURLOPT_USERPWD, "$username:$password");
163-
}
164-
165-
/**
166-
* Sets Oauth authentication headers
155+
* Sets OAuth authentication headers
167156
*
168157
* @param string $clientId
169158
* @param string $authToken
170159
*/
171-
public function authenticateOauth($clientId, $authToken)
160+
public function authenticate($clientId, $authToken)
172161
{
173162
$this->addHeader('X-Auth-Client', $clientId);
174163
$this->addHeader('X-Auth-Token', $authToken);
@@ -530,6 +519,8 @@ public function getBody()
530519
/**
531520
* Access given header from the response.
532521
*
522+
* @param string $header
523+
*
533524
* @return string|void
534525
*/
535526
public function getHeader($header)

test/Unit/Api/ClientTest.php

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,20 @@ public function setUp()
4646

4747
public function tearDown()
4848
{
49-
Client::configure(array('username' => '', 'api_key' => '', 'store_url' => ''));
49+
Client::configure(array('client_id' => '', 'auth_token' => '', 'store_hash' => ''));
5050
unset($this->connection);
5151
}
5252

53-
public function testConfigureRequiresStoreUrl()
53+
public function testConfigureRequiresAuthToken()
5454
{
55-
$this->setExpectedException('\\Exception', "'store_url' must be provided");
56-
Client::configure(array('username' => 'whatever', 'api_key' => 'whatever'));
55+
$this->setExpectedException('\\Exception', "'auth_token' must be provided");
56+
Client::configure(array('client_id' => 'whatever', 'store_hash' => 'whatever'));
5757
}
5858

59-
public function testConfigureRequiresUsername()
59+
public function testConfigureRequiresStoreHash()
6060
{
61-
$this->setExpectedException('\\Exception', "'username' must be provided");
62-
Client::configure(array('store_url' => 'whatever', 'api_key' => 'whatever'));
63-
}
64-
65-
public function testConfigureRequiresApiKey()
66-
{
67-
$this->setExpectedException('\\Exception', "'api_key' must be provided");
68-
Client::configure(array('username' => 'whatever', 'store_url' => 'whatever'));
61+
$this->setExpectedException('\\Exception', "'store_hash' must be provided");
62+
Client::configure(array('client_id' => 'whatever', 'auth_token' => 'whatever'));
6963
}
7064

7165
public function testFailOnErrorPassesThroughToConnection()
@@ -122,10 +116,14 @@ public function testGetResourceReturnsSpecifiedType()
122116
{
123117
$this->connection->expects($this->once())
124118
->method('get')
125-
->with('http://storeurl' . $this->basePath . '/whatever', false)
119+
->with('https://api.bigcommerce.com/stores/hash/v2/whatever', false)
126120
->will($this->returnValue(array(array())));
127121

128-
Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever'));
122+
Client::configure(array(
123+
'store_hash' => 'hash',
124+
'client_id' => 'whatever',
125+
'auth_token' => 'whatever'
126+
));
129127
Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it
130128
$resource = Client::getResource('/whatever');
131129
$this->assertInstanceOf('Bigcommerce\\Api\\Resource', $resource);
@@ -135,10 +133,14 @@ public function testGetCountReturnsSpecifiedCount()
135133
{
136134
$this->connection->expects($this->once())
137135
->method('get')
138-
->with('http://storeurl' . $this->basePath . '/whatever', false)
136+
->with('https://api.bigcommerce.com/stores/hash/v2/whatever', false)
139137
->will($this->returnValue((object)array('count' => 5)));
140138

141-
Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever'));
139+
Client::configure(array(
140+
'store_hash' => 'hash',
141+
'client_id' => 'whatever',
142+
'auth_token' => 'whatever'
143+
));
142144
Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it
143145
$count = Client::getCount('/whatever');
144146
$this->assertSame(5, $count);
@@ -148,10 +150,14 @@ public function testGetCollectionReturnsCollectionOfSpecifiedTypes()
148150
{
149151
$this->connection->expects($this->once())
150152
->method('get')
151-
->with('http://storeurl' . $this->basePath . '/whatever', false)
153+
->with('https://api.bigcommerce.com/stores/hash/v2/whatever', false)
152154
->will($this->returnValue(array(array(), array())));
153155

154-
Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever'));
156+
Client::configure(array(
157+
'store_hash' => 'hash',
158+
'client_id' => 'whatever',
159+
'auth_token' => 'whatever'
160+
));
155161
Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it
156162
$resources = Client::getCollection('/whatever');
157163
$this->assertInternalType('array', $resources);
@@ -165,10 +171,14 @@ public function testCreateResourcePostsToTheRightPlace()
165171
$new = array(rand() => rand());
166172
$this->connection->expects($this->once())
167173
->method('post')
168-
->with('http://storeurl' . $this->basePath . '/whatever', (object)$new)
174+
->with('https://api.bigcommerce.com/stores/hash/v2/whatever', (object)$new)
169175
->will($this->returnValue($new));
170176

171-
Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever'));
177+
Client::configure(array(
178+
'store_hash' => 'hash',
179+
'client_id' => 'whatever',
180+
'auth_token' => 'whatever'
181+
));
172182
Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it
173183
$result = Client::createResource('/whatever', $new);
174184
$this->assertSame($new, $result);
@@ -179,10 +189,14 @@ public function testUpdateResourcePutsToTheRightPlace()
179189
$update = array(rand() => rand());
180190
$this->connection->expects($this->once())
181191
->method('put')
182-
->with('http://storeurl' . $this->basePath . '/whatever', (object)$update)
192+
->with('https://api.bigcommerce.com/stores/hash/v2/whatever', (object)$update)
183193
->will($this->returnValue($update));
184194

185-
Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever'));
195+
Client::configure(array(
196+
'store_hash' => 'hash',
197+
'client_id' => 'whatever',
198+
'auth_token' => 'whatever'
199+
));
186200
Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it
187201
$result = Client::updateResource('/whatever', $update);
188202
$this->assertSame($update, $result);
@@ -192,10 +206,14 @@ public function testDeleteResourceDeletesToTheRightPlace()
192206
{
193207
$this->connection->expects($this->once())
194208
->method('delete')
195-
->with('http://storeurl' . $this->basePath . '/whatever')
209+
->with('https://api.bigcommerce.com/stores/hash/v2/whatever')
196210
->will($this->returnValue("Successfully deleted"));
197211

198-
Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever'));
212+
Client::configure(array(
213+
'store_hash' => 'hash',
214+
'client_id' => 'whatever',
215+
'auth_token' => 'whatever'
216+
));
199217
Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it
200218
$result = Client::deleteResource('/whatever');
201219
$this->assertSame("Successfully deleted", $result);
@@ -249,18 +267,18 @@ public function testGetRequestsRemainingRequestsTimeWhenNoValueAvailable()
249267
public function collections()
250268
{
251269
return array(
252-
// path function classname
253-
array('products', 'getProducts', 'Product'),
254-
array('brands', 'getBrands', 'Brand'),
255-
array('orders', 'getOrders', 'Order'),
256-
array('customers', 'getCustomers', 'Customer'),
257-
array('coupons', 'getCoupons', 'Coupon'),
270+
// path function classname
271+
array('products', 'getProducts', 'Product'),
272+
array('brands', 'getBrands', 'Brand'),
273+
array('orders', 'getOrders', 'Order'),
274+
array('customers', 'getCustomers', 'Customer'),
275+
array('coupons', 'getCoupons', 'Coupon'),
258276
array('order_statuses', 'getOrderStatuses', 'OrderStatus'),
259-
array('categories', 'getCategories', 'Category'),
260-
array('options', 'getOptions', 'Option'),
261-
array('optionsets', 'getOptionSets', 'OptionSet'),
262-
array('products/skus', 'getSkus', 'Sku'),
263-
array('requestlogs', 'getRequestLogs', 'RequestLog'),
277+
array('categories', 'getCategories', 'Category'),
278+
array('options', 'getOptions', 'Option'),
279+
array('optionsets', 'getOptionSets', 'OptionSet'),
280+
array('products/skus', 'getSkus', 'Sku'),
281+
array('requestlogs', 'getRequestLogs', 'RequestLog'),
264282
);
265283
}
266284

@@ -303,15 +321,15 @@ public function testGettingTheCountOfACollectionReturnsThatCollectionsCount($pat
303321
public function resources()
304322
{
305323
return array(
306-
// path function classname
307-
array('products', '%sProduct', 'Product'),
308-
array('brands', '%sBrand', 'Brand'),
309-
array('orders', '%sOrder', 'Order'),
310-
array('customers', '%sCustomer', 'Customer'),
311-
array('categories', '%sCategory', 'Category'),
312-
array('options', '%sOption', 'Option'),
313-
array('optionsets', '%sOptionSet', 'OptionSet'),
314-
array('coupons', '%sCoupon', 'Coupon'),
324+
// path function classname
325+
array('products', '%sProduct', 'Product'),
326+
array('brands', '%sBrand', 'Brand'),
327+
array('orders', '%sOrder', 'Order'),
328+
array('customers', '%sCustomer', 'Customer'),
329+
array('categories', '%sCategory', 'Category'),
330+
array('options', '%sOption', 'Option'),
331+
array('optionsets', '%sOptionSet', 'OptionSet'),
332+
array('coupons', '%sCoupon', 'Coupon'),
315333
);
316334
}
317335

0 commit comments

Comments
 (0)