Skip to content

Commit e50589b

Browse files
committed
cache more tokens
1 parent cd96709 commit e50589b

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/cargo/util/auth.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ pub fn cache_token(config: &Config, sid: &SourceId, token: &str) {
304304
url.clone(),
305305
CredentialCacheValue {
306306
from_commandline: true,
307+
independent_of_endpoint: true,
307308
token_value: token.to_string(),
308309
},
309310
);
@@ -341,15 +342,18 @@ fn auth_token_optional(
341342
if let Some(cache_token_value) = cache.get(url) {
342343
// Tokens for endpoints that do not involve a mutation can always be reused.
343344
// If the value is put in the cach by the command line, then we reuse it without looking at the configuration.
344-
if cache_token_value.from_commandline || mutation.is_none() {
345+
if cache_token_value.from_commandline
346+
|| cache_token_value.independent_of_endpoint
347+
|| mutation.is_none()
348+
{
345349
return Ok(Some(cache_token_value.token_value.clone()));
346350
}
347351
}
348352

349353
let credential = registry_credential_config(config, sid)?;
350-
let token = match credential {
354+
let (independent_of_endpoint, token) = match credential {
351355
RegistryCredentialConfig::None => return Ok(None),
352-
RegistryCredentialConfig::Token(config_token) => config_token.to_string(),
356+
RegistryCredentialConfig::Token(config_token) => (true, config_token.to_string()),
353357
RegistryCredentialConfig::Process(process) => {
354358
// todo: PASETO with process
355359
run_command(config, &process, sid, Action::Get)?.unwrap()
@@ -407,18 +411,21 @@ fn auth_token_optional(
407411
kip,
408412
};
409413

410-
pasetors::version3::PublicToken::sign(
411-
&secret,
412-
serde_json::to_string(&message)
413-
.expect("cannot serialize")
414-
.as_bytes(),
415-
Some(
416-
serde_json::to_string(&footer)
414+
(
415+
false,
416+
pasetors::version3::PublicToken::sign(
417+
&secret,
418+
serde_json::to_string(&message)
417419
.expect("cannot serialize")
418420
.as_bytes(),
419-
),
420-
None,
421-
)?
421+
Some(
422+
serde_json::to_string(&footer)
423+
.expect("cannot serialize")
424+
.as_bytes(),
425+
),
426+
None,
427+
)?,
428+
)
422429
}
423430
};
424431

@@ -427,6 +434,7 @@ fn auth_token_optional(
427434
url.clone(),
428435
CredentialCacheValue {
429436
from_commandline: false,
437+
independent_of_endpoint,
430438
token_value: token.to_string(),
431439
},
432440
);
@@ -527,7 +535,7 @@ fn run_command(
527535
process: &(PathBuf, Vec<String>),
528536
sid: &SourceId,
529537
action: Action,
530-
) -> CargoResult<Option<String>> {
538+
) -> CargoResult<Option<(bool, String)>> {
531539
let index_url = sid.url().as_str();
532540
let cred_proc;
533541
let (exe, args) = if process.0.to_str().unwrap_or("").starts_with("cargo:") {
@@ -552,6 +560,8 @@ fn run_command(
552560
Action::Erase => bail!(msg("log out")),
553561
}
554562
}
563+
// todo: PASETO with process
564+
let independent_of_endpoint = false;
555565
let action_str = match action {
556566
Action::Get => "get",
557567
Action::Store(_) => "store",
@@ -622,7 +632,7 @@ fn run_command(
622632
}
623633
buffer.truncate(end);
624634
}
625-
token = Some(buffer);
635+
token = Some((independent_of_endpoint, buffer));
626636
}
627637
Action::Store(token) => {
628638
writeln!(child.stdin.as_ref().unwrap(), "{}", token).with_context(|| {

src/cargo/util/config/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ pub struct CredentialCacheValue {
141141
/// If the command line was used to override the token then it must always be reused,
142142
/// even if reading the configuration files would lead to a different value.
143143
pub from_commandline: bool,
144+
/// If nothing depends on which endpoint is being hit, then we can reuse the token
145+
/// for any future request even if some of the requests involve mutations.
146+
pub independent_of_endpoint: bool,
144147
pub token_value: String,
145148
}
146149

0 commit comments

Comments
 (0)