Skip to content

Commit f1c010c

Browse files
author
Tony Sansone
committed
Cleaned up dart pub token
1 parent 926f56c commit f1c010c

File tree

1 file changed

+51
-36
lines changed

1 file changed

+51
-36
lines changed

src/content/tools/pub/cmd/pub-token.md

+51-36
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@ title: dart pub token
33
description: Manage authentication tokens for package repositories.
44
---
55

6-
_pub_ _token_ is one of the subcommands of the [pub command](/tools/pub/cmd).
6+
The `dart pub token` subcommand manages a store of secret tokens to
7+
authenticate against third-party servers when [publishing](pub-lish) packages
8+
and [retrieving](pub-get) dependencies.
79

8-
It is used to manage a store of secret tokens for authenticating
9-
against third-party servers when [publishing](pub-lish) packages and
10-
[retrieving](pub-get) dependencies.
11-
12-
The tokens are stored in a
13-
[user-wide config dir]({{site.repo.dart.org}}/cli_util/blob/71ba36e2554f7b7717f3f12b5ddd33751a4e3ddd/lib/cli_util.dart#L88-L118).
10+
It stores these tokens in a [user-wide config directory][config-dir].
1411

1512
It has three subcommands: `add`, `list` and `remove`.
1613

17-
If you try to `dart pub get` and have a [dependency](/tools/pub/dependencies) hosted
18-
on a private repository you _may_ be asked to provide credentials:
14+
Consider a scenario when you have a [dependency](/tools/pub/dependencies)
15+
hosted on a private repository.
16+
When you invoke `dart pub get`, the command _might_ return a prompt
17+
to provide credentials:
1918

2019
```console
2120
$ dart pub get
@@ -26,13 +25,16 @@ https://some-package-repo.com/my-org/my-repo package repository requested authen
2625
Go to https://some-package-repo.com and log in to obtain your token.
2726
```
2827

29-
The last line is a message the server can provide to help you obtaining a token.
30-
Some servers might not provide such a message.
28+
In this last message, the final line gives you the server that can help you
29+
obtain a token. Not all servers provide such this message.
30+
31+
## Add a new credential
32+
33+
To create a new credential, invoke `dart pub token add`.
3134

32-
## Adding credentials `dart pub token add`
35+
### Add a credential for the current session
3336

34-
To enter the credentials use `dart pub token add`,
35-
and type the credential on stdin.
37+
At the prompt, type the credential on the command line (`stdin`).
3638

3739
```console
3840
$ dart pub token add https://some-package-repo.com/my-org/my-repo
@@ -42,33 +44,45 @@ Enter secret token: <Type token on stdin>
4244
```
4345

4446
:::note
45-
The token is input on stdin rather than as a command line option to avoid it
46-
ending up in the shell history such as `~/.bash_history`.
47+
The token takes input on `stdin` rather than as a command line option
48+
to keep the token out of the shell history.
4749
:::
4850

49-
In a scripting situation you can store the secret in an environment variable and
50-
use `dart pub token add <hosted-url> --env-var <ENV_VAR_NAME>`.
51+
### Add a credential for all sessions
5152

52-
```console
53-
$ dart pub token add https://other-package-repo.com/ --env-var TOKEN_VAR
54-
Requests to "https://other-package-repo.com/" will now be authenticated using the secret token stored in the environment variable "TOKEN_VAR".
55-
```
53+
In a script, you can store the secret in an environment variable.
54+
55+
If you choose to store your secret in an environment variable,
56+
find a way to hide the secret from your shell history.
57+
To explore one way of doing this, consult [this post on Medium][zsh-post].
58+
If you add a local environment variable, you need to restart any open
59+
consoles to enable that new variable.
60+
61+
Most CI environments can inject secrets into an environment variable.
62+
To learn how, consult documentation for [GitHub Actions][]] or
63+
[GitLab][] as examples.
5664

57-
This will cause `dart pub get` to read whatever is stored in `$TOKEN_VAR` and
58-
use that as the authentication token.
65+
[GitHub Actions]: https://docs.github.com/actions/security-guides/encrypted-secrets#using-encrypted-secrets-in-a-workflow
66+
[GitLab]: https://docs.gitlab.com/ee/ci/secrets/
67+
[zsh-post]: https://medium.com/@prasincs/hiding-secret-keys-from-shell-history-part-1-5875eb5556cc
5968

60-
You can set the environment variable in Bash with `export TOKEN_VAR=...` but
61-
that still doesn't prevent the command being logged.
69+
To use an environment variable as a token, invoke:
6270

63-
Most CI environments has a way to inject secrets into an environment
64-
variable:
71+
```console
72+
dart pub token add <hosted-url> --env-var <ENV_VAR_NAME>
73+
```
6574

66-
* [GitHub Actions](https://docs.github.com/actions/security-guides/encrypted-secrets#using-encrypted-secrets-in-a-workflow).
67-
* [GitLab](https://docs.gitlab.com/ee/ci/secrets/).
75+
This causes `dart pub get` to read the data stored in `$TOKEN_VAR` then
76+
use it as the authentication token.
6877

69-
## Listing credentials `dart pub token list`
78+
```console
79+
$ dart pub token add https://other-package-repo.com/ --env-var TOKEN_VAR
80+
Requests to "https://other-package-repo.com/" will now be authenticated using the secret token stored in the environment variable "TOKEN_VAR".
81+
```
82+
83+
## Return a list of credentials
7084

71-
To see a list of all active credentials use `dart pub token list`:
85+
To see a list of all active credentials, invoke `dart pub token list`:
7286

7387
```console
7488
$ dart pub token list
@@ -77,22 +91,23 @@ https://some-package-repo.com/my-org/my-repo
7791
https://other-package-repo.com/
7892
```
7993

80-
## Removing credentials `dart pub token remove`
94+
## Remove one or more credentials
8195

82-
You can remove a single token with `dart pub token remove`:
96+
To remove a single token, invoke `dart pub token remove`:
8397

8498
```console
8599
$ dart pub token remove https://other-package-repo.com
86100
Removed secret token for package repository: https://other-package-repo.com
87101
```
88102

89-
Or remove all with `remove --all`:
103+
To remove all tokens, invoke the command with the `remove --all` option:
90104

91105
```console
92106
$ dart pub token remove --all
93107
pub-tokens.json is deleted.
94108
Removed 1 secret tokens.
95109
```
96110

97-
98111
{% include 'pub-problems.md' %}
112+
113+
[config-dir]: {{site.repo.dart.org}}/cli_util/blob/71ba36e2554f7b7717f3f12b5ddd33751a4e3ddd/lib/cli_util.dart#L88-L118

0 commit comments

Comments
 (0)