@@ -141,7 +141,7 @@ public function hasScope($scope = '')
141
141
public function get ($ scope = '' )
142
142
{
143
143
$ 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 )) {
145
145
$ scopesRequested = Tool::$ defaultTool ->requiredScopes ;
146
146
if (substr ($ scope , -9 ) === '.readonly ' ) {
147
147
$ scope2 = substr ($ scope , 0 , -9 );
@@ -152,29 +152,38 @@ public function get($scope = '')
152
152
$ scopesRequested [] = $ scope ;
153
153
}
154
154
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 ();
171
178
}
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 );
176
185
}
177
- }
186
+ } while ( $ retry );
178
187
}
179
188
} else {
180
189
$ this ->scopes = null ;
0 commit comments