Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Initiate a new OAuth external browser login flow in case of refresh token exception #823

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions databricks/sdk/credentials_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def oauth_service_principal(cfg: 'Config') -> Optional[CredentialsProvider]:
oidc = cfg.oidc_endpoints
if oidc is None:
return None

token_source = ClientCredentials(client_id=cfg.client_id,
client_secret=cfg.client_secret,
token_url=oidc.token_endpoint,
Expand Down Expand Up @@ -210,16 +211,22 @@ def external_browser(cfg: 'Config') -> Optional[CredentialsProvider]:
credentials = token_cache.load()
if credentials:
# Force a refresh in case the loaded credentials are expired.
credentials.token()
else:
oauth_client = OAuthClient(oidc_endpoints=oidc_endpoints,
client_id=client_id,
redirect_url=redirect_url,
client_secret=client_secret)
consent = oauth_client.initiate_consent()
if not consent:
return None
credentials = consent.launch_external_browser()
# If the refresh fails, rather than throw exception we will initiate a new OAuth login flow.
try:
credentials.token()
return credentials(cfg)
# TODO: we should ideally use more specific exceptions.
except Exception as e:
eric-wang-1990 marked this conversation as resolved.
Show resolved Hide resolved
logger.warning(f'Failed to refresh cached token: {e}. Initiating new OAuth login flow')

oauth_client = OAuthClient(oidc_endpoints=oidc_endpoints,
client_id=client_id,
redirect_url=redirect_url,
client_secret=client_secret)
consent = oauth_client.initiate_consent()
if not consent:
return None
credentials = consent.launch_external_browser()
token_cache.save(credentials)
return credentials(cfg)

Expand Down
Loading