Skip to content

Commit 1c52bbd

Browse files
committed
add CredentialCacheValue
1 parent 5e709d4 commit 1c52bbd

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

src/cargo/util/auth.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use url::Url;
1919
use crate::core::SourceId;
2020
use crate::ops::RegistryCredentialConfig;
2121

22+
use super::config::CredentialCacheValue;
23+
2224
/// Get the credential configuration for a `SourceId`.
2325
pub fn registry_credential_config(
2426
config: &Config,
@@ -298,9 +300,13 @@ my-registry = {{ index = "{}" }}
298300
// Store a token in the cache for future calls.
299301
pub fn cache_token(config: &Config, sid: &SourceId, token: &str) {
300302
let url = sid.canonical_url();
301-
config
302-
.credential_cache()
303-
.insert(url.clone(), (true, token.to_string()));
303+
config.credential_cache().insert(
304+
url.clone(),
305+
CredentialCacheValue {
306+
from_commandline: true,
307+
token_value: token.to_string(),
308+
},
309+
);
304310
}
305311

306312
/// Returns the token to use for the given registry.
@@ -332,11 +338,11 @@ fn auth_token_optional(
332338
let mut cache = config.credential_cache();
333339
let url = sid.canonical_url();
334340

335-
if let Some((overridden_on_commandline, token)) = cache.get(url) {
341+
if let Some(cache_token_value) = cache.get(url) {
336342
// Tokens for endpoints that do not involve a mutation can always be reused.
337343
// If the value is put in the cach by the command line, then we reuse it without looking at the configuration.
338-
if *overridden_on_commandline || mutation.is_none() {
339-
return Ok(Some(token.clone()));
344+
if cache_token_value.from_commandline || mutation.is_none() {
345+
return Ok(Some(cache_token_value.token_value.clone()));
340346
}
341347
}
342348

@@ -417,7 +423,13 @@ fn auth_token_optional(
417423
};
418424

419425
if mutation.is_none() {
420-
cache.insert(url.clone(), (false, token.clone()));
426+
cache.insert(
427+
url.clone(),
428+
CredentialCacheValue {
429+
from_commandline: false,
430+
token_value: token.to_string(),
431+
},
432+
);
421433
}
422434
Ok(Some(token))
423435
}

src/cargo/util/config/mod.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ enum WhyLoad {
136136
FileDiscovery,
137137
}
138138

139+
/// A previously generated authentication token and the data needed to determine if it can be reused.
140+
pub struct CredentialCacheValue {
141+
/// If the command line was used to override the token then it must always be reused,
142+
/// even if reading the configuration files would lead to a different value.
143+
pub from_commandline: bool,
144+
pub token_value: String,
145+
}
146+
147+
impl fmt::Debug for CredentialCacheValue {
148+
/// This manual implementation helps ensure that the token value is redacted from all logs.
149+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150+
f.debug_struct("CredentialCacheValue")
151+
.field("from_commandline", &self.from_commandline)
152+
.field("token_value", &"REDACTED")
153+
.finish()
154+
}
155+
}
156+
139157
/// Configuration information for cargo. This is not specific to a build, it is information
140158
/// relating to cargo itself.
141159
#[derive(Debug)]
@@ -193,7 +211,7 @@ pub struct Config {
193211
updated_sources: LazyCell<RefCell<HashSet<SourceId>>>,
194212
/// Cache of credentials from configuration or credential providers.
195213
/// Maps from url to credential value.
196-
credential_cache: LazyCell<RefCell<HashMap<CanonicalUrl, (bool, String)>>>,
214+
credential_cache: LazyCell<RefCell<HashMap<CanonicalUrl, CredentialCacheValue>>>,
197215
/// Lock, if held, of the global package cache along with the number of
198216
/// acquisitions so far.
199217
package_cache_lock: RefCell<Option<(Option<FileLock>, usize)>>,
@@ -468,7 +486,7 @@ impl Config {
468486
}
469487

470488
/// Cached credentials from credential providers or configuration.
471-
pub fn credential_cache(&self) -> RefMut<'_, HashMap<CanonicalUrl, (bool, String)>> {
489+
pub fn credential_cache(&self) -> RefMut<'_, HashMap<CanonicalUrl, CredentialCacheValue>> {
472490
self.credential_cache
473491
.borrow_with(|| RefCell::new(HashMap::new()))
474492
.borrow_mut()

0 commit comments

Comments
 (0)