Skip to content

Commit aa6c48f

Browse files
committed
Don't suggest cargo login for other credential providers
1 parent 8f12fe5 commit aa6c48f

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/cargo/util/auth/mod.rs

+23-12
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ pub struct AuthorizationError {
395395
pub login_url: Option<Url>,
396396
/// Specific reason indicating what failed
397397
reason: AuthorizationErrorReason,
398-
/// Should the _TOKEN environment variable name be included when displaying this error?
399-
display_token_env_help: bool,
398+
/// Should `cargo login` and the `_TOKEN` env var be included when displaying this error?
399+
supports_cargo_token_credential_provider: bool,
400400
}
401401

402402
impl AuthorizationError {
@@ -409,15 +409,16 @@ impl AuthorizationError {
409409
// Only display the _TOKEN environment variable suggestion if the `cargo:token` credential
410410
// provider is available for the source. Otherwise setting the environment variable will
411411
// have no effect.
412-
let display_token_env_help = credential_provider(gctx, &sid, false, false)?
413-
.iter()
414-
.any(|p| p.first().map(String::as_str) == Some("cargo:token"));
412+
let supports_cargo_token_credential_provider =
413+
credential_provider(gctx, &sid, false, false)?
414+
.iter()
415+
.any(|p| p.first().map(String::as_str) == Some("cargo:token"));
415416
Ok(AuthorizationError {
416417
sid,
417418
default_registry: gctx.default_registry()?,
418419
login_url,
419420
reason,
420-
display_token_env_help,
421+
supports_cargo_token_credential_provider,
421422
})
422423
}
423424
}
@@ -432,20 +433,30 @@ impl fmt::Display for AuthorizationError {
432433
""
433434
};
434435
write!(f, "{}, please run `cargo login{args}`", self.reason)?;
435-
if self.display_token_env_help {
436+
if self.supports_cargo_token_credential_provider {
436437
write!(f, "\nor use environment variable CARGO_REGISTRY_TOKEN")?;
437438
}
438439
Ok(())
439440
} else if let Some(name) = self.sid.alt_registry_key() {
440-
let key = ConfigKey::from_str(&format!("registries.{name}.token"));
441441
write!(
442442
f,
443-
"{} for `{}`, please run `cargo login --registry {name}`",
443+
"{} for `{}`",
444444
self.reason,
445-
self.sid.display_registry_name(),
445+
self.sid.display_registry_name()
446446
)?;
447-
if self.display_token_env_help {
448-
write!(f, "\nor use environment variable {}", key.as_env_key())?;
447+
if self.supports_cargo_token_credential_provider {
448+
let key = ConfigKey::from_str(&format!("registries.{name}.token"));
449+
write!(
450+
f,
451+
", please run `cargo login --registry {name}`\n\
452+
or use environment variable {}",
453+
key.as_env_key()
454+
)?;
455+
} else {
456+
write!(
457+
f,
458+
"\nYou may need to log in using this registry's credential provider"
459+
)?;
449460
}
450461
Ok(())
451462
} else if self.reason == AuthorizationErrorReason::TokenMissing {

tests/testsuite/credential_process.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ fn credential_provider_auth_failure() {
101101
.with_status(101)
102102
.with_stderr_data(str![[r#"
103103
[UPDATING] `alternative` index
104-
[ERROR] token rejected for `alternative`, please run `cargo login --registry alternative`
104+
[ERROR] token rejected for `alternative`
105+
You may need to log in using this registry's credential provider
105106
106107
Caused by:
107108
failed to get successful HTTP response from [..]

0 commit comments

Comments
 (0)