Skip to content

Commit c4eb670

Browse files
committed
remove duplication in auth
1 parent 2ac1508 commit c4eb670

File tree

1 file changed

+59
-56
lines changed

1 file changed

+59
-56
lines changed

src/cargo/util/auth.rs

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -55,39 +55,14 @@ pub fn registry_credential_config(
5555
secret_key_subject,
5656
..
5757
} = config.get::<RegistryConfig>("registry")?;
58-
let credential_process =
59-
credential_process.filter(|_| config.cli_unstable().credential_process);
60-
let secret_key = secret_key.filter(|_| config.cli_unstable().registry_auth);
61-
let secret_key_subject = secret_key_subject.filter(|_| config.cli_unstable().registry_auth);
62-
63-
let err_both = |token_key: &str, proc_key: &str| {
64-
Err(format_err!(
65-
"both `{token_key}` and `{proc_key}` \
66-
were specified in the config`.\n\
67-
Only one of these values may be set, remove one or the other to proceed.",
68-
))
69-
};
70-
return Ok(
71-
match (token, credential_process, secret_key, secret_key_subject) {
72-
(Some(_), Some(_), _, _) => return err_both("token", "credential-process"),
73-
(Some(_), _, Some(_), _) => return err_both("token", "secret-key"),
74-
(_, Some(_), Some(_), _) => return err_both("credential-process", "secret-key"),
75-
(_, _, None, Some(_)) => {
76-
return Err(format_err!(
77-
"`secret-key-subject` was set but `secret-key` was not in the config.\n\
78-
Ether set the `secret-key` or remove the `secret-key-subject`."
79-
));
80-
}
81-
(Some(token), _, _, _) => RegistryCredentialConfig::Token(token),
82-
(_, Some(process), _, _) => RegistryCredentialConfig::Process((
83-
process.path.resolve_program(config),
84-
process.args,
85-
)),
86-
(None, None, Some(key), subject) => {
87-
RegistryCredentialConfig::AsymmetricKey((key, subject))
88-
}
89-
(None, None, None, _) => RegistryCredentialConfig::None,
90-
},
58+
return registry_credential_config_iner(
59+
true,
60+
None,
61+
token,
62+
credential_process,
63+
secret_key,
64+
secret_key_subject,
65+
config,
9166
);
9267
}
9368

@@ -165,23 +140,46 @@ pub fn registry_credential_config(
165140
credential_process,
166141
..
167142
} = config.get::<RegistryConfig>(&format!("registries.{name}"))?;
168-
let credential_process =
169-
credential_process.filter(|_| config.cli_unstable().credential_process);
170-
let secret_key = secret_key.filter(|_| config.cli_unstable().registry_auth);
171-
let secret_key_subject = secret_key_subject.filter(|_| config.cli_unstable().registry_auth);
172143
(token, credential_process, secret_key, secret_key_subject)
173144
} else {
174145
log::debug!("no registry name found for {sid}");
175146
(None, None, None, None)
176147
};
177148

178-
let name = name.as_deref();
149+
registry_credential_config_iner(
150+
false,
151+
name.as_deref(),
152+
token,
153+
credential_process,
154+
secret_key,
155+
secret_key_subject,
156+
config,
157+
)
158+
}
159+
160+
fn registry_credential_config_iner(
161+
is_crates_io: bool,
162+
name: Option<&str>,
163+
token: Option<String>,
164+
credential_process: Option<config::PathAndArgs>,
165+
secret_key: Option<String>,
166+
secret_key_subject: Option<String>,
167+
config: &Config,
168+
) -> CargoResult<RegistryCredentialConfig> {
169+
let credential_process =
170+
credential_process.filter(|_| config.cli_unstable().credential_process);
171+
let secret_key = secret_key.filter(|_| config.cli_unstable().registry_auth);
172+
let secret_key_subject = secret_key_subject.filter(|_| config.cli_unstable().registry_auth);
179173
let err_both = |token_key: &str, proc_key: &str| {
174+
let registry = if is_crates_io {
175+
"".to_string()
176+
} else {
177+
format!(" for registry `{}`", name.unwrap_or("UN-NAMED"))
178+
};
180179
Err(format_err!(
181180
"both `{token_key}` and `{proc_key}` \
182-
were specified in the config for registry `{name}`.\n\
183-
Only one of these values may be set, remove one or the other to proceed.",
184-
name = name.unwrap()
181+
were specified in the config{registry}.\n\
182+
Only one of these values may be set, remove one or the other to proceed.",
185183
))
186184
};
187185
Ok(
@@ -190,11 +188,15 @@ pub fn registry_credential_config(
190188
(Some(_), _, Some(_), _) => return err_both("token", "secret-key"),
191189
(_, Some(_), Some(_), _) => return err_both("credential-process", "secret-key"),
192190
(_, _, None, Some(_)) => {
191+
let registry = if is_crates_io {
192+
"".to_string()
193+
} else {
194+
format!(" for registry `{}`", name.as_ref().unwrap())
195+
};
193196
return Err(format_err!(
194-
"`secret-key-subject` was set but `secret-key` was not in the config \
195-
for registry `{}`.\n\
196-
Ether set the `secret-key` or remove the `secret-key-subject`.",
197-
name.unwrap()
197+
"`secret-key-subject` was set but `secret-key` was not in the config{}.\n\
198+
Ether set the `secret-key` or remove the `secret-key-subject`.",
199+
registry
198200
));
199201
}
200202
(Some(token), _, _, _) => RegistryCredentialConfig::Token(token),
@@ -206,18 +208,19 @@ pub fn registry_credential_config(
206208
RegistryCredentialConfig::AsymmetricKey((key, subject))
207209
}
208210
(None, None, None, _) => {
209-
// If we couldn't find a registry-specific credential, try the global credential process.
210-
if let Some(process) = config
211-
.get::<Option<config::PathAndArgs>>("registry.credential-process")?
212-
.filter(|_| config.cli_unstable().credential_process)
213-
{
214-
RegistryCredentialConfig::Process((
215-
process.path.resolve_program(config),
216-
process.args,
217-
))
218-
} else {
219-
RegistryCredentialConfig::None
211+
if !is_crates_io {
212+
// If we couldn't find a registry-specific credential, try the global credential process.
213+
if let Some(process) = config
214+
.get::<Option<config::PathAndArgs>>("registry.credential-process")?
215+
.filter(|_| config.cli_unstable().credential_process)
216+
{
217+
return Ok(RegistryCredentialConfig::Process((
218+
process.path.resolve_program(config),
219+
process.args,
220+
)));
221+
}
220222
}
223+
RegistryCredentialConfig::None
221224
}
222225
},
223226
)

0 commit comments

Comments
 (0)