Skip to content
This repository was archived by the owner on Oct 29, 2025. It is now read-only.

Commit cc74283

Browse files
authored
Merge pull request #6 from MichaelJ2324/master
Better Token Management
2 parents 908f137 + 5295d08 commit cc74283

File tree

5 files changed

+110
-33
lines changed

5 files changed

+110
-33
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"private": true,
2+
"private": false,
33
"name": "rest-php-client",
4-
"description": "An SDK for interacting with Sugar's REST v10 API.",
5-
"version": "1.1",
4+
"version": "1.2",
5+
"description": "An object oriented framework for interacting with Sugar 7's REST API.",
66
"directories": {
77
"test": "tests"
88
},

src/Client/Abstracts/AbstractSugarClient.php

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ abstract class AbstractSugarClient extends AbstractClient {
5151
'platform' => ''
5252
);
5353

54-
/**
55-
* Token expiration time
56-
* @var
57-
*/
58-
protected $expiration;
59-
6054
/**
6155
* The API Version to be used.
6256
* Defaults to 10 (for v10), but can be any number above 10, since customizing API allows for additional versioning to allow for duplicate entrypoints
@@ -89,6 +83,10 @@ public function setVersion($version){
8983
return $this;
9084
}
9185

86+
/**
87+
* Get the Version of API being used by the Client
88+
* @return int
89+
*/
9290
public function getVersion(){
9391
return $this->apiVersion;
9492
}
@@ -119,8 +117,13 @@ public function setCredentials(array $credentials){
119117
*/
120118
public function setToken($token){
121119
if ($token instanceof \stdClass) {
120+
if (!isset($token->expiration)){
121+
$token->expiration = time() + $token->expires_in;
122+
}
123+
if (!isset($token->refresh_expiration)){
124+
$token->refresh_expiration = time() + $token->refresh_expires_in;
125+
}
122126
parent::setToken($token);
123-
$this->expiration = time() + $token->expires_in;
124127
return $this;
125128
}else{
126129
throw new SDKException('Sugar API Client requires Token to be of type \stdClass');
@@ -131,7 +134,20 @@ public function setToken($token){
131134
* @inheritdoc
132135
*/
133136
public function authenticated(){
134-
return time() < $this->expiration;
137+
if (parent::authenticated()){
138+
if (!$this->expiredToken()){
139+
return TRUE;
140+
}
141+
}
142+
return FALSE;
143+
}
144+
145+
/**
146+
* Check if token is expired based on Access Token Expiration
147+
* @return bool
148+
*/
149+
protected function expiredToken(){
150+
return time() >= $this->token->expiration;
135151
}
136152

137153
/**
@@ -152,7 +168,7 @@ protected function registerSDKEndpoints(){
152168
public function __call($name, $params){
153169
$Endpoint = parent::__call($name,$params);
154170

155-
if ($Endpoint->authRequired() && $this->authenticated()){
171+
if ($Endpoint->authRequired()){
156172
$Endpoint->setAuth($this->token->access_token);
157173
}
158174
return $Endpoint;
@@ -173,8 +189,13 @@ public function login() {
173189
static::storeToken($this->token, $this->credentials['client_id']);
174190
return TRUE;
175191
} else {
176-
$error = $response->getBody();
177-
throw new AuthenticationException("Login Response [" . $error['error'] . "] " . $error['error_message']);
192+
if ($response->getError() === FALSE) {
193+
$error = $response->getBody();
194+
$error = $error['error'] . " - " . $error['error_message'];
195+
}else{
196+
$error = $response->getError();
197+
}
198+
throw new AuthenticationException("Login Response [" .$response->getStatus() ."] - " .$error);
178199
}
179200
}
180201
return FALSE;
@@ -188,19 +209,26 @@ public function refreshToken(){
188209
if (isset($this->credentials['client_id'])&&
189210
isset($this->credentials['client_secret'])&&
190211
isset($this->token)) {
191-
$refreshOptions = array(
192-
'client_id' => $this->credentials['client_id'],
193-
'client_secret' => $this->credentials['client_secret'],
194-
'refresh_token' => $this->token->refresh_token
195-
);
196-
$response = $this->oauth2Refresh()->execute($refreshOptions)->getResponse();
197-
if ($response->getStatus() == '200') {
198-
$this->setToken($response->getBody(FALSE));
199-
static::storeToken($this->token, $this->credentials['client_id']);
200-
return TRUE;
201-
} else {
202-
$error = $response->getBody();
203-
throw new AuthenticationException("Refresh Response [" . $error['error'] . "] " . $error['error_message']);
212+
if (time() < $this->token->refresh_expiration) {
213+
$refreshOptions = array(
214+
'client_id' => $this->credentials['client_id'],
215+
'client_secret' => $this->credentials['client_secret'],
216+
'refresh_token' => $this->token->refresh_token
217+
);
218+
$response = $this->oauth2Refresh()->execute($refreshOptions)->getResponse();
219+
if ($response->getStatus() == '200') {
220+
$this->setToken($response->getBody(FALSE));
221+
static::storeToken($this->token, $this->credentials['client_id']);
222+
return TRUE;
223+
} else {
224+
if ($response->getError() === FALSE) {
225+
$error = $response->getBody();
226+
$error = $error['error'] . " - " . $error['error_message'];
227+
}else{
228+
$error = $response->getError();
229+
}
230+
throw new AuthenticationException("Refresh Response [" .$response->getStatus() ."] - " .$error);
231+
}
204232
}
205233
}
206234
return FALSE;
@@ -219,8 +247,13 @@ public function logout(){
219247
}
220248
return parent::logout();
221249
}else{
222-
$error = $response->getBody();
223-
throw new AuthenticationException("Logout Response [".$error['error']."] ".$error['message']);
250+
if ($response->getError() === FALSE) {
251+
$error = $response->getBody();
252+
$error = $error['error'] . " - " . $error['error_message'];
253+
}else{
254+
$error = $response->getError();
255+
}
256+
throw new AuthenticationException("Logout Response [" .$response->getStatus() ."] - " .$error);
224257
}
225258
}
226259
return FALSE;

src/Request/Abstracts/AbstractRequest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ abstract class AbstractRequest implements RequestInterface {
2828
CURLOPT_HEADER => TRUE,
2929
CURLOPT_SSL_VERIFYPEER => FALSE,
3030
CURLOPT_RETURNTRANSFER => TRUE,
31-
CURLOPT_FOLLOWLOCATION => FALSE,
32-
CURLOPT_USERAGENT => 'SugarAPI-SDK-PHP'
31+
CURLOPT_USERAGENT => 'Sugar-REST-PHP-Client'
3332
);
3433

3534
/**

src/Response/Abstracts/AbstractResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct($curlRequest,$curlResponse = NULL){
5454

5555
public function setCurlResponse($curlResponse) {
5656
$this->extractInfo();
57-
if (!$this->error){
57+
if ($this->error === FALSE){
5858
$this->extractResponse($curlResponse);
5959
}
6060
}

tests/Clients/AbstractSugarClientTest.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,19 @@ public function testSetCredentials($Stub){
185185
* @covers ::setToken
186186
* @covers ::getToken
187187
* @covers ::authenticated
188+
* @covers ::expiredToken
188189
* @group abstractClient
189190
* @return SugarClientStub
190191
*/
191192
public function testSetToken($Stub){
192-
193193
static::$token->expires_in = 0;
194+
194195
$Stub->setToken(static::$token);
195196
$this->assertEquals(static::$token,$Stub->getToken());
196197
$this->assertEquals(false,$Stub->authenticated());
197198

198199
static::$token->expires_in = 3600;
200+
unset(static::$token->expiration);
199201

200202
$Stub->setToken(static::$token);
201203
$this->assertEquals(static::$token,$Stub->getToken());
@@ -277,6 +279,20 @@ public function testLoginException($Stub){
277279
$Stub->login();
278280
}
279281

282+
/**
283+
* @param SugarClientStub $Stub
284+
* @depends testLogin
285+
* @covers ::login
286+
* @expectedException SugarAPI\SDK\Exception\Authentication\AuthenticationException
287+
* @expectedExceptionMessageRegExp /Login Response/
288+
* @group abstractClients
289+
*/
290+
public function testLoginExceptionCurlError($Stub){
291+
$Stub->setCredentials($this->credentials);
292+
$Stub->setServer('test.foo.bar');
293+
$Stub->login();
294+
}
295+
280296
/**
281297
* @covers ::refreshToken
282298
* @group abstractClients
@@ -302,6 +318,21 @@ public function testRefreshException($Stub){
302318
$Stub->refreshToken();
303319
}
304320

321+
/**
322+
* @param SugarClientStub $Stub
323+
* @depends testRefreshToken
324+
* @covers ::refreshToken
325+
* @expectedException SugarAPI\SDK\Exception\Authentication\AuthenticationException
326+
* @expectedExceptionMessageRegExp /Refresh Response/
327+
* @group abstractClients
328+
*/
329+
public function testRefreshExceptionCurlError($Stub){
330+
$Stub->setCredentials($this->credentials);
331+
$Stub->setToken(static::$token);
332+
$Stub->setServer('test.foo.bar');
333+
$Stub->refreshToken();
334+
}
335+
305336
/**
306337
* @covers ::logout
307338
* @group abstractClients
@@ -326,5 +357,19 @@ public function testLogoutException($Stub){
326357
$Stub->logout();
327358
}
328359

360+
/**
361+
* @param SugarClientStub $Stub
362+
* @depends testLogout
363+
* @covers ::logout
364+
* @expectedException SugarAPI\SDK\Exception\Authentication\AuthenticationException
365+
* @expectedExceptionMessageRegExp /Logout Response/
366+
* @group abstractClients
367+
*/
368+
public function testLogoutExceptionCurlError($Stub){
369+
$Stub->setToken(static::$token);
370+
$Stub->setServer('test.foo.bar');
371+
$Stub->logout();
372+
}
373+
329374

330375
}

0 commit comments

Comments
 (0)