mirror of
https://github.com/astral-sh/uv.git
synced 2025-11-19 11:35:36 +00:00
Avoid initiating login flow for invalid API keys (#15773)
## Summary If the login flow fails, and the user provided an API key, it's unintuitive to initiate the login flow.
This commit is contained in:
parent
b2c59a8ace
commit
ccf01fff66
2 changed files with 23 additions and 3 deletions
|
|
@ -261,11 +261,24 @@ impl PyxTokenStore {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `true` if the user appears to have an authentication token set.
|
||||||
|
pub fn has_auth_token(&self) -> bool {
|
||||||
|
read_pyx_auth_token().is_some()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns `true` if the user appears to have an API key set.
|
||||||
|
pub fn has_api_key(&self) -> bool {
|
||||||
|
read_pyx_api_key().is_some()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns `true` if the user appears to have OAuth tokens stored on disk.
|
||||||
|
pub fn has_oauth_tokens(&self) -> bool {
|
||||||
|
self.subdirectory.join("tokens.json").is_file()
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns `true` if the user appears to have credentials (which may be invalid).
|
/// Returns `true` if the user appears to have credentials (which may be invalid).
|
||||||
pub fn has_credentials(&self) -> bool {
|
pub fn has_credentials(&self) -> bool {
|
||||||
read_pyx_auth_token().is_some()
|
self.has_auth_token() || self.has_api_key() || self.has_oauth_tokens()
|
||||||
|| read_pyx_api_key().is_some()
|
|
||||||
|| self.subdirectory.join("tokens.json").is_file()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read the tokens from the store.
|
/// Read the tokens from the store.
|
||||||
|
|
|
||||||
|
|
@ -110,6 +110,13 @@ async fn pyx_refresh(store: &PyxTokenStore, client: &BaseClient, printer: Printe
|
||||||
|
|
||||||
// Similarly, if the refresh token expired, prompt for login.
|
// Similarly, if the refresh token expired, prompt for login.
|
||||||
Err(err) if err.is_unauthorized() => {
|
Err(err) if err.is_unauthorized() => {
|
||||||
|
if store.has_auth_token() {
|
||||||
|
return Err(
|
||||||
|
anyhow::Error::from(err).context("Failed to authenticate with access token")
|
||||||
|
);
|
||||||
|
} else if store.has_api_key() {
|
||||||
|
return Err(anyhow::Error::from(err).context("Failed to authenticate with API key"));
|
||||||
|
}
|
||||||
debug!(
|
debug!(
|
||||||
"Received 401 (Unauthorized) response from refresh endpoint; prompting for login..."
|
"Received 401 (Unauthorized) response from refresh endpoint; prompting for login..."
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue