From 0a5050c0d074b42fcc31035a904bca0554e4ac28 Mon Sep 17 00:00:00 2001 From: Arlo Siemsen Date: Tue, 29 Aug 2023 11:28:02 -0500 Subject: [PATCH] Improve logout message for asymmetric tokens --- src/cargo/util/credential/paseto.rs | 13 +++++++++++-- tests/testsuite/logout.rs | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/cargo/util/credential/paseto.rs b/src/cargo/util/credential/paseto.rs index 329d1f11c74..bd49af98acd 100644 --- a/src/cargo/util/credential/paseto.rs +++ b/src/cargo/util/credential/paseto.rs @@ -200,8 +200,17 @@ impl<'a> Credential for PasetoCredential<'a> { Ok(CredentialResponse::Login) } Action::Logout => { - config::save_credentials(self.config, None, &sid)?; - Ok(CredentialResponse::Logout) + if reg_cfg.and_then(|c| c.secret_key).is_some() { + config::save_credentials(self.config, None, &sid)?; + let reg_name = sid.display_registry_name(); + let _ = self.config.shell().status( + "Logout", + format!("secret-key for `{reg_name}` has been removed from local storage"), + ); + Ok(CredentialResponse::Logout) + } else { + Err(Error::NotFound) + } } _ => Err(Error::OperationNotSupported), } diff --git a/tests/testsuite/logout.rs b/tests/testsuite/logout.rs index 7b5e10de2bf..58af2611de8 100644 --- a/tests/testsuite/logout.rs +++ b/tests/testsuite/logout.rs @@ -102,3 +102,20 @@ fn default_registry_configured() { .with_stderr("[LOGOUT] not currently logged in to `dummy-registry`") .run(); } + +#[cargo_test] +fn logout_asymmetric() { + let _registry = registry::RegistryBuilder::new() + .token(cargo_test_support::registry::Token::rfc_key()) + .build(); + + cargo_process("logout --registry crates-io -Zasymmetric-token") + .masquerade_as_nightly_cargo(&["asymmetric-token"]) + .with_stderr("[LOGOUT] secret-key for `crates-io` has been removed from local storage") + .run(); + + cargo_process("logout --registry crates-io -Zasymmetric-token") + .masquerade_as_nightly_cargo(&["asymmetric-token"]) + .with_stderr("[LOGOUT] not currently logged in to `crates-io`") + .run(); +}