Skip to content

Commit e4b577a

Browse files
authored
Merge pull request #57 from Weebly/master
Add support for OAuth Bearer tokens
2 parents 091cdd3 + 42efaa6 commit e4b577a

File tree

3 files changed

+83
-19
lines changed

3 files changed

+83
-19
lines changed

lib/Shippo/ApiRequestor.php

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,31 +106,30 @@ public function handleApiError($rbody, $rcode, $resp)
106106
throw new Shippo_ApiError($msg, $rcode, $rbody, $resp);
107107
}
108108
}
109-
110-
private function _requestRaw($method, $url, $params)
109+
110+
public function getRequestHeaders()
111111
{
112-
$myApiKey = $this->_apiKey;
113-
if (!$myApiKey)
114-
$myApiKey = Shippo::$apiKey;
115-
116-
if (!$myApiKey) {
117-
$msg = 'No credentials provided.';
118-
throw new Shippo_AuthenticationError($msg);
119-
}
120-
121-
$absUrl = $this->apiUrl($url);
122-
$params = self::_encodeObjects($params);
123-
$langVersion = phpversion();
124-
$uname = php_uname();
112+
$apiKey = $this->_getApiKey();
113+
125114
$headers = array(
126115
'Content-Type: application/json',
127-
'Authorization: ShippoToken ' . $myApiKey,
116+
'Authorization: ' . $this->_getAuthorizationType($apiKey) . ' ' . $apiKey,
128117
'Accept: application/json',
129118
'User-Agent: Shippo/v1 PHPBindings/' . Shippo::VERSION
130119
);
131120
if (Shippo::getApiVersion()){
132121
$headers[] = 'Shippo-API-Version: ' . Shippo::getApiVersion();
133122
}
123+
124+
return $headers;
125+
}
126+
127+
private function _requestRaw($method, $url, $params)
128+
{
129+
$absUrl = $this->apiUrl($url);
130+
$params = self::_encodeObjects($params);
131+
$myApiKey = $this->_getApiKey();
132+
$headers = $this->getRequestHeaders();
134133

135134
list($rbody, $rcode) = $this->httpClient()->request($method, $absUrl, $headers, $params);
136135
return array(
@@ -155,7 +154,24 @@ private function _interpretResponse($rbody, $rcode)
155154
}
156155
return $resp;
157156
}
158-
157+
158+
private function _getApiKey()
159+
{
160+
$apiKey = $this->_apiKey;
161+
if (!$apiKey)
162+
$apiKey = Shippo::$apiKey;
163+
164+
if (!$apiKey) {
165+
throw new Shippo_AuthenticationError('No credentials provided.');
166+
}
167+
168+
return $apiKey;
169+
}
170+
171+
private function _getAuthorizationType($apiKey = '')
172+
{
173+
return strpos($apiKey, 'oauth.') === 0 ? 'Bearer' : 'ShippoToken';
174+
}
159175

160176
public static function setHttpClient($client)
161177
{

test/ApiRequestorTest.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,47 @@ public function testEncodeObjects()
3737
));
3838
}
3939
}
40-
40+
41+
/**
42+
* @dataProvider provideValidAPITokens
43+
*
44+
* @param $expectedAuthorizationType
45+
* @param $apiToken
46+
*/
47+
public function testGetAuthorizationType($expectedAuthorizationType, $apiToken)
48+
{
49+
$apiRequestor = new Shippo_ApiRequestor($apiToken);
50+
$headers = $apiRequestor->getRequestHeaders();
51+
$authorizationHeader = current(array_filter($headers, function ($header) {
52+
return strpos($header, 'Authorization:') === 0;
53+
}));
54+
55+
$this->assertEquals(strpos($authorizationHeader, 'Authorization: ' . $expectedAuthorizationType), 0);
56+
}
57+
58+
public function provideValidAPITokens()
59+
{
60+
return [
61+
'oauth bearer token' => [
62+
'Bearer',
63+
'oauth.612BUDkTaTuJP3ll5-VkebURXUIJ5Zefxwda1tpd.U_akmGaXVQl80CWPXSbueSG7NX7sNe_HvLJLN1d1pn0='
64+
],
65+
'random oauth formatted token' => [
66+
'Bearer',
67+
'oauth.foo'
68+
],
69+
'shippo token' => [
70+
'ShippoToken',
71+
'dW5pdHRlc3Q6dW5pdHRlc3Q='
72+
],
73+
'random token' => [
74+
'ShippoToken',
75+
'askdljfgaklsdfjalskdfjalksjd'
76+
],
77+
'random token with oauth in the string' => [
78+
'ShippoToken',
79+
'askdljfgaklsdfjalskdfjalksjd.oauth'
80+
],
81+
];
82+
}
4183
}

test/ShippoBaseTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@ public function testSetApiKey()
77
Shippo::setApiKey('dW5pdHRlc3Q6dW5pdHRlc3Q=');
88
$this->assertEquals(Shippo::getApiKey(), 'dW5pdHRlc3Q6dW5pdHRlc3Q=');
99
}
10-
}
10+
11+
public function testSetOAuthTokenApiKey()
12+
{
13+
Shippo::setApiKey('oauth.612BUDkTaTuJP3ll5-VkebURXUIJ5Zefxwda1tpd.U_akmGaXVQl80CWPXSbueSG7NX7sNe_HvLJLN1d1pn0=');
14+
$this->assertEquals(Shippo::getApiKey(), 'oauth.612BUDkTaTuJP3ll5-VkebURXUIJ5Zefxwda1tpd.U_akmGaXVQl80CWPXSbueSG7NX7sNe_HvLJLN1d1pn0=');
15+
}
16+
}

0 commit comments

Comments
 (0)