Skip to content

Commit 47af6dc

Browse files
committed
fix(login): Deprecate CLI token
This came up in #13623 to avoid putting tokens into shell history.
1 parent f15df8f commit 47af6dc

File tree

9 files changed

+80
-49
lines changed

9 files changed

+80
-49
lines changed

src/bin/cargo/commands/login.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ use crate::command_prelude::*;
66
pub fn cli() -> Command {
77
subcommand("login")
88
.about("Log in to a registry.")
9-
.arg(Arg::new("token").value_name("TOKEN").action(ArgAction::Set))
9+
.arg(
10+
Arg::new("token")
11+
.value_name("TOKEN")
12+
.action(ArgAction::Set)
13+
.hide(true),
14+
)
1015
.arg_registry("Registry to use")
1116
.arg(
1217
Arg::new("args")
@@ -27,16 +32,18 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
2732
"must not be index URL"
2833
);
2934

35+
let token = args.get_one::<String>("token").map(|s| s.as_str().into());
36+
if token.is_some() {
37+
let _ = gctx
38+
.shell()
39+
.warn("`cargo login <token>` is deprecated in favor of reading `<token>` from stdin");
40+
}
41+
3042
let extra_args = args
3143
.get_many::<String>("args")
3244
.unwrap_or_default()
3345
.map(String::as_str)
3446
.collect::<Vec<_>>();
35-
ops::registry_login(
36-
gctx,
37-
args.get_one::<String>("token").map(|s| s.as_str().into()),
38-
reg.as_ref(),
39-
&extra_args,
40-
)?;
47+
ops::registry_login(gctx, token, reg.as_ref(), &extra_args)?;
4148
Ok(())
4249
}

src/doc/man/cargo-login.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cargo-login --- Log in to a registry
66

77
## SYNOPSIS
88

9-
`cargo login` [_options_] [_token_] [`--` _args_]
9+
`cargo login` [_options_] [`--` _args_]
1010

1111
## DESCRIPTION
1212

@@ -24,7 +24,7 @@ If a registry has a credential-provider specified, it will be used. Otherwise,
2424
the providers from the config value `registry.global-credential-providers` will
2525
be attempted, starting from the end of the list.
2626

27-
If the _token_ argument is not specified, it will be read from stdin.
27+
The _token_ will be read from stdin.
2828

2929
The API token for crates.io may be retrieved from <https://crates.io/me>.
3030

src/doc/man/generated_txt/cargo-login.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ NAME
44
cargo-login — Log in to a registry
55

66
SYNOPSIS
7-
cargo login [options] [token] [-- args]
7+
cargo login [options] [-- args]
88

99
DESCRIPTION
1010
This command will run a credential provider to save a token so that
@@ -23,7 +23,7 @@ DESCRIPTION
2323
registry.global-credential-providers will be attempted, starting from
2424
the end of the list.
2525

26-
If the token argument is not specified, it will be read from stdin.
26+
The token will be read from stdin.
2727

2828
The API token for crates.io may be retrieved from
2929
<https://crates.io/me>.

src/doc/src/commands/cargo-login.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cargo-login --- Log in to a registry
66

77
## SYNOPSIS
88

9-
`cargo login` [_options_] [_token_] [`--` _args_]
9+
`cargo login` [_options_] [`--` _args_]
1010

1111
## DESCRIPTION
1212

@@ -24,7 +24,7 @@ If a registry has a credential-provider specified, it will be used. Otherwise,
2424
the providers from the config value `registry.global-credential-providers` will
2525
be attempted, starting from the end of the list.
2626

27-
If the _token_ argument is not specified, it will be read from stdin.
27+
The _token_ will be read from stdin.
2828

2929
The API token for crates.io may be retrieved from <https://crates.io/me>.
3030

src/etc/man/cargo-login.1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
.SH "NAME"
77
cargo\-login \[em] Log in to a registry
88
.SH "SYNOPSIS"
9-
\fBcargo login\fR [\fIoptions\fR] [\fItoken\fR] [\fB\-\-\fR \fIargs\fR]
9+
\fBcargo login\fR [\fIoptions\fR] [\fB\-\-\fR \fIargs\fR]
1010
.SH "DESCRIPTION"
1111
This command will run a credential provider to save a token so that commands
1212
that require authentication, such as \fBcargo\-publish\fR(1), will be
@@ -22,7 +22,7 @@ If a registry has a credential\-provider specified, it will be used. Otherwise,
2222
the providers from the config value \fBregistry.global\-credential\-providers\fR will
2323
be attempted, starting from the end of the list.
2424
.sp
25-
If the \fItoken\fR argument is not specified, it will be read from stdin.
25+
The \fItoken\fR will be read from stdin.
2626
.sp
2727
The API token for crates.io may be retrieved from <https://crates.io/me>\&.
2828
.sp

