@@ -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 ;
0 commit comments