Skip to content

Commit

Permalink
fixup! fixup! fixup! Issue #237 WIP 3
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Apr 26, 2023
1 parent 0c346a9 commit a9a47fe
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 101 deletions.
70 changes: 38 additions & 32 deletions openeo/rest/auth/oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import base64
import contextlib
import enum
import functools
import hashlib
Expand Down Expand Up @@ -712,8 +713,10 @@ def show_progress(self, status: Optional[str] = None):
if status:
self.set_status(status)
col_width = (self._max_width - 1) // 2
self._display(f"{self._progress_bar(width=col_width)} {self._status[:col_width]}", end="\r")
self._display(f"{self._progress_bar(width=col_width)} {self._status[:col_width]: <{col_width}s}", end="\r")

def close(self):
self._display("", end="\n")

class _JupyterDeviceCodePollUi(_BasicDeviceCodePollUi):
def __init__(
Expand Down Expand Up @@ -745,6 +748,8 @@ def show_progress(self, status: Optional[str] = None):
self.set_status(status)
self._progress_display.update({"text/html": f"<code>{self._progress_bar()}</code> {self._status}"}, raw=True)

def close(self):
pass

class OidcDeviceAuthenticator(OidcAuthenticator):
"""
Expand Down Expand Up @@ -832,36 +837,37 @@ def get_tokens(self, request_refresh_token: bool = False) -> AccessTokenResult:
poll_ui = _BasicDeviceCodePollUi(timeout=self._max_poll_time, elapsed=elapsed, display=self._display)
poll_ui.show_instructions(info=verification_info)

while elapsed() <= self._max_poll_time:
poll_ui.show_progress()
time.sleep(sleep)
with contextlib.closing(poll_ui):
while elapsed() <= self._max_poll_time:
poll_ui.show_progress()
time.sleep(sleep)

if elapsed() > next_poll:
log.debug(
f"Doing {self.grant_type!r} token request {token_endpoint!r} with post data fields {list(post_data.keys())!r} (client_id {self.client_id!r})"
)
poll_ui.show_progress(status="Polling")
resp = self._requests.post(url=token_endpoint, data=post_data)
if resp.status_code == 200:
log.info(f"[{elapsed():5.1f}s] Authorized successfully.")
poll_ui.show_progress(status="Authorized successfully")
return self._get_access_token_result(data=resp.json())
else:
try:
error = resp.json()["error"]
except Exception:
error = "unknown"
log.info(f"[{elapsed():5.1f}s] not authorized yet: {error=}")
if error == "authorization_pending":
poll_ui.show_progress(status="Authorization pending")
elif error == "slow_down":
poll_ui.show_progress(status="Slowing down")
poll_interval += 5
if elapsed() >= next_poll:
log.debug(
f"Doing {self.grant_type!r} token request {token_endpoint!r} with post data fields {list(post_data.keys())!r} (client_id {self.client_id!r})"
)
poll_ui.show_progress(status="Polling")
resp = self._requests.post(url=token_endpoint, data=post_data)
if resp.status_code == 200:
log.info(f"[{elapsed():5.1f}s] Authorized successfully.")
poll_ui.show_progress(status="Authorized successfully")
return self._get_access_token_result(data=resp.json())
else:
raise OidcException(
f"Failed to retrieve access token at {token_endpoint!r}: {resp.status_code} {resp.reason!r} {resp.text!r}"
)
next_poll = elapsed() + poll_interval

poll_ui.show_progress(status="Timed out")
raise OidcException(f"Timeout ({self._max_poll_time:.1f}s) while polling for access token.")
try:
error = resp.json()["error"]
except Exception:
error = "unknown"
log.info(f"[{elapsed():5.1f}s] not authorized yet: {error=}")
if error == "authorization_pending":
poll_ui.show_progress(status="Authorization pending")
elif error == "slow_down":
poll_ui.show_progress(status="Slowing down")
poll_interval += 5
else:
raise OidcException(
f"Failed to retrieve access token at {token_endpoint!r}: {resp.status_code} {resp.reason!r} {resp.text!r}"
)
next_poll = elapsed() + poll_interval

poll_ui.show_progress(status="Timed out")
raise OidcException(f"Timeout ({self._max_poll_time:.1f}s) while polling for access token.")
Loading

0 comments on commit a9a47fe

Please sign in to comment.