@@ -30,7 +30,7 @@ use crate::ops;
30
30
use crate :: ops:: Packages ;
31
31
use crate :: sources:: { RegistrySource , SourceConfigMap , CRATES_IO_DOMAIN , CRATES_IO_REGISTRY } ;
32
32
use crate :: util:: auth:: {
33
- paserk_public_from_paserk_secret, { self , AuthorizationError } ,
33
+ paserk_public_from_paserk_secret, Secret , { self , AuthorizationError } ,
34
34
} ;
35
35
use crate :: util:: config:: { Config , SslVersionConfig , SslVersionConfigRange } ;
36
36
use crate :: util:: errors:: CargoResult ;
@@ -45,11 +45,11 @@ use crate::{drop_print, drop_println, version};
45
45
pub enum RegistryCredentialConfig {
46
46
None ,
47
47
/// The authentication token.
48
- Token ( String ) ,
48
+ Token ( Secret < String > ) ,
49
49
/// Process used for fetching a token.
50
50
Process ( ( PathBuf , Vec < String > ) ) ,
51
51
/// Secret Key and subject for Asymmetric tokens.
52
- AsymmetricKey ( ( String , Option < String > ) ) ,
52
+ AsymmetricKey ( ( Secret < String > , Option < String > ) ) ,
53
53
}
54
54
55
55
impl RegistryCredentialConfig {
@@ -71,9 +71,9 @@ impl RegistryCredentialConfig {
71
71
pub fn is_asymmetric_key ( & self ) -> bool {
72
72
matches ! ( self , Self :: AsymmetricKey ( ..) )
73
73
}
74
- pub fn as_token ( & self ) -> Option < & str > {
74
+ pub fn as_token ( & self ) -> Option < Secret < & str > > {
75
75
if let Self :: Token ( v) = self {
76
- Some ( & * v )
76
+ Some ( v . as_deref ( ) )
77
77
} else {
78
78
None
79
79
}
@@ -85,7 +85,7 @@ impl RegistryCredentialConfig {
85
85
None
86
86
}
87
87
}
88
- pub fn as_asymmetric_key ( & self ) -> Option < & ( String , Option < String > ) > {
88
+ pub fn as_asymmetric_key ( & self ) -> Option < & ( Secret < String > , Option < String > ) > {
89
89
if let Self :: AsymmetricKey ( v) = self {
90
90
Some ( v)
91
91
} else {
@@ -96,7 +96,7 @@ impl RegistryCredentialConfig {
96
96
97
97
pub struct PublishOpts < ' cfg > {
98
98
pub config : & ' cfg Config ,
99
- pub token : Option < String > ,
99
+ pub token : Option < Secret < String > > ,
100
100
pub index : Option < String > ,
101
101
pub verify : bool ,
102
102
pub allow_dirty : bool ,
@@ -174,7 +174,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
174
174
175
175
let ( mut registry, reg_ids) = registry (
176
176
opts. config ,
177
- opts. token . as_deref ( ) ,
177
+ opts. token . as_ref ( ) . map ( Secret :: as_deref ) ,
178
178
opts. index . as_deref ( ) ,
179
179
publish_registry. as_deref ( ) ,
180
180
true ,
@@ -512,7 +512,7 @@ fn wait_for_publish(
512
512
/// * `token_required`: If `true`, the token will be set.
513
513
fn registry (
514
514
config : & Config ,
515
- token_from_cmdline : Option < & str > ,
515
+ token_from_cmdline : Option < Secret < & str > > ,
516
516
index : Option < & str > ,
517
517
registry : Option < & str > ,
518
518
force_update : bool ,
@@ -786,7 +786,7 @@ fn http_proxy_exists(config: &Config) -> CargoResult<bool> {
786
786
787
787
pub fn registry_login (
788
788
config : & Config ,
789
- token : Option < & str > ,
789
+ token : Option < Secret < & str > > ,
790
790
reg : Option < & str > ,
791
791
generate_keypair : bool ,
792
792
secret_key_required : bool ,
@@ -795,7 +795,7 @@ pub fn registry_login(
795
795
let source_ids = get_source_id ( config, None , reg) ?;
796
796
let reg_cfg = auth:: registry_credential_config ( config, & source_ids. original ) ?;
797
797
798
- let login_url = match registry ( config, token, None , reg, false , None ) {
798
+ let login_url = match registry ( config, token. clone ( ) , None , reg, false , None ) {
799
799
Ok ( ( registry, _) ) => Some ( format ! ( "{}/me" , registry. host( ) ) ) ,
800
800
Err ( e) if e. is :: < AuthorizationError > ( ) => e
801
801
. downcast :: < AuthorizationError > ( )
@@ -830,29 +830,33 @@ pub fn registry_login(
830
830
}
831
831
_ => ( None , None ) ,
832
832
} ;
833
- let secret_key: String ;
833
+ let secret_key: Secret < String > ;
834
834
if generate_keypair {
835
835
assert ! ( !secret_key_required) ;
836
836
let kp = AsymmetricKeyPair :: < pasetors:: version3:: V3 > :: generate ( ) . unwrap ( ) ;
837
- let mut key = String :: new ( ) ;
838
- FormatAsPaserk :: fmt ( & kp. secret , & mut key) . unwrap ( ) ;
839
- secret_key = key;
837
+ secret_key = Secret :: default ( ) . map ( |mut key| {
838
+ FormatAsPaserk :: fmt ( & kp. secret , & mut key) . unwrap ( ) ;
839
+ key
840
+ } ) ;
840
841
} else if secret_key_required {
841
842
assert ! ( !generate_keypair) ;
842
843
drop_println ! ( config, "please paste the API secret key below" ) ;
843
- let mut line = String :: new ( ) ;
844
- let input = io:: stdin ( ) ;
845
- input
846
- . lock ( )
847
- . read_line ( & mut line)
848
- . with_context ( || "failed to read stdin" ) ?;
849
- secret_key = line. trim ( ) . to_string ( ) ;
844
+ secret_key = Secret :: default ( )
845
+ . map ( |mut line| {
846
+ let input = io:: stdin ( ) ;
847
+ input
848
+ . lock ( )
849
+ . read_line ( & mut line)
850
+ . with_context ( || "failed to read stdin" )
851
+ . map ( |_| line. trim ( ) . to_string ( ) )
852
+ } )
853
+ . transpose ( ) ?;
850
854
} else {
851
855
secret_key = old_secret_key
852
856
. cloned ( )
853
857
. ok_or_else ( || anyhow ! ( "need a secret_key to set a key_subject" ) ) ?;
854
858
}
855
- if let Some ( p) = paserk_public_from_paserk_secret ( & secret_key) {
859
+ if let Some ( p) = paserk_public_from_paserk_secret ( secret_key. as_deref ( ) ) {
856
860
drop_println ! ( config, "{}" , & p) ;
857
861
} else {
858
862
bail ! ( "not a validly formatted PASERK secret key" ) ;
@@ -866,7 +870,7 @@ pub fn registry_login(
866
870
) ) ;
867
871
} else {
868
872
new_token = RegistryCredentialConfig :: Token ( match token {
869
- Some ( token) => token. to_string ( ) ,
873
+ Some ( token) => token. owned ( ) ,
870
874
None => {
871
875
if let Some ( login_url) = login_url {
872
876
drop_println ! (
@@ -890,7 +894,7 @@ pub fn registry_login(
890
894
. with_context ( || "failed to read stdin" ) ?;
891
895
// Automatically remove `cargo login` from an inputted token to
892
896
// allow direct pastes from `registry.host()`/me.
893
- line. replace ( "cargo login" , "" ) . trim ( ) . to_string ( )
897
+ Secret :: from ( line. replace ( "cargo login" , "" ) . trim ( ) . to_string ( ) )
894
898
}
895
899
} ) ;
896
900
@@ -938,7 +942,7 @@ pub fn registry_logout(config: &Config, reg: Option<&str>) -> CargoResult<()> {
938
942
939
943
pub struct OwnersOptions {
940
944
pub krate : Option < String > ,
941
- pub token : Option < String > ,
945
+ pub token : Option < Secret < String > > ,
942
946
pub index : Option < String > ,
943
947
pub to_add : Option < Vec < String > > ,
944
948
pub to_remove : Option < Vec < String > > ,
@@ -960,7 +964,7 @@ pub fn modify_owners(config: &Config, opts: &OwnersOptions) -> CargoResult<()> {
960
964
961
965
let ( mut registry, _) = registry (
962
966
config,
963
- opts. token . as_deref ( ) ,
967
+ opts. token . as_ref ( ) . map ( Secret :: as_deref ) ,
964
968
opts. index . as_deref ( ) ,
965
969
opts. registry . as_deref ( ) ,
966
970
true ,
@@ -1019,7 +1023,7 @@ pub fn yank(
1019
1023
config : & Config ,
1020
1024
krate : Option < String > ,
1021
1025
version : Option < String > ,
1022
- token : Option < String > ,
1026
+ token : Option < Secret < String > > ,
1023
1027
index : Option < String > ,
1024
1028
undo : bool ,
1025
1029
reg : Option < String > ,
@@ -1051,7 +1055,7 @@ pub fn yank(
1051
1055
1052
1056
let ( mut registry, _) = registry (
1053
1057
config,
1054
- token. as_deref ( ) ,
1058
+ token. as_ref ( ) . map ( Secret :: as_deref ) ,
1055
1059
index. as_deref ( ) ,
1056
1060
reg. as_deref ( ) ,
1057
1061
true ,
0 commit comments