From a9e0b505d640bdb4b90952f4e69f169bdadafb16 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 10 Apr 2023 10:20:21 -0700 Subject: [PATCH] Update auth error message to specify args for `cargo login`. --- src/cargo/sources/registry/http_remote.rs | 1 + src/cargo/util/auth.rs | 10 ++- tests/testsuite/registry.rs | 82 +++++++++++++++++++++++ tests/testsuite/registry_auth.rs | 2 +- 4 files changed, 93 insertions(+), 2 deletions(-) diff --git a/src/cargo/sources/registry/http_remote.rs b/src/cargo/sources/registry/http_remote.rs index c0552734b33..ae89b629dfc 100644 --- a/src/cargo/sources/registry/http_remote.rs +++ b/src/cargo/sources/registry/http_remote.rs @@ -551,6 +551,7 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> { if self.auth_required { return Poll::Ready(err.context(auth::AuthorizationError { sid: self.source_id.clone(), + default_registry: self.config.default_registry()?, login_url: self.login_url.clone(), reason: auth::AuthorizationErrorReason::TokenRejected, })); diff --git a/src/cargo/util/auth.rs b/src/cargo/util/auth.rs index f45adc6b745..f19acaebe08 100644 --- a/src/cargo/util/auth.rs +++ b/src/cargo/util/auth.rs @@ -347,6 +347,8 @@ impl fmt::Display for AuthorizationErrorReason { pub struct AuthorizationError { /// Url that was attempted pub sid: SourceId, + /// The `registry.default` config value. + pub default_registry: Option, /// Url where the user could log in. pub login_url: Option, /// Specific reason indicating what failed @@ -356,9 +358,14 @@ impl Error for AuthorizationError {} impl fmt::Display for AuthorizationError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if self.sid.is_crates_io() { + let args = if self.default_registry.is_some() { + " --registry crates-io" + } else { + "" + }; write!( f, - "{}, please run `cargo login`\nor use environment variable CARGO_REGISTRY_TOKEN", + "{}, please run `cargo login{args}`\nor use environment variable CARGO_REGISTRY_TOKEN", self.reason ) } else if let Some(name) = self.sid.alt_registry_key() { @@ -421,6 +428,7 @@ pub fn auth_token( Some(token) => Ok(token.expose()), None => Err(AuthorizationError { sid: sid.clone(), + default_registry: config.default_registry()?, login_url: login_url.cloned(), reason: AuthorizationErrorReason::TokenMissing, } diff --git a/tests/testsuite/registry.rs b/tests/testsuite/registry.rs index 786d598d649..68a16d222b6 100644 --- a/tests/testsuite/registry.rs +++ b/tests/testsuite/registry.rs @@ -3192,3 +3192,85 @@ required by package `foo v0.0.1 ([ROOT]/foo)` ] ); } + +#[cargo_test] +fn default_auth_error() { + // Check for the error message for an authentication error when default is set. + let crates_io = RegistryBuilder::new().http_api().build(); + let _alternative = RegistryBuilder::new().http_api().alternative().build(); + + paths::home().join(".cargo/credentials.toml").rm_rf(); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + license = "MIT" + description = "foo" + "#, + ) + .file("src/lib.rs", "") + .build(); + + // Test output before setting the default. + p.cargo("publish --no-verify") + .replace_crates_io(crates_io.index_url()) + .with_stderr( + "\ +[UPDATING] crates.io index +error: no token found, please run `cargo login` +or use environment variable CARGO_REGISTRY_TOKEN +", + ) + .with_status(101) + .run(); + + p.cargo("publish --no-verify --registry alternative") + .replace_crates_io(crates_io.index_url()) + .with_stderr( + "\ +[UPDATING] `alternative` index +error: no token found for `alternative`, please run `cargo login --registry alternative` +or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN +", + ) + .with_status(101) + .run(); + + // Test the output with the default. + cargo_util::paths::append( + &cargo_home().join("config"), + br#" + [registry] + default = "alternative" + "#, + ) + .unwrap(); + + p.cargo("publish --no-verify") + .replace_crates_io(crates_io.index_url()) + .with_stderr( + "\ +[UPDATING] `alternative` index +error: no token found for `alternative`, please run `cargo login --registry alternative` +or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN +", + ) + .with_status(101) + .run(); + + p.cargo("publish --no-verify --registry crates-io") + .replace_crates_io(crates_io.index_url()) + .with_stderr( + "\ +[UPDATING] crates.io index +error: no token found, please run `cargo login --registry crates-io` +or use environment variable CARGO_REGISTRY_TOKEN +", + ) + .with_status(101) + .run(); +} diff --git a/tests/testsuite/registry_auth.rs b/tests/testsuite/registry_auth.rs index 3989dc9ec2f..27d967b98fb 100644 --- a/tests/testsuite/registry_auth.rs +++ b/tests/testsuite/registry_auth.rs @@ -1,4 +1,4 @@ -//! Tests for normal registry dependencies. +//! Tests for registry authentication. use cargo_test_support::registry::{Package, RegistryBuilder}; use cargo_test_support::{project, Execs, Project};