_login.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. # Copyright 2020 The HuggingFace Team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Contains methods to log in to the Hub."""
  15. import os
  16. import subprocess
  17. from getpass import getpass
  18. from pathlib import Path
  19. from typing import Optional
  20. from . import constants
  21. from .commands._cli_utils import ANSI
  22. from .utils import (
  23. capture_output,
  24. get_token,
  25. is_google_colab,
  26. is_notebook,
  27. list_credential_helpers,
  28. logging,
  29. run_subprocess,
  30. set_git_credential,
  31. unset_git_credential,
  32. )
  33. from .utils._auth import (
  34. _get_token_by_name,
  35. _get_token_from_environment,
  36. _get_token_from_file,
  37. _get_token_from_google_colab,
  38. _save_stored_tokens,
  39. _save_token,
  40. get_stored_tokens,
  41. )
  42. from .utils._deprecation import _deprecate_arguments, _deprecate_positional_args
  43. logger = logging.get_logger(__name__)
  44. _HF_LOGO_ASCII = """
  45. _| _| _| _| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _|_|_|_| _|_| _|_|_| _|_|_|_|
  46. _| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|
  47. _|_|_|_| _| _| _| _|_| _| _|_| _| _| _| _| _| _|_| _|_|_| _|_|_|_| _| _|_|_|
  48. _| _| _| _| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|
  49. _| _| _|_| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _| _| _| _|_|_| _|_|_|_|
  50. """
  51. @_deprecate_arguments(
  52. version="1.0",
  53. deprecated_args="write_permission",
  54. custom_message="Fine-grained tokens added complexity to the permissions, making it irrelevant to check if a token has 'write' access.",
  55. )
  56. @_deprecate_positional_args(version="1.0")
  57. def login(
  58. token: Optional[str] = None,
  59. *,
  60. add_to_git_credential: bool = False,
  61. new_session: bool = True,
  62. write_permission: bool = False,
  63. ) -> None:
  64. """Login the machine to access the Hub.
  65. The `token` is persisted in cache and set as a git credential. Once done, the machine
  66. is logged in and the access token will be available across all `huggingface_hub`
  67. components. If `token` is not provided, it will be prompted to the user either with
  68. a widget (in a notebook) or via the terminal.
  69. To log in from outside of a script, one can also use `hf auth login` which is
  70. a cli command that wraps [`login`].
  71. > [!TIP]
  72. > [`login`] is a drop-in replacement method for [`notebook_login`] as it wraps and
  73. > extends its capabilities.
  74. > [!TIP]
  75. > When the token is not passed, [`login`] will automatically detect if the script runs
  76. > in a notebook or not. However, this detection might not be accurate due to the
  77. > variety of notebooks that exists nowadays. If that is the case, you can always force
  78. > the UI by using [`notebook_login`] or [`interpreter_login`].
  79. Args:
  80. token (`str`, *optional*):
  81. User access token to generate from https://huggingface.co/settings/token.
  82. add_to_git_credential (`bool`, defaults to `False`):
  83. If `True`, token will be set as git credential. If no git credential helper
  84. is configured, a warning will be displayed to the user. If `token` is `None`,
  85. the value of `add_to_git_credential` is ignored and will be prompted again
  86. to the end user.
  87. new_session (`bool`, defaults to `True`):
  88. If `True`, will request a token even if one is already saved on the machine.
  89. write_permission (`bool`):
  90. Ignored and deprecated argument.
  91. Raises:
  92. [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
  93. If an organization token is passed. Only personal account tokens are valid
  94. to log in.
  95. [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
  96. If token is invalid.
  97. [`ImportError`](https://docs.python.org/3/library/exceptions.html#ImportError)
  98. If running in a notebook but `ipywidgets` is not installed.
  99. """
  100. if token is not None:
  101. if not add_to_git_credential:
  102. logger.info(
  103. "The token has not been saved to the git credentials helper. Pass "
  104. "`add_to_git_credential=True` in this function directly or "
  105. "`--add-to-git-credential` if using via `hf`CLI if "
  106. "you want to set the git credential as well."
  107. )
  108. _login(token, add_to_git_credential=add_to_git_credential)
  109. elif is_notebook():
  110. notebook_login(new_session=new_session)
  111. else:
  112. interpreter_login(new_session=new_session)
  113. def logout(token_name: Optional[str] = None) -> None:
  114. """Logout the machine from the Hub.
  115. Token is deleted from the machine and removed from git credential.
  116. Args:
  117. token_name (`str`, *optional*):
  118. Name of the access token to logout from. If `None`, will logout from all saved access tokens.
  119. Raises:
  120. [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError):
  121. If the access token name is not found.
  122. """
  123. if get_token() is None and not get_stored_tokens(): # No active token and no saved access tokens
  124. logger.warning("Not logged in!")
  125. return
  126. if not token_name:
  127. # Delete all saved access tokens and token
  128. for file_path in (constants.HF_TOKEN_PATH, constants.HF_STORED_TOKENS_PATH):
  129. try:
  130. Path(file_path).unlink()
  131. except FileNotFoundError:
  132. pass
  133. logger.info("Successfully logged out from all access tokens.")
  134. else:
  135. _logout_from_token(token_name)
  136. logger.info(f"Successfully logged out from access token: {token_name}.")
  137. unset_git_credential()
  138. # Check if still logged in
  139. if _get_token_from_google_colab() is not None:
  140. raise EnvironmentError(
  141. "You are automatically logged in using a Google Colab secret.\n"
  142. "To log out, you must unset the `HF_TOKEN` secret in your Colab settings."
  143. )
  144. if _get_token_from_environment() is not None:
  145. raise EnvironmentError(
  146. "Token has been deleted from your machine but you are still logged in.\n"
  147. "To log out, you must clear out both `HF_TOKEN` and `HUGGING_FACE_HUB_TOKEN` environment variables."
  148. )
  149. def auth_switch(token_name: str, add_to_git_credential: bool = False) -> None:
  150. """Switch to a different access token.
  151. Args:
  152. token_name (`str`):
  153. Name of the access token to switch to.
  154. add_to_git_credential (`bool`, defaults to `False`):
  155. If `True`, token will be set as git credential. If no git credential helper
  156. is configured, a warning will be displayed to the user. If `token` is `None`,
  157. the value of `add_to_git_credential` is ignored and will be prompted again
  158. to the end user.
  159. Raises:
  160. [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError):
  161. If the access token name is not found.
  162. """
  163. token = _get_token_by_name(token_name)
  164. if not token:
  165. raise ValueError(f"Access token {token_name} not found in {constants.HF_STORED_TOKENS_PATH}")
  166. # Write token to HF_TOKEN_PATH
  167. _set_active_token(token_name, add_to_git_credential)
  168. logger.info(f"The current active token is: {token_name}")
  169. token_from_environment = _get_token_from_environment()
  170. if token_from_environment is not None and token_from_environment != token:
  171. logger.warning(
  172. "The environment variable `HF_TOKEN` is set and will override the access token you've just switched to."
  173. )
  174. def auth_list() -> None:
  175. """List all stored access tokens."""
  176. tokens = get_stored_tokens()
  177. if not tokens:
  178. logger.info("No access tokens found.")
  179. return
  180. # Find current token
  181. current_token = get_token()
  182. current_token_name = None
  183. for token_name in tokens:
  184. if tokens.get(token_name) == current_token:
  185. current_token_name = token_name
  186. # Print header
  187. max_offset = max(len("token"), max(len(token) for token in tokens)) + 2
  188. print(f" {{:<{max_offset}}}| {{:<15}}".format("name", "token"))
  189. print("-" * (max_offset + 2) + "|" + "-" * 15)
  190. # Print saved access tokens
  191. for token_name in tokens:
  192. token = tokens.get(token_name, "<not set>")
  193. masked_token = f"{token[:3]}****{token[-4:]}" if token != "<not set>" else token
  194. is_current = "*" if token == current_token else " "
  195. print(f"{is_current} {{:<{max_offset}}}| {{:<15}}".format(token_name, masked_token))
  196. if _get_token_from_environment():
  197. logger.warning(
  198. "\nNote: Environment variable `HF_TOKEN` is set and is the current active token independently from the stored tokens listed above."
  199. )
  200. elif current_token_name is None:
  201. logger.warning(
  202. "\nNote: No active token is set and no environment variable `HF_TOKEN` is found. Use `hf auth login` to log in."
  203. )
  204. ###
  205. # Interpreter-based login (text)
  206. ###
  207. @_deprecate_arguments(
  208. version="1.0",
  209. deprecated_args="write_permission",
  210. custom_message="Fine-grained tokens added complexity to the permissions, making it irrelevant to check if a token has 'write' access.",
  211. )
  212. @_deprecate_positional_args(version="1.0")
  213. def interpreter_login(*, new_session: bool = True, write_permission: bool = False) -> None:
  214. """
  215. Displays a prompt to log in to the HF website and store the token.
  216. This is equivalent to [`login`] without passing a token when not run in a notebook.
  217. [`interpreter_login`] is useful if you want to force the use of the terminal prompt
  218. instead of a notebook widget.
  219. For more details, see [`login`].
  220. Args:
  221. new_session (`bool`, defaults to `True`):
  222. If `True`, will request a token even if one is already saved on the machine.
  223. write_permission (`bool`):
  224. Ignored and deprecated argument.
  225. """
  226. if not new_session and get_token() is not None:
  227. logger.info("User is already logged in.")
  228. return
  229. from .commands.delete_cache import _ask_for_confirmation_no_tui
  230. print(_HF_LOGO_ASCII)
  231. if get_token() is not None:
  232. logger.info(
  233. " A token is already saved on your machine. Run `hf auth whoami`"
  234. " to get more information or `hf auth logout` if you want"
  235. " to log out."
  236. )
  237. logger.info(" Setting a new token will erase the existing one.")
  238. logger.info(
  239. " To log in, `huggingface_hub` requires a token generated from https://huggingface.co/settings/tokens ."
  240. )
  241. if os.name == "nt":
  242. logger.info("Token can be pasted using 'Right-Click'.")
  243. token = getpass("Enter your token (input will not be visible): ")
  244. add_to_git_credential = _ask_for_confirmation_no_tui("Add token as git credential?")
  245. _login(token=token, add_to_git_credential=add_to_git_credential)
  246. ###
  247. # Notebook-based login (widget)
  248. ###
  249. NOTEBOOK_LOGIN_PASSWORD_HTML = """<center> <img
  250. src=https://huggingface.co/front/assets/huggingface_logo-noborder.svg
  251. alt='Hugging Face'> <br> Immediately click login after typing your password or
  252. it might be stored in plain text in this notebook file. </center>"""
  253. NOTEBOOK_LOGIN_TOKEN_HTML_START = """<center> <img
  254. src=https://huggingface.co/front/assets/huggingface_logo-noborder.svg
  255. alt='Hugging Face'> <br> Copy a token from <a
  256. href="https://huggingface.co/settings/tokens" target="_blank">your Hugging Face
  257. tokens page</a> and paste it below. <br> Immediately click login after copying
  258. your token or it might be stored in plain text in this notebook file. </center>"""
  259. NOTEBOOK_LOGIN_TOKEN_HTML_END = """
  260. <b>Pro Tip:</b> If you don't already have one, you can create a dedicated
  261. 'notebooks' token with 'write' access, that you can then easily reuse for all
  262. notebooks. </center>"""
  263. @_deprecate_arguments(
  264. version="1.0",
  265. deprecated_args="write_permission",
  266. custom_message="Fine-grained tokens added complexity to the permissions, making it irrelevant to check if a token has 'write' access.",
  267. )
  268. @_deprecate_positional_args(version="1.0")
  269. def notebook_login(*, new_session: bool = True, write_permission: bool = False) -> None:
  270. """
  271. Displays a widget to log in to the HF website and store the token.
  272. This is equivalent to [`login`] without passing a token when run in a notebook.
  273. [`notebook_login`] is useful if you want to force the use of the notebook widget
  274. instead of a prompt in the terminal.
  275. For more details, see [`login`].
  276. Args:
  277. new_session (`bool`, defaults to `True`):
  278. If `True`, will request a token even if one is already saved on the machine.
  279. write_permission (`bool`):
  280. Ignored and deprecated argument.
  281. """
  282. try:
  283. import ipywidgets.widgets as widgets # type: ignore
  284. from IPython.display import display # type: ignore
  285. except ImportError:
  286. raise ImportError(
  287. "The `notebook_login` function can only be used in a notebook (Jupyter or"
  288. " Colab) and you need the `ipywidgets` module: `pip install ipywidgets`."
  289. )
  290. if not new_session and get_token() is not None:
  291. logger.info("User is already logged in.")
  292. return
  293. box_layout = widgets.Layout(display="flex", flex_flow="column", align_items="center", width="50%")
  294. token_widget = widgets.Password(description="Token:")
  295. git_checkbox_widget = widgets.Checkbox(value=True, description="Add token as git credential?")
  296. token_finish_button = widgets.Button(description="Login")
  297. login_token_widget = widgets.VBox(
  298. [
  299. widgets.HTML(NOTEBOOK_LOGIN_TOKEN_HTML_START),
  300. token_widget,
  301. git_checkbox_widget,
  302. token_finish_button,
  303. widgets.HTML(NOTEBOOK_LOGIN_TOKEN_HTML_END),
  304. ],
  305. layout=box_layout,
  306. )
  307. display(login_token_widget)
  308. # On click events
  309. def login_token_event(t):
  310. """Event handler for the login button."""
  311. token = token_widget.value
  312. add_to_git_credential = git_checkbox_widget.value
  313. # Erase token and clear value to make sure it's not saved in the notebook.
  314. token_widget.value = ""
  315. # Hide inputs
  316. login_token_widget.children = [widgets.Label("Connecting...")]
  317. try:
  318. with capture_output() as captured:
  319. _login(token, add_to_git_credential=add_to_git_credential)
  320. message = captured.getvalue()
  321. except Exception as error:
  322. message = str(error)
  323. # Print result (success message or error)
  324. login_token_widget.children = [widgets.Label(line) for line in message.split("\n") if line.strip()]
  325. token_finish_button.on_click(login_token_event)
  326. ###
  327. # Login private helpers
  328. ###
  329. def _login(
  330. token: str,
  331. add_to_git_credential: bool,
  332. ) -> None:
  333. from .hf_api import whoami # avoid circular import
  334. if token.startswith("api_org"):
  335. raise ValueError("You must use your personal account token, not an organization token.")
  336. token_info = whoami(token)
  337. permission = token_info["auth"]["accessToken"]["role"]
  338. logger.info(f"Token is valid (permission: {permission}).")
  339. token_name = token_info["auth"]["accessToken"]["displayName"]
  340. # Store token locally
  341. _save_token(token=token, token_name=token_name)
  342. # Set active token
  343. _set_active_token(token_name=token_name, add_to_git_credential=add_to_git_credential)
  344. logger.info("Login successful.")
  345. if _get_token_from_environment():
  346. logger.warning(
  347. "Note: Environment variable`HF_TOKEN` is set and is the current active token independently from the token you've just configured."
  348. )
  349. else:
  350. logger.info(f"The current active token is: `{token_name}`")
  351. def _logout_from_token(token_name: str) -> None:
  352. """Logout from a specific access token.
  353. Args:
  354. token_name (`str`):
  355. The name of the access token to logout from.
  356. Raises:
  357. [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError):
  358. If the access token name is not found.
  359. """
  360. stored_tokens = get_stored_tokens()
  361. # If there is no access tokens saved or the access token name is not found, do nothing
  362. if not stored_tokens or token_name not in stored_tokens:
  363. return
  364. token = stored_tokens.pop(token_name)
  365. _save_stored_tokens(stored_tokens)
  366. if token == _get_token_from_file():
  367. logger.warning(f"Active token '{token_name}' has been deleted.")
  368. Path(constants.HF_TOKEN_PATH).unlink(missing_ok=True)
  369. def _set_active_token(
  370. token_name: str,
  371. add_to_git_credential: bool,
  372. ) -> None:
  373. """Set the active access token.
  374. Args:
  375. token_name (`str`):
  376. The name of the token to set as active.
  377. """
  378. token = _get_token_by_name(token_name)
  379. if not token:
  380. raise ValueError(f"Token {token_name} not found in {constants.HF_STORED_TOKENS_PATH}")
  381. if add_to_git_credential:
  382. if _is_git_credential_helper_configured():
  383. set_git_credential(token)
  384. logger.info(
  385. "Your token has been saved in your configured git credential helpers"
  386. + f" ({','.join(list_credential_helpers())})."
  387. )
  388. else:
  389. logger.warning("Token has not been saved to git credential helper.")
  390. # Write token to HF_TOKEN_PATH
  391. path = Path(constants.HF_TOKEN_PATH)
  392. path.parent.mkdir(parents=True, exist_ok=True)
  393. path.write_text(token)
  394. logger.info(f"Your token has been saved to {constants.HF_TOKEN_PATH}")
  395. def _is_git_credential_helper_configured() -> bool:
  396. """Check if a git credential helper is configured.
  397. Warns user if not the case (except for Google Colab where "store" is set by default
  398. by `huggingface_hub`).
  399. """
  400. helpers = list_credential_helpers()
  401. if len(helpers) > 0:
  402. return True # Do not warn: at least 1 helper is set
  403. # Only in Google Colab to avoid the warning message
  404. # See https://github.com/huggingface/huggingface_hub/issues/1043#issuecomment-1247010710
  405. if is_google_colab():
  406. _set_store_as_git_credential_helper_globally()
  407. return True # Do not warn: "store" is used by default in Google Colab
  408. # Otherwise, warn user
  409. print(
  410. ANSI.red(
  411. "Cannot authenticate through git-credential as no helper is defined on your"
  412. " machine.\nYou might have to re-authenticate when pushing to the Hugging"
  413. " Face Hub.\nRun the following command in your terminal in case you want to"
  414. " set the 'store' credential helper as default.\n\ngit config --global"
  415. " credential.helper store\n\nRead"
  416. " https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage for more"
  417. " details."
  418. )
  419. )
  420. return False
  421. def _set_store_as_git_credential_helper_globally() -> None:
  422. """Set globally the credential.helper to `store`.
  423. To be used only in Google Colab as we assume the user doesn't care about the git
  424. credential config. It is the only particular case where we don't want to display the
  425. warning message in [`notebook_login()`].
  426. Related:
  427. - https://github.com/huggingface/huggingface_hub/issues/1043
  428. - https://github.com/huggingface/huggingface_hub/issues/1051
  429. - https://git-scm.com/docs/git-credential-store
  430. """
  431. try:
  432. run_subprocess("git config --global credential.helper store")
  433. except subprocess.CalledProcessError as exc:
  434. raise EnvironmentError(exc.stderr)