tests/testsuite/alt_registry.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,8 @@ fn no_api() {
814814
"#]])
815815
.run();
816816

817-
p.cargo("login --registry alternative TOKEN")
817+
p.cargo("login --registry alternative")
818+
.with_stdin("TOKEN")
818819
.with_status(101)
819820
.with_stderr_data(str![[r#"
820821
[ERROR] registry `alternative` does not support API commands

tests/testsuite/cargo_login/help/stdout.term.svg

Lines changed: 21 additions & 23 deletions
Loading

tests/testsuite/credential_process.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ fn basic_unsupported() {
9595
.credential_provider(&["cargo:token-from-stdout", "false"])
9696
.build();
9797

98-
cargo_process("login abcdefg")
98+
cargo_process("login")
99+
.with_stdin("abcdefg")
99100
.replace_crates_io(registry.index_url())
100101
.with_status(101)
101102
.with_stderr_data(str![[r#"
@@ -132,7 +133,8 @@ fn login() {
132133
])
133134
.build();
134135

135-
cargo_process("login abcdefg -- cmd3 --cmd4")
136+
cargo_process("login -- cmd3 --cmd4")
137+
.with_stdin("abcdefg")
136138
.replace_crates_io(registry.index_url())
137139
.with_stderr_data(str![[r#"
138140
[UPDATING] crates.io index
@@ -383,7 +385,8 @@ fn multiple_providers() {
383385
)
384386
.unwrap();
385387

386-
cargo_process("login -v abcdefg")
388+
cargo_process("login -v")
389+
.with_stdin("abcdefg")
387390
.replace_crates_io(server.index_url())
388391
.with_stderr_data(str![[r#"
389392
[UPDATING] crates.io index
@@ -429,7 +432,8 @@ fn registry_provider_overrides_global() {
429432
)
430433
.unwrap();
431434

432-
cargo_process("login -v abcdefg")
435+
cargo_process("login -v")
436+
.with_stdin("abcdefg")
433437
.env("CARGO_REGISTRY_CREDENTIAL_PROVIDER", "cargo:token")
434438
.replace_crates_io(server.index_url())
435439
.with_stderr_data(str![[r#"
@@ -460,7 +464,7 @@ fn both_asymmetric_and_token() {
460464
)
461465
.unwrap();
462466

463-
cargo_process("login -Zasymmetric-token -v abcdefg")
467+
cargo_process("login -Zasymmetric-token -v").with_stdin("abcdefg")
464468
.masquerade_as_nightly_cargo(&["asymmetric-token"])
465469
.replace_crates_io(server.index_url())
466470
.with_stderr_data(str![[r#"
@@ -675,7 +679,8 @@ fn unsupported_version() {
675679
.credential_provider(&[&provider])
676680
.build();
677681

678-
cargo_process("login abcdefg")
682+
cargo_process("login")
683+
.with_stdin("abcdefg")
679684
.replace_crates_io(registry.index_url())
680685
.with_status(101)
681686
.with_stderr_data(str![[r#"
@@ -707,7 +712,8 @@ fn alias_builtin_warning() {
707712
)
708713
.unwrap();
709714

710-
cargo_process("login abcdefg")
715+
cargo_process("login")
716+
.with_stdin("abcdefg")
711717
.replace_crates_io(registry.index_url())
712718
.with_stderr_data(str![[r#"
713719
[UPDATING] crates.io index

tests/testsuite/login.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ fn registry_credentials() {
8181

8282
let reg = "alternative";
8383

84-
cargo_process("login --registry").arg(reg).arg(TOKEN).run();
84+
cargo_process("login --registry")
85+
.arg(reg)
86+
.with_stdin(TOKEN)
87+
.run();
8588

8689
// Ensure that we have not updated the default token
8790
check_token(Some(ORIGINAL_TOKEN), None);
@@ -92,7 +95,7 @@ fn registry_credentials() {
9295
let reg2 = "alternative2";
9396
cargo_process("login --registry")
9497
.arg(reg2)
95-
.arg(TOKEN2)
98+
.with_stdin(TOKEN2)
9699
.run();
97100

98101
// Ensure not overwriting 1st alternate registry token with
@@ -118,6 +121,20 @@ fn empty_login_token() {
118121
please paste the token found on [ROOTURL]/api/me below
119122
[ERROR] credential provider `cargo:token` failed action `login`
120123
124+
Caused by:
125+
please provide a non-empty token
126+
127+
"#]])
128+
.with_status(101)
129+
.run();
130+
131+
cargo_process("login")
132+
.replace_crates_io(registry.index_url())
133+
.with_stdin("")
134+
.with_stderr_data(str![[r#"
135+
please paste the token found on [ROOTURL]/api/me below
136+
[ERROR] credential provider `cargo:token` failed action `login`
137+
121138
Caused by:
122139
please provide a non-empty token
123140
@@ -128,7 +145,9 @@ Caused by:
128145
cargo_process("login")
129146
.replace_crates_io(registry.index_url())
130147
.arg("")
148+
.with_stdin("")
131149
.with_stderr_data(str![[r#"
150+
[WARNING] `cargo login <token>` is deprecated in favor of reading `<token>` from stdin
132151
[ERROR] credential provider `cargo:token` failed action `login`
133152
134153
Caused by:
@@ -356,7 +375,7 @@ fn default_registry_configured() {
356375
.unwrap();
357376

358377
cargo_process("login")
359-
.arg("a-new-token")
378+
.with_stdin("a-new-token")
360379
.with_stderr_data(str![[r#"
361380
[UPDATING] `alternative` index
362381
[LOGIN] token for `alternative` saved

0 commit comments

Comments
 (0)