Skip to content

Commit 4e8086a

Browse files
committed
Revert "removing basic auth from the client in favor of oauth. including removing no longer used properties"
This reverts commit 03fccfc.
1 parent 03fccfc commit 4e8086a

3 files changed

Lines changed: 138 additions & 85 deletions

File tree

src/Bigcommerce/Api/Client.php

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@
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+
1233
/**
1334
* Connection instance
1435
*
@@ -24,37 +45,41 @@ class Client
2445
static private $resource;
2546

2647
/**
27-
* Full URL path to the configured store API.
48+
* API path prefix to be added to store URL for requests
2849
*
2950
* @var string
3051
*/
31-
static public $api_path;
52+
static private $path_prefix = '/api/v2';
53+
3254
/**
33-
* Client ID from app installation
55+
* Full URL path to the configured store API.
3456
*
3557
* @var string
3658
*/
59+
static public $api_path;
3760
static private $client_id;
3861
static private $store_hash;
3962
static private $auth_token;
40-
/**
41-
* Appended to API URL to create API endpoints
42-
*
43-
* @var string
44-
*/
4563
static private $stores_prefix = '/stores/%s/v2';
46-
/**
47-
* Base URL for the Bigcommerce API
48-
*
49-
* @var string
50-
*/
5164
static private $api_url = 'https://api.bigcommerce.com';
65+
static private $login_url = 'https://login.bigcommerce.com';
66+
5267
/**
53-
* Endpoint for getting tokens
68+
* Configure the API client with the required settings to access
69+
* the API for a store.
5470
*
55-
* @var string
71+
* Accepts both OAuth and Basic Auth credentials
72+
*
73+
* @param array $settings
5674
*/
57-
static private $login_url = 'https://login.bigcommerce.com';
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+
}
5883

5984
/**
6085
* Configure the API client with the required OAuth credentials.
@@ -68,7 +93,7 @@ class Client
6893
* @param array $settings
6994
* @throws \Exception
7095
*/
71-
public static function configure($settings)
96+
public static function configureOAuth($settings)
7297
{
7398
if (!isset($settings['auth_token'])) {
7499
throw new Exception("'auth_token' must be provided");
@@ -85,6 +110,39 @@ public static function configure($settings)
85110
self::$connection = false;
86111
}
87112

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+
88146
/**
89147
* Configure the API client to throw exceptions when HTTP errors occur.
90148
*
@@ -156,7 +214,11 @@ private static function connection()
156214
{
157215
if (!self::$connection) {
158216
self::$connection = new Connection();
159-
self::$connection->authenticate(self::$client_id, self::$auth_token);
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+
}
160222
}
161223

162224
return self::$connection;

src/Bigcommerce/Api/Connection.php

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

154154
/**
155-
* Sets OAuth authentication headers
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
156167
*
157168
* @param string $clientId
158169
* @param string $authToken
159170
*/
160-
public function authenticate($clientId, $authToken)
171+
public function authenticateOauth($clientId, $authToken)
161172
{
162173
$this->addHeader('X-Auth-Client', $clientId);
163174
$this->addHeader('X-Auth-Token', $authToken);
@@ -519,8 +530,6 @@ public function getBody()
519530
/**
520531
* Access given header from the response.
521532
*
522-
* @param string $header
523-
*
524533
* @return string|void
525534
*/
526535
public function getHeader($header)

test/Unit/Api/ClientTest.php

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

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

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

59-
public function testConfigureRequiresStoreHash()
59+
public function testConfigureRequiresUsername()
6060
{
61-
$this->setExpectedException('\\Exception', "'store_hash' must be provided");
62-
Client::configure(array('client_id' => 'whatever', 'auth_token' => 'whatever'));
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'));
6369
}
6470

6571
public function testFailOnErrorPassesThroughToConnection()
@@ -116,14 +122,10 @@ public function testGetResourceReturnsSpecifiedType()
116122
{
117123
$this->connection->expects($this->once())
118124
->method('get')
119-
->with('https://api.bigcommerce.com/stores/hash/v2/whatever', false)
125+
->with('http://storeurl' . $this->basePath . '/whatever', false)
120126
->will($this->returnValue(array(array())));
121127

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

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

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

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

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

212-
Client::configure(array(
213-
'store_hash' => 'hash',
214-
'client_id' => 'whatever',
215-
'auth_token' => 'whatever'
216-
));
198+
Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever'));
217199
Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it
218200
$result = Client::deleteResource('/whatever');
219201
$this->assertSame("Successfully deleted", $result);
@@ -267,18 +249,18 @@ public function testGetRequestsRemainingRequestsTimeWhenNoValueAvailable()
267249
public function collections()
268250
{
269251
return array(
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'),
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'),
276258
array('order_statuses', 'getOrderStatuses', 'OrderStatus'),
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'),
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'),
282264
);
283265
}
284266

@@ -321,15 +303,15 @@ public function testGettingTheCountOfACollectionReturnsThatCollectionsCount($pat
321303
public function resources()
322304
{
323305
return array(
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'),
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'),
333315
);
334316
}
335317

0 commit comments

Comments
 (0)