Skip to content

Commit

Permalink
remove Secret<&str> in favor of &Secret<String>; remove Secret::as_de…
Browse files Browse the repository at this point in the history
…ref, Secret::owned
  • Loading branch information
kylematsuda committed Jan 18, 2023
1 parent 36cad92 commit 7468e68
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 46 deletions.
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ pub fn cli() -> Command {
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
ops::registry_login(
config,
args.get_one::<String>("token").map(|s| s.as_str().into()),
args.get_one::<String>("token")
.map(|s| s.clone().into())
.as_ref(),
args.get_one("registry").map(String::as_str),
args.flag("generate-keypair"),
args.flag("secret-key"),
Expand Down
18 changes: 9 additions & 9 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ impl RegistryCredentialConfig {
pub fn is_asymmetric_key(&self) -> bool {
matches!(self, Self::AsymmetricKey(..))
}
pub fn as_token(&self) -> Option<Secret<&str>> {
pub fn as_token(&self) -> Option<&Secret<String>> {
if let Self::Token(v) = self {
Some(v.as_deref())
Some(v)
} else {
None
}
Expand Down Expand Up @@ -174,7 +174,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {

let (mut registry, reg_ids) = registry(
opts.config,
opts.token.as_ref().map(Secret::as_deref),
opts.token.as_ref(),
opts.index.as_deref(),
publish_registry.as_deref(),
true,
Expand Down Expand Up @@ -512,7 +512,7 @@ fn wait_for_publish(
/// * `token_required`: If `true`, the token will be set.
fn registry(
config: &Config,
token_from_cmdline: Option<Secret<&str>>,
token_from_cmdline: Option<&Secret<String>>,
index: Option<&str>,
registry: Option<&str>,
force_update: bool,
Expand Down Expand Up @@ -786,7 +786,7 @@ fn http_proxy_exists(config: &Config) -> CargoResult<bool> {

pub fn registry_login(
config: &Config,
token: Option<Secret<&str>>,
token: Option<&Secret<String>>,
reg: Option<&str>,
generate_keypair: bool,
secret_key_required: bool,
Expand Down Expand Up @@ -852,7 +852,7 @@ pub fn registry_login(
.cloned()
.ok_or_else(|| anyhow!("need a secret_key to set a key_subject"))?;
}
if let Some(p) = paserk_public_from_paserk_secret(secret_key.as_deref()) {
if let Some(p) = paserk_public_from_paserk_secret(&secret_key) {
drop_println!(config, "{}", &p);
} else {
bail!("not a validly formated PASERK secret key");
Expand All @@ -866,7 +866,7 @@ pub fn registry_login(
));
} else {
new_token = RegistryCredentialConfig::Token(match token {
Some(token) => token.owned(),
Some(token) => token.clone(),
None => {
if let Some(login_url) = login_url {
drop_println!(
Expand Down Expand Up @@ -960,7 +960,7 @@ pub fn modify_owners(config: &Config, opts: &OwnersOptions) -> CargoResult<()> {

let (mut registry, _) = registry(
config,
opts.token.as_ref().map(Secret::as_deref),
opts.token.as_ref(),
opts.index.as_deref(),
opts.registry.as_deref(),
true,
Expand Down Expand Up @@ -1051,7 +1051,7 @@ pub fn yank(

let (mut registry, _) = registry(
config,
token.as_ref().map(Secret::as_deref),
token.as_ref(),
index.as_deref(),
reg.as_deref(),
true,
Expand Down
48 changes: 12 additions & 36 deletions src/cargo/util/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use serde::Deserialize;
use std::collections::HashMap;
use std::error::Error;
use std::io::{Read, Write};
use std::ops::Deref;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use time::format_description::well_known::Rfc3339;
Expand Down Expand Up @@ -40,21 +39,6 @@ impl<T> Secret<T> {
self.inner
}

/// Converts a `Secret<T>` to a `Secret<&T::Target>`.
///
/// For example, this can be used to convert from `&Secret<String>` to
/// `Secret<&str>`.
pub fn as_deref(&self) -> Secret<&<T as Deref>::Target>
where
T: Deref,
{
Secret::from(self.inner.deref())
}

pub fn as_ref(&self) -> Secret<&T> {
Secret::from(&self.inner)
}

pub fn map<U, F>(self, f: F) -> Secret<U>
where
F: FnOnce(T) -> U,
Expand All @@ -63,17 +47,6 @@ impl<T> Secret<T> {
}
}

impl<T: ToOwned + ?Sized> Secret<&T> {
/// Converts a `Secret` containing a borrowed type to a `Secret` containing the
/// corresponding owned type.
///
/// For example, this can be used to convert from `Secret<&str>` to
/// `Secret<String>`.
pub fn owned(&self) -> Secret<<T as ToOwned>::Owned> {
Secret::from(self.inner.to_owned())
}
}

impl<T, E> Secret<Result<T, E>> {
pub fn transpose(self) -> Result<Secret<T>, E> {
self.inner.map(|v| Secret::from(v))
Expand Down Expand Up @@ -377,14 +350,14 @@ my-registry = {{ index = "{}" }}
}

// Store a token in the cache for future calls.
pub fn cache_token(config: &Config, sid: &SourceId, token: Secret<&str>) {
pub fn cache_token(config: &Config, sid: &SourceId, token: &Secret<String>) {
let url = sid.canonical_url();
config.credential_cache().insert(
url.clone(),
CredentialCacheValue {
from_commandline: true,
independent_of_endpoint: true,
token_value: token.owned(),
token_value: token.clone(),
},
);
}
Expand Down Expand Up @@ -443,8 +416,8 @@ fn auth_token_optional(
let secret: Secret<AsymmetricSecretKey<pasetors::version3::V3>> =
secret_key.map(|key| key.as_str().try_into()).transpose()?;
let public: AsymmetricPublicKey<pasetors::version3::V3> = secret
.as_ref()
.map(|key| key.try_into())
.clone()
.map(|key| (&key).try_into())
.transpose()?
.expose();
let kip: pasetors::paserk::Id = (&public).try_into()?;
Expand Down Expand Up @@ -608,6 +581,7 @@ pub fn login(config: &Config, sid: &SourceId, token: RegistryCredentialConfig) -
let token = token
.as_token()
.expect("credential_process cannot use login with a secret_key")
.clone()
.expose()
.to_owned();
run_command(config, &process, sid, Action::Store(token))?;
Expand All @@ -620,12 +594,14 @@ pub fn login(config: &Config, sid: &SourceId, token: RegistryCredentialConfig) -
}

/// Checks that a secret key is valid, and returns the associated public key in Paserk format.
pub(crate) fn paserk_public_from_paserk_secret(secret_key: Secret<&str>) -> Option<String> {
let secret: Secret<AsymmetricSecretKey<pasetors::version3::V3>> =
secret_key.map(|key| key.try_into()).transpose().ok()?;
pub(crate) fn paserk_public_from_paserk_secret(secret_key: &Secret<String>) -> Option<String> {
let secret: Secret<AsymmetricSecretKey<pasetors::version3::V3>> = secret_key
.clone()
.map(|key| (&*key).try_into())
.transpose()
.ok()?;
let public: AsymmetricPublicKey<pasetors::version3::V3> = secret
.as_ref()
.map(|key| key.try_into())
.map(|key| (&key).try_into())
.transpose()
.ok()?
.expose();
Expand Down

0 comments on commit 7468e68

Please sign in to comment.