Skip to content

Commit fb933b3

Browse files
committed
Better handling of oidc_auth_renewer (based on refresh tokens)
1 parent 6177666 commit fb933b3

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

openeo/rest/connection.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -342,28 +342,32 @@ def _authenticate_oidc(
342342
*,
343343
provider_id: str,
344344
store_refresh_token: bool = False,
345-
fallback_refresh_token_to_store: Optional[str] = None,
345+
auto_renew_from_refresh_token: bool = False,
346+
fallback_refresh_token: Optional[str] = None,
346347
oidc_auth_renewer: Optional[OidcAuthenticator] = None,
347348
) -> Connection:
348349
"""
349350
Authenticate through OIDC and set up bearer token (based on OIDC access_token) for further requests.
350351
"""
351-
tokens = authenticator.get_tokens(request_refresh_token=store_refresh_token)
352+
request_refresh_token = store_refresh_token or (not oidc_auth_renewer and auto_renew_from_refresh_token)
353+
tokens = authenticator.get_tokens(request_refresh_token=request_refresh_token)
352354
_log.info("Obtained tokens: {t}".format(t=[k for k, v in tokens._asdict().items() if v]))
355+
356+
refresh_token = tokens.refresh_token or fallback_refresh_token
353357
if store_refresh_token:
354-
refresh_token = tokens.refresh_token or fallback_refresh_token_to_store
355358
if refresh_token:
356359
self._get_refresh_token_store().set_refresh_token(
357360
issuer=authenticator.provider_info.issuer,
358361
client_id=authenticator.client_id,
359362
refresh_token=refresh_token
360363
)
361-
if not oidc_auth_renewer:
362-
oidc_auth_renewer = OidcRefreshTokenAuthenticator(
363-
client_info=authenticator.client_info, refresh_token=refresh_token
364-
)
365364
else:
366365
_log.warning("No OIDC refresh token to store.")
366+
if not oidc_auth_renewer and auto_renew_from_refresh_token and refresh_token:
367+
oidc_auth_renewer = OidcRefreshTokenAuthenticator(
368+
client_info=authenticator.client_info, refresh_token=refresh_token
369+
)
370+
367371
token = tokens.access_token
368372
self.auth = OidcBearerAuth(provider_id=provider_id, access_token=token)
369373
self._oidc_auth_renewer = oidc_auth_renewer
@@ -452,7 +456,12 @@ def authenticate_oidc_resource_owner_password_credentials(
452456
authenticator = OidcResourceOwnerPasswordAuthenticator(
453457
client_info=client_info, username=username, password=password
454458
)
455-
return self._authenticate_oidc(authenticator, provider_id=provider_id, store_refresh_token=store_refresh_token)
459+
return self._authenticate_oidc(
460+
authenticator,
461+
provider_id=provider_id,
462+
store_refresh_token=store_refresh_token,
463+
oidc_auth_renewer=authenticator,
464+
)
456465

457466
def authenticate_oidc_refresh_token(
458467
self,
@@ -493,7 +502,7 @@ def authenticate_oidc_refresh_token(
493502
authenticator,
494503
provider_id=provider_id,
495504
store_refresh_token=store_refresh_token,
496-
fallback_refresh_token_to_store=refresh_token,
505+
fallback_refresh_token=refresh_token,
497506
oidc_auth_renewer=authenticator,
498507
)
499508

@@ -534,7 +543,13 @@ def authenticate_oidc_device(
534543
authenticator = OidcDeviceAuthenticator(
535544
client_info=client_info, use_pkce=use_pkce, max_poll_time=max_poll_time, **kwargs
536545
)
537-
return self._authenticate_oidc(authenticator, provider_id=provider_id, store_refresh_token=store_refresh_token)
546+
return self._authenticate_oidc(
547+
authenticator,
548+
provider_id=provider_id,
549+
store_refresh_token=store_refresh_token,
550+
# TODO: expose `auto_renew_from_refresh_token` directly as option instead of reusing `store_refresh_token` arg?
551+
auto_renew_from_refresh_token=store_refresh_token,
552+
)
538553

539554
def authenticate_oidc(
540555
self,
@@ -604,7 +619,8 @@ def authenticate_oidc(
604619
authenticator,
605620
provider_id=provider_id,
606621
store_refresh_token=store_refresh_token,
607-
fallback_refresh_token_to_store=refresh_token,
622+
fallback_refresh_token=refresh_token,
623+
oidc_auth_renewer=authenticator,
608624
)
609625
# TODO: pluggable/jupyter-aware display function?
610626
print("Authenticated using refresh token.")
@@ -622,6 +638,8 @@ def authenticate_oidc(
622638
authenticator,
623639
provider_id=provider_id,
624640
store_refresh_token=store_refresh_token,
641+
# TODO: expose `auto_renew_from_refresh_token` directly as option instead of reusing `store_refresh_token` arg?
642+
auto_renew_from_refresh_token=store_refresh_token,
625643
)
626644
print("Authenticated using device code flow.")
627645
return con

0 commit comments

Comments
 (0)