Skip to content

Commit 15b3967

Browse files
committed
Add retry for failed access token request
1 parent a4123d5 commit 15b3967

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

src/AccessToken.php

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function hasScope($scope = '')
141141
public function get($scope = '')
142142
{
143143
$url = $this->platform->accessTokenUrl;
144-
if (!empty($url) && !$this->hasScope($scope) && !empty(Tool::$defaultTool) && !empty(Tool::$defaultTool->rsaKey)) {
144+
if (!empty($url) && !empty(Tool::$defaultTool) && !empty(Tool::$defaultTool->rsaKey)) {
145145
$scopesRequested = Tool::$defaultTool->requiredScopes;
146146
if (substr($scope, -9) === '.readonly') {
147147
$scope2 = substr($scope, 0, -9);
@@ -152,29 +152,38 @@ public function get($scope = '')
152152
$scopesRequested[] = $scope;
153153
}
154154
if (!empty($scopesRequested)) {
155-
$method = 'POST';
156-
$type = 'application/x-www-form-urlencoded';
157-
$body = array(
158-
'grant_type' => 'client_credentials',
159-
'client_assertion_type' => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
160-
'scope' => implode(' ', $scopesRequested)
161-
);
162-
$body = $this->platform->signServiceRequest($url, $method, $type, $body);
163-
$http = new HttpMessage($url, $method, $body);
164-
if ($http->send() && !empty($http->response)) {
165-
$http->responseJson = json_decode($http->response);
166-
if (!is_null($http->responseJson) && !empty($http->responseJson->access_token) && !empty($http->responseJson->expires_in)) {
167-
if (isset($http->responseJson->scope)) {
168-
$scopesAccepted = explode(' ', $http->responseJson->scope);
169-
} else {
170-
$scopesAccepted = $scopesRequested;
155+
$retry = false;
156+
do {
157+
$method = 'POST';
158+
$type = 'application/x-www-form-urlencoded';
159+
$body = array(
160+
'grant_type' => 'client_credentials',
161+
'client_assertion_type' => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
162+
'scope' => implode(' ', $scopesRequested)
163+
);
164+
$body = $this->platform->signServiceRequest($url, $method, $type, $body);
165+
$http = new HttpMessage($url, $method, $body);
166+
if ($http->send() && !empty($http->response)) {
167+
$http->responseJson = json_decode($http->response);
168+
if (!is_null($http->responseJson) && !empty($http->responseJson->access_token) && !empty($http->responseJson->expires_in)) {
169+
if (isset($http->responseJson->scope)) {
170+
$scopesAccepted = explode(' ', $http->responseJson->scope);
171+
} else {
172+
$scopesAccepted = $scopesRequested;
173+
}
174+
$this->scopes = $scopesAccepted;
175+
$this->token = $http->responseJson->access_token;
176+
$this->expires = time() + $http->responseJson->expires_in;
177+
$this->save();
171178
}
172-
$this->scopes = $scopesAccepted;
173-
$this->token = $http->responseJson->access_token;
174-
$this->expires = time() + $http->responseJson->expires_in;
175-
$this->save();
179+
$retry = false;
180+
} elseif ($retry) {
181+
$retry = false;
182+
} elseif (!empty($scope) && (count($scopesRequested) > 1)) { // Just ask for the single scope requested
183+
$retry = true;
184+
$scopesRequested = array($scope);
176185
}
177-
}
186+
} while ($retry);
178187
}
179188
} else {
180189
$this->scopes = null;

0 commit comments

Comments
 (0)