@@ -387,6 +387,23 @@ impl fmt::Display for AuthorizationErrorReason {
387
387
}
388
388
}
389
389
390
+ #[ derive( Clone , Debug , PartialEq ) ]
391
+ pub enum AuthorizationScheme {
392
+ Basic ,
393
+ Bearer ,
394
+ Unrecognized ,
395
+ }
396
+
397
+ impl fmt:: Display for AuthorizationScheme {
398
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
399
+ match self {
400
+ AuthorizationScheme :: Basic => write ! ( f, "Basic" ) ,
401
+ AuthorizationScheme :: Bearer => write ! ( f, "Bearer" ) ,
402
+ AuthorizationScheme :: Unrecognized => write ! ( f, "unrecognized" ) ,
403
+ }
404
+ }
405
+ }
406
+
390
407
/// An authorization error from accessing a registry.
391
408
#[ derive( Debug ) ]
392
409
pub struct AuthorizationError {
@@ -400,6 +417,8 @@ pub struct AuthorizationError {
400
417
reason : AuthorizationErrorReason ,
401
418
/// Should `cargo login` and the `_TOKEN` env var be included when displaying this error?
402
419
supports_cargo_token_credential_provider : bool ,
420
+ /// What authorization scheme was specified in the token, if any?
421
+ scheme : Option < AuthorizationScheme > ,
403
422
}
404
423
405
424
impl AuthorizationError {
@@ -408,6 +427,7 @@ impl AuthorizationError {
408
427
sid : SourceId ,
409
428
login_url : Option < Url > ,
410
429
reason : AuthorizationErrorReason ,
430
+ scheme : Option < AuthorizationScheme > ,
411
431
) -> CargoResult < Self > {
412
432
// Only display the _TOKEN environment variable suggestion if the `cargo:token` credential
413
433
// provider is available for the source. Otherwise setting the environment variable will
@@ -422,6 +442,7 @@ impl AuthorizationError {
422
442
login_url,
423
443
reason,
424
444
supports_cargo_token_credential_provider,
445
+ scheme,
425
446
} )
426
447
}
427
448
}
@@ -461,6 +482,19 @@ impl fmt::Display for AuthorizationError {
461
482
"\n You may need to log in using this registry's credential provider"
462
483
) ?;
463
484
}
485
+ if let Some ( scheme) = & self . scheme {
486
+ write ! (
487
+ f,
488
+ "Your registry token is prefixed with an embedded {} authentication scheme. Is this correct?" ,
489
+ scheme,
490
+ ) ?;
491
+ } else {
492
+ write ! (
493
+ f,
494
+ "Your registry token is not prefixed with an embedded authorization scheme (e.g. `Bearer `).\n \
495
+ Does this registry require an authentication scheme?",
496
+ ) ?;
497
+ }
464
498
Ok ( ( ) )
465
499
} else if self . reason == AuthorizationErrorReason :: TokenMissing {
466
500
write ! (
@@ -624,6 +658,7 @@ pub fn auth_token(
624
658
* sid,
625
659
login_url. cloned ( ) ,
626
660
AuthorizationErrorReason :: TokenMissing ,
661
+ None ,
627
662
) ?
628
663
. into ( ) ) ,
629
664
}
0 commit comments