@@ -170,28 +170,37 @@ def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument
170
170
171
171
token = self ._client .get_cached_token (scopes )
172
172
if not token :
173
- resource = scopes [0 ]
174
- if resource .endswith ("/.default" ):
175
- resource = resource [: - len ("/.default" )]
176
- params = dict ({"api-version" : "2018-02-01" , "resource" : resource }, ** self ._identity_config )
177
-
173
+ token = self ._refresh_token (* scopes )
174
+ elif self ._client .should_refresh (token ):
178
175
try :
179
- token = self ._client .request_token (scopes , method = "GET" , params = params )
180
- except HttpResponseError as ex :
181
- # 400 in response to a token request indicates managed identity is disabled,
182
- # or the identity with the specified client_id is not available
183
- if ex .status_code == 400 :
184
- self ._endpoint_available = False
185
- message = "ManagedIdentityCredential authentication unavailable. "
186
- if self ._identity_config :
187
- message += "The requested identity has not been assigned to this resource."
188
- else :
189
- message += "No identity has been assigned to this resource."
190
- six .raise_from (CredentialUnavailableError (message = message ), ex )
191
-
192
- # any other error is unexpected
193
- six .raise_from (ClientAuthenticationError (message = ex .message , response = ex .response ), None )
176
+ token = self ._refresh_token (* scopes )
177
+ except Exception : # pylint: disable=broad-except
178
+ pass
179
+
180
+ return token
194
181
182
+ def _refresh_token (self , * scopes ):
183
+ resource = scopes [0 ]
184
+ if resource .endswith ("/.default" ):
185
+ resource = resource [: - len ("/.default" )]
186
+ params = dict ({"api-version" : "2018-02-01" , "resource" : resource }, ** self ._identity_config )
187
+
188
+ try :
189
+ token = self ._client .request_token (scopes , method = "GET" , params = params )
190
+ except HttpResponseError as ex :
191
+ # 400 in response to a token request indicates managed identity is disabled,
192
+ # or the identity with the specified client_id is not available
193
+ if ex .status_code == 400 :
194
+ self ._endpoint_available = False
195
+ message = "ManagedIdentityCredential authentication unavailable. "
196
+ if self ._identity_config :
197
+ message += "The requested identity has not been assigned to this resource."
198
+ else :
199
+ message += "No identity has been assigned to this resource."
200
+ six .raise_from (CredentialUnavailableError (message = message ), ex )
201
+
202
+ # any other error is unexpected
203
+ six .raise_from (ClientAuthenticationError (message = ex .message , response = ex .response ), None )
195
204
return token
196
205
197
206
@@ -227,16 +236,25 @@ def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument
227
236
228
237
token = self ._client .get_cached_token (scopes )
229
238
if not token :
230
- resource = scopes [0 ]
231
- if resource .endswith ("/.default" ):
232
- resource = resource [: - len ("/.default" )]
233
- secret = os .environ .get (EnvironmentVariables .MSI_SECRET )
234
- if secret :
235
- # MSI_ENDPOINT and MSI_SECRET set -> App Service
236
- token = self ._request_app_service_token (scopes = scopes , resource = resource , secret = secret )
237
- else :
238
- # only MSI_ENDPOINT set -> legacy-style MSI (Cloud Shell)
239
- token = self ._request_legacy_token (scopes = scopes , resource = resource )
239
+ token = self ._refresh_token (* scopes )
240
+ elif self ._client .should_refresh (token ):
241
+ try :
242
+ token = self ._refresh_token (* scopes )
243
+ except Exception : # pylint: disable=broad-except
244
+ pass
245
+ return token
246
+
247
+ def _refresh_token (self , * scopes ):
248
+ resource = scopes [0 ]
249
+ if resource .endswith ("/.default" ):
250
+ resource = resource [: - len ("/.default" )]
251
+ secret = os .environ .get (EnvironmentVariables .MSI_SECRET )
252
+ if secret :
253
+ # MSI_ENDPOINT and MSI_SECRET set -> App Service
254
+ token = self ._request_app_service_token (scopes = scopes , resource = resource , secret = secret )
255
+ else :
256
+ # only MSI_ENDPOINT set -> legacy-style MSI (Cloud Shell)
257
+ token = self ._request_legacy_token (scopes = scopes , resource = resource )
240
258
return token
241
259
242
260
def _request_app_service_token (self , scopes , resource , secret ):
0 commit comments