Skip to content

Commit 487983c

Browse files
fix(auth); add auth scheme hint to token rejected error for alt
registries
1 parent db6ed51 commit 487983c

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/cargo/util/auth/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ pub struct AuthorizationError {
400400
reason: AuthorizationErrorReason,
401401
/// Should `cargo login` and the `_TOKEN` env var be included when displaying this error?
402402
supports_cargo_token_credential_provider: bool,
403+
/// Whether the cached token appears to lack an authentication scheme (no space found).
404+
token_lacks_scheme: Option<bool>,
403405
}
404406

405407
impl AuthorizationError {
@@ -416,12 +418,17 @@ impl AuthorizationError {
416418
credential_provider(gctx, &sid, false, false)?
417419
.iter()
418420
.any(|p| p.first().map(String::as_str) == Some("cargo:token"));
421+
let cache = gctx.credential_cache();
422+
let token_lacks_scheme = cache
423+
.get(sid.canonical_url())
424+
.map(|entry| !entry.token_value.as_deref().expose().contains(' '));
419425
Ok(AuthorizationError {
420426
sid,
421427
default_registry: gctx.default_registry()?,
422428
login_url,
423429
reason,
424430
supports_cargo_token_credential_provider,
431+
token_lacks_scheme,
425432
})
426433
}
427434
}
@@ -461,6 +468,15 @@ impl fmt::Display for AuthorizationError {
461468
"\nYou may need to log in using this registry's credential provider"
462469
)?;
463470
}
471+
472+
if self.reason == AuthorizationErrorReason::TokenRejected {
473+
if self.token_lacks_scheme == Some(true) {
474+
write!(
475+
f,
476+
"\nnote: the token does not include an authentication scheme"
477+
)?;
478+
}
479+
}
464480
Ok(())
465481
} else if self.reason == AuthorizationErrorReason::TokenMissing {
466482
write!(

tests/testsuite/credential_process.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ fn credential_provider_auth_failure() {
104104
[UPDATING] `alternative` index
105105
[ERROR] token rejected for `alternative`
106106
You may need to log in using this registry's credential provider
107+
[NOTE] the token does not include an authentication scheme
107108
108109
Caused by:
109110
failed to get successful HTTP response from [..]
@@ -629,7 +630,7 @@ fn basic_provider() {
629630
eprintln!("CARGO={:?}", std::env::var("CARGO").ok());
630631
eprintln!("CARGO_REGISTRY_NAME_OPT={:?}", std::env::var("CARGO_REGISTRY_NAME_OPT").ok());
631632
eprintln!("CARGO_REGISTRY_INDEX_URL={:?}", std::env::var("CARGO_REGISTRY_INDEX_URL").ok());
632-
print!("sekrit");
633+
print!("sekrit");
633634
}"#)
634635
.build();
635636
cred_proj.cargo("build").run();

tests/testsuite/registry_auth.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ Caused by:
231231
Caused by:
232232
token rejected for `alternative`, please run `cargo login --registry alternative`
233233
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
234+
[NOTE] the token does not include an authentication scheme
234235
235236
Caused by:
236237
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/config.json`, got 401
@@ -272,6 +273,7 @@ Caused by:
272273
Caused by:
273274
token rejected for `alternative`, please run `cargo login --registry alternative`
274275
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
276+
[NOTE] the token does not include an authentication scheme
275277
276278
Caused by:
277279
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/config.json`, got 401
@@ -316,6 +318,7 @@ Caused by:
316318
Caused by:
317319
token rejected for `alternative`, please run `cargo login --registry alternative`
318320
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
321+
[NOTE] the token does not include an authentication scheme
319322
320323
Caused by:
321324
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/config.json`, got 401
@@ -410,6 +413,7 @@ Caused by:
410413
Caused by:
411414
token rejected for `alternative`, please run `cargo login --registry alternative`
412415
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
416+
[NOTE] the token does not include an authentication scheme
413417
414418
Caused by:
415419
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/config.json`, got 401
@@ -465,6 +469,12 @@ fn incorrect_token_bearer_scheme() {
465469
[UPDATING] `alternative` index
466470
[ERROR] failed to get `bar` as a dependency of package `foo v0.0.1 ([ROOT]/foo)`
467471
472+
Caused by:
473+
failed to load source for dependency `bar`
474+
475+
Caused by:
476+
unable to update registry `alternative`
477+
468478
Caused by:
469479
token rejected for `alternative`, please run `cargo login --registry alternative`
470480
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN

0 commit comments

Comments
 (0)