Skip to content

Commit f2db0bd

Browse files
committed
OIDC device code flow: hide progress bar on completed (or timed out) authentication
1 parent 15639db commit f2db0bd

File tree

5 files changed

+24
-19
lines changed

5 files changed

+24
-19
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Changed
1515

16+
- OIDC device code flow: hide progress bar on completed (or timed out) authentication
17+
1618
### Removed
1719

1820
### Fixed

openeo/rest/auth/oidc.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -737,11 +737,13 @@ def show_instructions(self, info: VerificationInfo) -> None:
737737
def set_status(self, status: str):
738738
self._status = status
739739

740-
def show_progress(self, status: Optional[str] = None):
740+
def show_progress(self, status: Optional[str] = None, include_bar: bool = True):
741741
if status:
742742
self.set_status(status)
743-
progress_bar = self._progress_bar.get(fraction=1.0 - self.elapsed() / self.timeout)
744-
text = f"{progress_bar} {self._status}"
743+
text = self._status
744+
if include_bar:
745+
progress_bar = self._progress_bar.get(fraction=1.0 - self.elapsed() / self.timeout)
746+
text = f"{progress_bar} {text}"
745747
self._display(f"{text[:self._max_width]: <{self._max_width}s}", end="\r")
746748

747749
def close(self):
@@ -775,13 +777,15 @@ def _instructions(self, info: VerificationInfo) -> str:
775777
def show_instructions(self, info: VerificationInfo) -> None:
776778
self._instructions_display.update({"text/html": self._instructions(info=info)}, raw=True)
777779

778-
def show_progress(self, status: Optional[str] = None):
779-
# TODO Add emoticons to status?
780+
def show_progress(self, status: Optional[str] = None, include_bar: bool = True):
780781
if status:
781782
self.set_status(status)
782-
progress_bar = self._progress_bar.get(fraction=1.0 - self.elapsed() / self.timeout)
783783
icon = self._status_icon(self._status)
784-
self._progress_display.update({"text/html": f"<code>{progress_bar}</code> {icon} {self._status}"}, raw=True)
784+
text = f"{icon} {self._status}"
785+
if include_bar:
786+
progress_bar = self._progress_bar.get(fraction=1.0 - self.elapsed() / self.timeout)
787+
text = f"<code>{progress_bar}</code> {text}"
788+
self._progress_display.update({"text/html": text}, raw=True)
785789

786790
def _status_icon(self, status: str) -> str:
787791
status = status.lower()
@@ -790,7 +794,7 @@ def _status_icon(self, status: str) -> str:
790794
elif "success" in status:
791795
return "\u2705" # Green check mark
792796
elif "timed out" in status:
793-
return "\u274C" # Red cross mark
797+
return "\u274C" # Red cross mark
794798
else:
795799
return ""
796800

@@ -906,8 +910,7 @@ def get_tokens(self, request_refresh_token: bool = False) -> AccessTokenResult:
906910
resp = self._requests.post(url=token_endpoint, data=post_data, timeout=5)
907911
if resp.status_code == 200:
908912
log.info(f"[{elapsed():5.1f}s] Authorized successfully.")
909-
poll_ui.show_progress(status="Authorized successfully")
910-
# TODO remove progress bar when authorized succesfully?
913+
poll_ui.show_progress(status="Authorized successfully", include_bar=False)
911914
return self._get_access_token_result(data=resp.json())
912915
else:
913916
try:
@@ -927,5 +930,5 @@ def get_tokens(self, request_refresh_token: bool = False) -> AccessTokenResult:
927930
)
928931
next_poll = elapsed() + poll_interval
929932

930-
poll_ui.show_progress(status="Timed out")
933+
poll_ui.show_progress(status="Timed out", include_bar=False)
931934
raise OidcDeviceCodePollTimeout(f"Timeout ({self._max_poll_time:.1f}s) while polling for access token.")

