Skip to content

Commit b34428b

Browse files
fix(auth); add auth scheme hint to token rejected error for alt
registries
1 parent 36e1e4d commit b34428b

2 files changed

Lines changed: 20 additions & 0 deletions

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/registry_auth.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ fn bad_environment_token_with_asymmetric_subject() {
225225
Caused by:
226226
token rejected for `alternative`, please run `cargo login --registry alternative`
227227
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
228+
[NOTE] the token does not include an authentication scheme
228229
229230
Caused by:
230231
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/config.json`, got 401
@@ -260,6 +261,7 @@ fn bad_environment_token_with_asymmetric_incorrect_subject() {
260261
Caused by:
261262
token rejected for `alternative`, please run `cargo login --registry alternative`
262263
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
264+
[NOTE] the token does not include an authentication scheme
263265
264266
Caused by:
265267
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/config.json`, got 401
@@ -298,6 +300,7 @@ fn bad_environment_token_with_incorrect_asymmetric() {
298300
Caused by:
299301
token rejected for `alternative`, please run `cargo login --registry alternative`
300302
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
303+
[NOTE] the token does not include an authentication scheme
301304
302305
Caused by:
303306
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/config.json`, got 401
@@ -380,6 +383,7 @@ fn incorrect_token() {
380383
Caused by:
381384
token rejected for `alternative`, please run `cargo login --registry alternative`
382385
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
386+
[NOTE] the token does not include an authentication scheme
383387
384388
Caused by:
385389
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/config.json`, got 401

0 commit comments

Comments
 (0)