Skip to content

Commit 6fcc7b3

Browse files
Support multiple tokens locally (#2549)
* Add multi token support * Set git credentials when setting the active profile * Add git credential flag as argument for auth switch method * Fix logout helper function * Refactor auth utils + fixes * Remove unnecessary file creation * Add tests and some fixes * Make get profiles helper function public * Add multiselect menu for auth switch command * Move auth fixture * Refactor logout helper function * Update package reference and quick start documentation * Update cli documentation * Expose new auth methods in huggingface_hub module * Multiple small fixes * post-review fixes * Fix table of contents * Use server provided token names instead of profiles * Add stored tokens path in the environment info * Multiple fixes post-review
1 parent 833bdc5 commit 6fcc7b3

File tree

15 files changed

+559
-73
lines changed

15 files changed

+559
-73
lines changed

docs/source/en/_redirects.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ how-to-upstream: guides/upload
99
search-the-hub: guides/search
1010
guides/manage_spaces: guides/manage-spaces
1111
package_reference/inference_api: package_reference/inference_client
12-
12+
package_reference/login: package_reference/authentication
1313
# Alias for hf-transfer description
1414
hf_transfer: package_reference/environment_variables#hfhubenablehftransfer
1515

1616
# Rename webhooks_server to webhooks
17-
guides/webhooks_server: guides/webhooks
17+
guides/webhooks_server: guides/webhooks

docs/source/en/_toctree.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
- title: "Get started"
1+
- title: 'Get started'
22
sections:
33
- local: index
44
title: Home
55
- local: quick-start
66
title: Quickstart
77
- local: installation
88
title: Installation
9-
- title: "How-to guides"
9+
- title: 'How-to guides'
1010
sections:
1111
- local: guides/overview
1212
title: Overview
@@ -40,16 +40,16 @@
4040
title: Integrate a library
4141
- local: guides/webhooks
4242
title: Webhooks
43-
- title: "Conceptual guides"
43+
- title: 'Conceptual guides'
4444
sections:
4545
- local: concepts/git_vs_http
4646
title: Git vs HTTP paradigm
47-
- title: "Reference"
47+
- title: 'Reference'
4848
sections:
4949
- local: package_reference/overview
5050
title: Overview
51-
- local: package_reference/login
52-
title: Login and logout
51+
- local: package_reference/authentication
52+
title: Authentication
5353
- local: package_reference/environment_variables
5454
title: Environment variables
5555
- local: package_reference/repository

docs/source/en/guides/cli.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Once you have your token, run the following command in your terminal:
9292
>>> huggingface-cli login
9393
```
9494

95-
This command will prompt you for a token. Copy-paste yours and press *Enter*. Then you'll be asked if the token should also be saved as a git credential. Press *Enter* again (default to yes) if you plan to use `git` locally. Finally, it will call the Hub to check that your token is valid and save it locally.
95+
This command will prompt you for a token. Copy-paste yours and press *Enter*. Then, you'll be asked if the token should also be saved as a git credential. Press *Enter* again (default to yes) if you plan to use `git` locally. Finally, it will call the Hub to check that your token is valid and save it locally.
9696

9797
```
9898
_| _| _| _| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _|_|_|_| _|_| _|_|_| _|_|_|_|
@@ -102,7 +102,7 @@ _| _| _| _| _| _| _| _| _| _| _|_| _| _| _|
102102
_| _| _|_| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _| _| _| _|_|_| _|_|_|_|
103103
104104
To log in, `huggingface_hub` requires a token generated from https://huggingface.co/settings/tokens .
105-
Token:
105+
Enter your token (input will not be visible):
106106
Add token as git credential? (Y/n)
107107
Token is valid (permission: write).
108108
Your token has been saved in your configured git credential helpers (store).
@@ -114,11 +114,13 @@ Alternatively, if you want to log-in without being prompted, you can pass the to
114114

115115
```bash
116116
# Or using an environment variable
117-
>>> huggingface-cli login --token $HUGGINGFACE_TOKEN --add-to-git-credential
117+
>>> huggingface-cli login --token $HF_TOKEN --add-to-git-credential
118118
Token is valid (permission: write).
119+
The token `token_name` has been saved to /home/wauplin/.cache/huggingface/stored_tokens
119120
Your token has been saved in your configured git credential helpers (store).
120121
Your token has been saved to /home/wauplin/.cache/huggingface/token
121122
Login successful
123+
The current active token is: `token_name`
122124
```
123125

124126
For more details about authentication, check out [this section](../quick-start#authentication).
@@ -137,7 +139,7 @@ If you are not logged in, an error message will be printed.
137139

138140
## huggingface-cli logout
139141

140-
This commands logs you out. In practice, it will delete the token saved on your machine.
142+
This commands logs you out. In practice, it will delete all tokens stored on your machine. If you want to remove a specific token, you can specify the token name as an argument.
141143

142144
This command will not log you out if you are logged in using the `HF_TOKEN` environment variable (see [reference](../package_reference/environment_variables#hftoken)). If that is the case, you must unset the environment variable in your machine configuration.
143145

@@ -431,25 +433,25 @@ https://huggingface.co/Wauplin/my-cool-model/tree/main
431433

432434
## huggingface-cli repo-files
433435

434-
If you want to delete files from a Hugging Face repository, use the `huggingface-cli repo-files` command.
436+
If you want to delete files from a Hugging Face repository, use the `huggingface-cli repo-files` command.
435437

436438
### Delete files
437439

438440
The `huggingface-cli repo-files <repo_id> delete` sub-command allows you to delete files from a repository. Here are some usage examples.
439441

440442
Delete a folder :
441443
```bash
442-
>>> huggingface-cli repo-files Wauplin/my-cool-model delete folder/
444+
>>> huggingface-cli repo-files Wauplin/my-cool-model delete folder/
443445
Files correctly deleted from repo. Commit: https://huggingface.co/Wauplin/my-cool-mo...
444446
```
445447

446-
Delete multiple files:
448+
Delete multiple files:
447449
```bash
448450
>>> huggingface-cli repo-files Wauplin/my-cool-model delete file.txt folder/pytorch_model.bin
449451
Files correctly deleted from repo. Commit: https://huggingface.co/Wauplin/my-cool-mo...
450452
```
451453

452-
Use Unix-style wildcards to delete sets of files:
454+
Use Unix-style wildcards to delete sets of files:
453455
```bash
454456
>>> huggingface-cli repo-files Wauplin/my-cool-model delete "*.txt" "folder/*.bin"
455457
Files correctly deleted from repo. Commit: https://huggingface.co/Wauplin/my-cool-mo...
@@ -460,7 +462,7 @@ Files correctly deleted from repo. Commit: https://huggingface.co/Wauplin/my-coo
460462
To delete files from a repo you must be authenticated and authorized. By default, the token saved locally (using `huggingface-cli login`) will be used. If you want to authenticate explicitly, use the `--token` option:
461463

462464
```bash
463-
>>> huggingface-cli repo-files --token=hf_**** Wauplin/my-cool-model delete file.txt
465+
>>> huggingface-cli repo-files --token=hf_**** Wauplin/my-cool-model delete file.txt
464466
```
465467

466468
## huggingface-cli scan-cache

docs/source/en/package_reference/login.md renamed to docs/source/en/package_reference/authentication.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
rendered properly in your Markdown viewer.
33
-->
44

5-
# Login and logout
5+
# Authentication
66

7-
The `huggingface_hub` library allows users to programmatically login and logout the machine to the Hub.
7+
The `huggingface_hub` library allows users to programmatically manage authentication to the Hub. This includes logging in, logging out, switching between tokens, and listing available tokens.
88

99
For more details about authentication, check out [this section](../quick-start#authentication).
1010

@@ -23,3 +23,11 @@ For more details about authentication, check out [this section](../quick-start#a
2323
## logout
2424

2525
[[autodoc]] logout
26+
27+
## auth_switch
28+
29+
[[autodoc]] auth_switch
30+
31+
## auth_list
32+
33+
[[autodoc]] auth_list

docs/source/en/quick-start.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ Once logged in, all requests to the Hub - even methods that don't necessarily re
9393

9494
</Tip>
9595

96+
### Manage multiple tokens locally
97+
98+
You can save multiple tokens on your machine by simply logging in with the [`login`] command with each token. If you need to switch between these tokens locally, you can use the [`auth switch`] command:
99+
100+
```bash
101+
huggingface-cli auth switch
102+
```
103+
104+
This command will prompt you to select a token by its name from a list of saved tokens. Once selected, the chosen token becomes the _active_ token, and it will be used for all interactions with the Hub.
105+
106+
107+
You can list all available access tokens on your machine with `huggingface-cli auth list`.
108+
96109
### Environment variable
97110

98111
The environment variable `HF_TOKEN` can also be used to authenticate yourself. This is especially useful in a Space where you can set `HF_TOKEN` as a [Space secret](https://huggingface.co/docs/hub/spaces-overview#managing-secrets).

src/huggingface_hub/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
"InferenceEndpointType",
6464
],
6565
"_login": [
66+
"auth_list",
67+
"auth_switch",
6668
"interpreter_login",
6769
"login",
6870
"logout",
@@ -582,6 +584,8 @@ def __dir__():
582584
InferenceEndpointType, # noqa: F401
583585
)
584586
from ._login import (
587+
auth_list, # noqa: F401
588+
auth_switch, # noqa: F401
585589
interpreter_login, # noqa: F401
586590
login, # noqa: F401
587591
logout, # noqa: F401

0 commit comments

Comments
 (0)