tests/rest/auth/test_cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def test_oidc_auth_device_flow(auth_config, refresh_token_store, requests_mock,
319319
"Using client ID 'z3-cl13nt'",
320320
f"Visit https://authit.test/dc and enter user code '{user_code}' to authenticate.",
321321
"[#####################################] Authorization pending \r[#####################################] Polling \r",
322-
"[####################################-] Authorized successfully \r\n",
322+
"Authorized successfully \r\n"
323323
"The OpenID Connect device flow was successful.",
324324
"Stored refresh token in {p!r}".format(p=str(refresh_token_store.path)),
325325
]
@@ -369,7 +369,7 @@ def test_oidc_auth_device_flow_default_client(
369369
"Will try to use default client.",
370370
f"Visit https://authit.test/dc and enter user code '{user_code}' to authenticate.",
371371
"[#####################################] Authorization pending \r[#####################################] Polling \r",
372-
"[####################################-] Authorized successfully \r\n",
372+
"Authorized successfully \r\n",
373373
"The OpenID Connect device flow was successful.",
374374
"Stored refresh token in {p!r}".format(p=str(refresh_token_store.path)),
375375
]
@@ -418,7 +418,7 @@ def test_oidc_auth_device_flow_no_config_all_defaults(
418418
"Will try to use default client.",
419419
f"Visit https://authit.test/dc and enter user code '{user_code}' to authenticate.",
420420
"[#####################################] Authorization pending \r[#####################################] Polling \r",
421-
"[####################################-] Authorized successfully \r\n",
421+
"Authorized successfully \r\n",
422422
"The OpenID Connect device flow was successful.",
423423
"Stored refresh token in {p!r}".format(p=str(refresh_token_store.path)),
424424
]

tests/rest/auth/test_oidc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def test_oidc_device_flow_with_client_secret(requests_mock, caplog, support_veri
407407

408408
assert display.lines[-3:] == [
409409
(1011, "[#################--------------------] Polling ", "\r"),
410-
(1011, "[#################--------------------] Authorized successfully ", "\r"),
410+
(1011, "Authorized successfully ", "\r"),
411411
(1011, "", "\n"),
412412
]
413413

@@ -475,7 +475,7 @@ def test_oidc_device_flow_with_pkce(requests_mock, caplog, support_verification_
475475

476476
assert display.lines[-3:] == [
477477
(1016, "[###########################----------] Polling ", "\r"),
478-
(1016, "[###########################----------] Authorized successfully ", "\r"),
478+
(1016, "Authorized successfully ", "\r"),
479479
(1016, "", "\n"),
480480
]
481481

@@ -552,7 +552,7 @@ def test_oidc_device_flow_without_pkce_nor_secret(
552552

553553
assert display.lines[-3:] == [
554554
(1043.5, "[##########################-----------] Polling ", "\r"),
555-
(1043.5, "[##########################-----------] Authorized successfully ", "\r"),
555+
(1043.5, "Authorized successfully ", "\r"),
556556
(1043.5, "", "\n"),
557557
]
558558

@@ -674,7 +674,7 @@ def test_oidc_device_flow_auto_detect(
674674

675675
assert display.lines[-3:] == [
676676
(1033, "[#################################----] Polling ", "\r"),
677-
(1033, "[#################################----] Authorized successfully ", "\r"),
677+
(1033, "Authorized successfully ", "\r"),
678678
(1033, "", "\n"),
679679
]
680680

tests/rest/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def assert_oidc_device_code_flow(url: str = "https://oidc.test/dc", elapsed: flo
6464
stdout, _ = capsys.readouterr()
6565
assert f"Visit {url} and enter" in stdout
6666
assert re.search(r"\[#+-*\] Authorization pending *\r\[#+-*\] Polling *\r", stdout)
67-
assert re.search(r"\[#+-*\] Authorized successfully *\r\n", stdout)
67+
assert re.search(r"Authorized successfully *\r\n", stdout)
6868
assert time_machine.coordinates.time() - start >= elapsed
6969

7070
return assert_oidc_device_code_flow

0 commit comments

Comments
 (0)