Skip to content

Commit 8d23372

Browse files
fix token cache for oauth
1 parent ee6e70a commit 8d23372

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

databricks/sdk/credentials_provider.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ def oauth_service_principal(cfg: 'Config') -> Optional[CredentialsProvider]:
167167
oidc = cfg.oidc_endpoints
168168
if oidc is None:
169169
return None
170+
170171
token_source = ClientCredentials(client_id=cfg.client_id,
171172
client_secret=cfg.client_secret,
172173
token_url=oidc.token_endpoint,
@@ -210,16 +211,21 @@ def external_browser(cfg: 'Config') -> Optional[CredentialsProvider]:
210211
credentials = token_cache.load()
211212
if credentials:
212213
# Force a refresh in case the loaded credentials are expired.
213-
credentials.token()
214-
else:
215-
oauth_client = OAuthClient(oidc_endpoints=oidc_endpoints,
216-
client_id=client_id,
217-
redirect_url=redirect_url,
218-
client_secret=client_secret)
219-
consent = oauth_client.initiate_consent()
220-
if not consent:
221-
return None
222-
credentials = consent.launch_external_browser()
214+
# If the refresh fails, rather than throw exception we will initiate a new OAuth login flow.
215+
try:
216+
credentials.token()
217+
return credentials(cfg)
218+
except Exception as e:
219+
logger.warning(f'Failed to refresh cached token: {e}, will init new OAuth login flow')
220+
221+
oauth_client = OAuthClient(oidc_endpoints=oidc_endpoints,
222+
client_id=client_id,
223+
redirect_url=redirect_url,
224+
client_secret=client_secret)
225+
consent = oauth_client.initiate_consent()
226+
if not consent:
227+
return None
228+
credentials = consent.launch_external_browser()
223229
token_cache.save(credentials)
224230
return credentials(cfg)
225231

0 commit comments

Comments
 (0)