Skip to content

Commit 2b0b4ba

Browse files
committed
Merge branch 'trs/remotes-review-feedback'
2 parents 362cfad + a753a47 commit 2b0b4ba

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ development source code and as such may not be routinely kept up to date.
1313

1414
# __NEXT__
1515

16+
## Improvements
17+
18+
* Several kinds of errors from `nextstrain login` and `nextstrain whoami`
19+
related to their interactions with a remote server are now clearer.
20+
([#347](https://github.com/nextstrain/cli/pull/347))
21+
1622

1723
# 8.0.0 (18 January 2024)
1824

nextstrain/cli/authn/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,13 @@ def login(origin: Origin, credentials: Optional[Callable[[], Tuple[str, str]]] =
100100

101101
def renew(origin: Origin) -> Optional[User]:
102102
"""
103-
Renews existing tokens for *origin*, if possible.
103+
Renews existing saved credentials for *origin*, if possible.
104104
105105
Returns a :class:`User` object with renewed information about the logged in
106106
user when successful.
107107
108-
Raises a :class:`UserError` if authentication fails.
108+
Returns ``None`` if there are no saved credentials or if they're unable to
109+
be automatically renewed.
109110
"""
110111
assert origin
111112

@@ -183,9 +184,15 @@ def current_user(origin: Origin) -> Optional[User]:
183184
"""
184185
assert origin
185186

186-
session = Session(origin)
187187
tokens = _load_tokens(origin)
188188

189+
# Short-circuit if we don't have any tokens to speak of. Avoids trying to
190+
# fetch authn metadata from the remote origin.
191+
if all(token is None for token in tokens.values()):
192+
return None
193+
194+
session = Session(origin)
195+
189196
try:
190197
try:
191198
session.verify_tokens(**tokens)

nextstrain/cli/authn/configuration.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,31 @@ def openid_configuration(origin: Origin):
2020
assert origin
2121

2222
with requests.Session() as http:
23-
response = http.get(origin.rstrip("/") + "/.well-known/openid-configuration")
23+
try:
24+
response = http.get(origin.rstrip("/") + "/.well-known/openid-configuration")
25+
response.raise_for_status()
26+
return response.json()
2427

25-
if response.status_code == 404:
28+
except requests.exceptions.ConnectionError as err:
2629
raise UserError(f"""
27-
Failed to retrieve authentication metadata for {origin}.
30+
Could not connect to {origin} to retrieve
31+
authentication metadata:
32+
33+
{type(err).__name__}: {err}
34+
35+
That remote may be invalid or you may be experiencing network
36+
connectivity issues.
37+
""") from err
38+
39+
except (requests.exceptions.HTTPError, requests.exceptions.JSONDecodeError) as err:
40+
raise UserError(f"""
41+
Failed to retrieve authentication metadata for {origin}:
42+
43+
{type(err).__name__}: {err}
2844
2945
That remote seems unlikely to be an alternate nextstrain.org
3046
instance or an internal Nextstrain Groups Server instance.
31-
""")
32-
33-
response.raise_for_status()
34-
return response.json()
47+
""") from err
3548

3649

3750
def client_configuration(origin: Origin):

0 commit comments

Comments
 (0)