|
8 | 8 |
|
9 | 9 | from ...cli import saml |
10 | 10 | from .. import keyring |
11 | | -from ..keyring import should_use_keyring |
12 | 11 | from ..rest import RestClient |
13 | 12 | from .exceptions import ApiException |
14 | 13 |
|
@@ -48,55 +47,49 @@ def initialise_api( |
48 | 47 |
|
49 | 48 | # Use directly provided access token (e.g. from SSO callback), |
50 | 49 | # 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: |
53 | 51 | access_token = keyring.get_access_token(config.host) |
54 | | - token_from_keyring = True |
55 | 52 |
|
56 | 53 | if access_token: |
57 | 54 | auth_header = config.headers.get("Authorization") |
58 | 55 |
|
59 | 56 | # overwrite auth header if empty or is basic auth without username or password |
60 | 57 | 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 | + ) |
82 | 81 |
|
| 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: |
83 | 87 | 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.", |
85 | 89 | fg="yellow", |
86 | 90 | err=True, |
87 | 91 | ) |
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 |
100 | 93 |
|
101 | 94 | # Only use SSO token if refresh didn't fail |
102 | 95 | if access_token: |
|
0 commit comments