Skip to content

Commit f67bf0b

Browse files
Esteban PR feedback
1 parent c7770cd commit f67bf0b

5 files changed

Lines changed: 41 additions & 51 deletions

File tree

cloudsmith_cli/cli/commands/tokens.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def request_api_key(ctx, opts, save_config=False):
9999

100100
# Other errors - use the handler
101101
with handle_api_exceptions(ctx, opts=opts, context_msg=context_msg):
102-
raise
102+
raise exc
103103

104104

105105
@main.group(cls=command.AliasGroup, name="tokens")

cloudsmith_cli/cli/exceptions.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import click
88

99
from ..core.api.exceptions import ApiException
10-
from ..core.keyring import get_access_token, should_use_keyring
10+
from ..core.keyring import get_access_token
1111

1212

1313
@contextlib.contextmanager
@@ -170,10 +170,7 @@ def get_401_error_hint(ctx, opts, exc):
170170
"you don't have the permission to perform this action."
171171
)
172172

173-
# Only check keyring if enabled to avoid keyring prompts
174-
access_token = None
175-
if should_use_keyring():
176-
access_token = get_access_token(opts.api_host)
173+
access_token = get_access_token(opts.api_host)
177174
if access_token:
178175
return "Since you have an SSO access token set, this probably means that it has expired. Try getting a new token with 'cloudsmith auth', then try again."
179176

cloudsmith_cli/core/api/init.py

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from ...cli import saml
1010
from .. import keyring
11-
from ..keyring import should_use_keyring
1211
from ..rest import RestClient
1312
from .exceptions import ApiException
1413

@@ -48,55 +47,49 @@ def initialise_api(
4847

4948
# Use directly provided access token (e.g. from SSO callback),
5049
# or fall back to keyring lookup if enabled.
51-
token_from_keyring = False
52-
if not access_token and should_use_keyring():
50+
if not access_token:
5351
access_token = keyring.get_access_token(config.host)
54-
token_from_keyring = True
5552

5653
if access_token:
5754
auth_header = config.headers.get("Authorization")
5855

5956
# overwrite auth header if empty or is basic auth without username or password
6057
if not auth_header or auth_header == config.get_basic_auth_token():
61-
# Only attempt refresh for tokens retrieved from keyring.
62-
# Directly provided tokens (e.g. from SSO callback) are fresh
63-
# and don't need a refresh cycle.
64-
if token_from_keyring:
65-
refresh_token = keyring.get_refresh_token(config.host)
66-
67-
try:
68-
if keyring.should_refresh_access_token(config.host):
69-
new_access_token, new_refresh_token = saml.refresh_access_token(
70-
config.host,
71-
access_token,
72-
refresh_token,
73-
session=saml.create_configured_session(config),
74-
)
75-
keyring.store_sso_tokens(
76-
config.host, new_access_token, new_refresh_token
77-
)
78-
# Use the new tokens
79-
access_token = new_access_token
80-
except ApiException:
81-
keyring.update_refresh_attempted_at(config.host)
58+
refresh_token = keyring.get_refresh_token(config.host)
59+
60+
try:
61+
if keyring.should_refresh_access_token(config.host):
62+
new_access_token, new_refresh_token = saml.refresh_access_token(
63+
config.host,
64+
access_token,
65+
refresh_token,
66+
session=saml.create_configured_session(config),
67+
)
68+
keyring.store_sso_tokens(
69+
config.host, new_access_token, new_refresh_token
70+
)
71+
# Use the new tokens
72+
access_token = new_access_token
73+
except ApiException:
74+
keyring.update_refresh_attempted_at(config.host)
75+
76+
click.secho(
77+
"An error occurred when attempting to refresh your SSO access token. To refresh this session, run 'cloudsmith auth'",
78+
fg="yellow",
79+
err=True,
80+
)
8281

82+
# Clear access_token to prevent using expired token
83+
access_token = None
84+
85+
# Fall back to API key auth if available
86+
if key:
8387
click.secho(
84-
"An error occurred when attempting to refresh your SSO access token. To refresh this session, run 'cloudsmith auth'",
88+
"Falling back to API key authentication.",
8589
fg="yellow",
8690
err=True,
8791
)
88-
89-
# Clear access_token to prevent using expired token
90-
access_token = None
91-
92-
# Fall back to API key auth if available
93-
if key:
94-
click.secho(
95-
"Falling back to API key authentication.",
96-
fg="yellow",
97-
err=True,
98-
)
99-
config.api_key["X-Api-Key"] = key
92+
config.api_key["X-Api-Key"] = key
10093

10194
# Only use SSO token if refresh didn't fail
10295
if access_token:

cloudsmith_cli/core/download.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from . import keyring, ratelimits, utils
1212
from .api.exceptions import catch_raise_api_exception
1313
from .api.packages import get_packages_api, list_packages
14-
from .keyring import should_use_keyring
1514
from .rest import create_requests_session
1615

1716

@@ -40,9 +39,7 @@ def resolve_auth(
4039

4140
# Only attempt keyring operations if keyring is enabled
4241
config = cloudsmith_api.Configuration()
43-
access_token = None
44-
if should_use_keyring():
45-
access_token = keyring.get_access_token(config.host)
42+
access_token = keyring.get_access_token(config.host)
4643
api_key = api_key_opt or getattr(opts, "api_key", None)
4744

4845
if api_key:

cloudsmith_cli/core/keyring.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
def should_use_keyring():
1212
"""Check if keyring should be used based on CLOUDSMITH_NO_KEYRING env var."""
1313
env_value = os.environ.get("CLOUDSMITH_NO_KEYRING", "").strip().lower()
14-
if env_value in ("1", "true", "yes"):
15-
return False
16-
return True
14+
return env_value not in ("1", "true", "yes")
1715

1816

1917
ACCESS_TOKEN_REFRESH_ATTEMPTED_AT_KEY = (
@@ -45,6 +43,8 @@ def store_access_token(api_host, access_token):
4543

4644

4745
def get_access_token(api_host):
46+
if not should_use_keyring():
47+
return None
4848
key = ACCESS_TOKEN_KEY.format(api_host=api_host)
4949
return _get_value(key)
5050

@@ -73,6 +73,9 @@ def get_refresh_attempted_at(api_host):
7373

7474

7575
def should_refresh_access_token(api_host):
76+
if not should_use_keyring():
77+
return False
78+
7679
token_refreshed_at = get_refresh_attempted_at(api_host)
7780

7881
if token_refreshed_at:

0 commit comments

Comments
 (0)