auth.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. """Network Authentication Helpers
  2. Contains interface (MultiDomainBasicAuth) and associated glue code for
  3. providing credentials in the context of network requests.
  4. """
  5. import logging
  6. import os
  7. import shutil
  8. import subprocess
  9. import sysconfig
  10. import typing
  11. import urllib.parse
  12. from abc import ABC, abstractmethod
  13. from functools import lru_cache
  14. from os.path import commonprefix
  15. from pathlib import Path
  16. from typing import Any, Dict, List, NamedTuple, Optional, Tuple
  17. from pip._vendor.requests.auth import AuthBase, HTTPBasicAuth
  18. from pip._vendor.requests.models import Request, Response
  19. from pip._vendor.requests.utils import get_netrc_auth
  20. from pip._internal.utils.logging import getLogger
  21. from pip._internal.utils.misc import (
  22. ask,
  23. ask_input,
  24. ask_password,
  25. remove_auth_from_url,
  26. split_auth_netloc_from_url,
  27. )
  28. from pip._internal.vcs.versioncontrol import AuthInfo
  29. logger = getLogger(__name__)
  30. KEYRING_DISABLED = False
  31. class Credentials(NamedTuple):
  32. url: str
  33. username: str
  34. password: str
  35. class KeyRingBaseProvider(ABC):
  36. """Keyring base provider interface"""
  37. has_keyring: bool
  38. @abstractmethod
  39. def get_auth_info(
  40. self, url: str, username: Optional[str]
  41. ) -> Optional[AuthInfo]: ...
  42. @abstractmethod
  43. def save_auth_info(self, url: str, username: str, password: str) -> None: ...
  44. class KeyRingNullProvider(KeyRingBaseProvider):
  45. """Keyring null provider"""
  46. has_keyring = False
  47. def get_auth_info(self, url: str, username: Optional[str]) -> Optional[AuthInfo]:
  48. return None
  49. def save_auth_info(self, url: str, username: str, password: str) -> None:
  50. return None
  51. class KeyRingPythonProvider(KeyRingBaseProvider):
  52. """Keyring interface which uses locally imported `keyring`"""
  53. has_keyring = True
  54. def __init__(self) -> None:
  55. import keyring
  56. self.keyring = keyring
  57. def get_auth_info(self, url: str, username: Optional[str]) -> Optional[AuthInfo]:
  58. # Support keyring's get_credential interface which supports getting
  59. # credentials without a username. This is only available for
  60. # keyring>=15.2.0.
  61. if hasattr(self.keyring, "get_credential"):
  62. logger.debug("Getting credentials from keyring for %s", url)
  63. cred = self.keyring.get_credential(url, username)
  64. if cred is not None:
  65. return cred.username, cred.password
  66. return None
  67. if username is not None:
  68. logger.debug("Getting password from keyring for %s", url)
  69. password = self.keyring.get_password(url, username)
  70. if password:
  71. return username, password
  72. return None
  73. def save_auth_info(self, url: str, username: str, password: str) -> None:
  74. self.keyring.set_password(url, username, password)
  75. class KeyRingCliProvider(KeyRingBaseProvider):
  76. """Provider which uses `keyring` cli
  77. Instead of calling the keyring package installed alongside pip
  78. we call keyring on the command line which will enable pip to
  79. use which ever installation of keyring is available first in
  80. PATH.
  81. """
  82. has_keyring = True
  83. def __init__(self, cmd: str) -> None:
  84. self.keyring = cmd
  85. def get_auth_info(self, url: str, username: Optional[str]) -> Optional[AuthInfo]:
  86. # This is the default implementation of keyring.get_credential
  87. # https://github.com/jaraco/keyring/blob/97689324abcf01bd1793d49063e7ca01e03d7d07/keyring/backend.py#L134-L139
  88. if username is not None:
  89. password = self._get_password(url, username)
  90. if password is not None:
  91. return username, password
  92. return None
  93. def save_auth_info(self, url: str, username: str, password: str) -> None:
  94. return self._set_password(url, username, password)
  95. def _get_password(self, service_name: str, username: str) -> Optional[str]:
  96. """Mirror the implementation of keyring.get_password using cli"""
  97. if self.keyring is None:
  98. return None
  99. cmd = [self.keyring, "get", service_name, username]
  100. env = os.environ.copy()
  101. env["PYTHONIOENCODING"] = "utf-8"
  102. res = subprocess.run(
  103. cmd,
  104. stdin=subprocess.DEVNULL,
  105. stdout=subprocess.PIPE,
  106. env=env,
  107. )
  108. if res.returncode:
  109. return None
  110. return res.stdout.decode("utf-8").strip(os.linesep)
  111. def _set_password(self, service_name: str, username: str, password: str) -> None:
  112. """Mirror the implementation of keyring.set_password using cli"""
  113. if self.keyring is None:
  114. return None
  115. env = os.environ.copy()
  116. env["PYTHONIOENCODING"] = "utf-8"
  117. subprocess.run(
  118. [self.keyring, "set", service_name, username],
  119. input=f"{password}{os.linesep}".encode(),
  120. env=env,
  121. check=True,
  122. )
  123. return None
  124. @lru_cache(maxsize=None)
  125. def get_keyring_provider(provider: str) -> KeyRingBaseProvider:
  126. logger.verbose("Keyring provider requested: %s", provider)
  127. # keyring has previously failed and been disabled
  128. if KEYRING_DISABLED:
  129. provider = "disabled"
  130. if provider in ["import", "auto"]:
  131. try:
  132. impl = KeyRingPythonProvider()
  133. logger.verbose("Keyring provider set: import")
  134. return impl
  135. except ImportError:
  136. pass
  137. except Exception as exc:
  138. # In the event of an unexpected exception
  139. # we should warn the user
  140. msg = "Installed copy of keyring fails with exception %s"
  141. if provider == "auto":
  142. msg = msg + ", trying to find a keyring executable as a fallback"
  143. logger.warning(msg, exc, exc_info=logger.isEnabledFor(logging.DEBUG))
  144. if provider in ["subprocess", "auto"]:
  145. cli = shutil.which("keyring")
  146. if cli and cli.startswith(sysconfig.get_path("scripts")):
  147. # all code within this function is stolen from shutil.which implementation
  148. @typing.no_type_check
  149. def PATH_as_shutil_which_determines_it() -> str:
  150. path = os.environ.get("PATH", None)
  151. if path is None:
  152. try:
  153. path = os.confstr("CS_PATH")
  154. except (AttributeError, ValueError):
  155. # os.confstr() or CS_PATH is not available
  156. path = os.defpath
  157. # bpo-35755: Don't use os.defpath if the PATH environment variable is
  158. # set to an empty string
  159. return path
  160. scripts = Path(sysconfig.get_path("scripts"))
  161. paths = []
  162. for path in PATH_as_shutil_which_determines_it().split(os.pathsep):
  163. p = Path(path)
  164. try:
  165. if not p.samefile(scripts):
  166. paths.append(path)
  167. except FileNotFoundError:
  168. pass
  169. path = os.pathsep.join(paths)
  170. cli = shutil.which("keyring", path=path)
  171. if cli:
  172. logger.verbose("Keyring provider set: subprocess with executable %s", cli)
  173. return KeyRingCliProvider(cli)
  174. logger.verbose("Keyring provider set: disabled")
  175. return KeyRingNullProvider()
  176. class MultiDomainBasicAuth(AuthBase):
  177. def __init__(
  178. self,
  179. prompting: bool = True,
  180. index_urls: Optional[List[str]] = None,
  181. keyring_provider: str = "auto",
  182. ) -> None:
  183. self.prompting = prompting
  184. self.index_urls = index_urls
  185. self.keyring_provider = keyring_provider # type: ignore[assignment]
  186. self.passwords: Dict[str, AuthInfo] = {}
  187. # When the user is prompted to enter credentials and keyring is
  188. # available, we will offer to save them. If the user accepts,
  189. # this value is set to the credentials they entered. After the
  190. # request authenticates, the caller should call
  191. # ``save_credentials`` to save these.
  192. self._credentials_to_save: Optional[Credentials] = None
  193. @property
  194. def keyring_provider(self) -> KeyRingBaseProvider:
  195. return get_keyring_provider(self._keyring_provider)
  196. @keyring_provider.setter
  197. def keyring_provider(self, provider: str) -> None:
  198. # The free function get_keyring_provider has been decorated with
  199. # functools.cache. If an exception occurs in get_keyring_auth that
  200. # cache will be cleared and keyring disabled, take that into account
  201. # if you want to remove this indirection.
  202. self._keyring_provider = provider
  203. @property
  204. def use_keyring(self) -> bool:
  205. # We won't use keyring when --no-input is passed unless
  206. # a specific provider is requested because it might require
  207. # user interaction
  208. return self.prompting or self._keyring_provider not in ["auto", "disabled"]
  209. def _get_keyring_auth(
  210. self,
  211. url: Optional[str],
  212. username: Optional[str],
  213. ) -> Optional[AuthInfo]:
  214. """Return the tuple auth for a given url from keyring."""
  215. # Do nothing if no url was provided
  216. if not url:
  217. return None
  218. try:
  219. return self.keyring_provider.get_auth_info(url, username)
  220. except Exception as exc:
  221. # Log the full exception (with stacktrace) at debug, so it'll only
  222. # show up when running in verbose mode.
  223. logger.debug("Keyring is skipped due to an exception", exc_info=True)
  224. # Always log a shortened version of the exception.
  225. logger.warning(
  226. "Keyring is skipped due to an exception: %s",
  227. str(exc),
  228. )
  229. global KEYRING_DISABLED
  230. KEYRING_DISABLED = True
  231. get_keyring_provider.cache_clear()
  232. return None
  233. def _get_index_url(self, url: str) -> Optional[str]:
  234. """Return the original index URL matching the requested URL.
  235. Cached or dynamically generated credentials may work against
  236. the original index URL rather than just the netloc.
  237. The provided url should have had its username and password
  238. removed already. If the original index url had credentials then
  239. they will be included in the return value.
  240. Returns None if no matching index was found, or if --no-index
  241. was specified by the user.
  242. """
  243. if not url or not self.index_urls:
  244. return None
  245. url = remove_auth_from_url(url).rstrip("/") + "/"
  246. parsed_url = urllib.parse.urlsplit(url)
  247. candidates = []
  248. for index in self.index_urls:
  249. index = index.rstrip("/") + "/"
  250. parsed_index = urllib.parse.urlsplit(remove_auth_from_url(index))
  251. if parsed_url == parsed_index:
  252. return index
  253. if parsed_url.netloc != parsed_index.netloc:
  254. continue
  255. candidate = urllib.parse.urlsplit(index)
  256. candidates.append(candidate)
  257. if not candidates:
  258. return None
  259. candidates.sort(
  260. reverse=True,
  261. key=lambda candidate: commonprefix(
  262. [
  263. parsed_url.path,
  264. candidate.path,
  265. ]
  266. ).rfind("/"),
  267. )
  268. return urllib.parse.urlunsplit(candidates[0])
  269. def _get_new_credentials(
  270. self,
  271. original_url: str,
  272. *,
  273. allow_netrc: bool = True,
  274. allow_keyring: bool = False,
  275. ) -> AuthInfo:
  276. """Find and return credentials for the specified URL."""
  277. # Split the credentials and netloc from the url.
  278. url, netloc, url_user_password = split_auth_netloc_from_url(
  279. original_url,
  280. )
  281. # Start with the credentials embedded in the url
  282. username, password = url_user_password
  283. if username is not None and password is not None:
  284. logger.debug("Found credentials in url for %s", netloc)
  285. return url_user_password
  286. # Find a matching index url for this request
  287. index_url = self._get_index_url(url)
  288. if index_url:
  289. # Split the credentials from the url.
  290. index_info = split_auth_netloc_from_url(index_url)
  291. if index_info:
  292. index_url, _, index_url_user_password = index_info
  293. logger.debug("Found index url %s", index_url)
  294. # If an index URL was found, try its embedded credentials
  295. if index_url and index_url_user_password[0] is not None:
  296. username, password = index_url_user_password
  297. if username is not None and password is not None:
  298. logger.debug("Found credentials in index url for %s", netloc)
  299. return index_url_user_password
  300. # Get creds from netrc if we still don't have them
  301. if allow_netrc:
  302. netrc_auth = get_netrc_auth(original_url)
  303. if netrc_auth:
  304. logger.debug("Found credentials in netrc for %s", netloc)
  305. return netrc_auth
  306. # If we don't have a password and keyring is available, use it.
  307. if allow_keyring:
  308. # The index url is more specific than the netloc, so try it first
  309. # fmt: off
  310. kr_auth = (
  311. self._get_keyring_auth(index_url, username) or
  312. self._get_keyring_auth(netloc, username)
  313. )
  314. # fmt: on
  315. if kr_auth:
  316. logger.debug("Found credentials in keyring for %s", netloc)
  317. return kr_auth
  318. return username, password
  319. def _get_url_and_credentials(
  320. self, original_url: str
  321. ) -> Tuple[str, Optional[str], Optional[str]]:
  322. """Return the credentials to use for the provided URL.
  323. If allowed, netrc and keyring may be used to obtain the
  324. correct credentials.
  325. Returns (url_without_credentials, username, password). Note
  326. that even if the original URL contains credentials, this
  327. function may return a different username and password.
  328. """
  329. url, netloc, _ = split_auth_netloc_from_url(original_url)
  330. # Try to get credentials from original url
  331. username, password = self._get_new_credentials(original_url)
  332. # If credentials not found, use any stored credentials for this netloc.
  333. # Do this if either the username or the password is missing.
  334. # This accounts for the situation in which the user has specified
  335. # the username in the index url, but the password comes from keyring.
  336. if (username is None or password is None) and netloc in self.passwords:
  337. un, pw = self.passwords[netloc]
  338. # It is possible that the cached credentials are for a different username,
  339. # in which case the cache should be ignored.
  340. if username is None or username == un:
  341. username, password = un, pw
  342. if username is not None or password is not None:
  343. # Convert the username and password if they're None, so that
  344. # this netloc will show up as "cached" in the conditional above.
  345. # Further, HTTPBasicAuth doesn't accept None, so it makes sense to
  346. # cache the value that is going to be used.
  347. username = username or ""
  348. password = password or ""
  349. # Store any acquired credentials.
  350. self.passwords[netloc] = (username, password)
  351. assert (
  352. # Credentials were found
  353. (username is not None and password is not None)
  354. # Credentials were not found
  355. or (username is None and password is None)
  356. ), f"Could not load credentials from url: {original_url}"
  357. return url, username, password
  358. def __call__(self, req: Request) -> Request:
  359. # Get credentials for this request
  360. url, username, password = self._get_url_and_credentials(req.url)
  361. # Set the url of the request to the url without any credentials
  362. req.url = url
  363. if username is not None and password is not None:
  364. # Send the basic auth with this request
  365. req = HTTPBasicAuth(username, password)(req)
  366. # Attach a hook to handle 401 responses
  367. req.register_hook("response", self.handle_401)
  368. return req
  369. # Factored out to allow for easy patching in tests
  370. def _prompt_for_password(
  371. self, netloc: str
  372. ) -> Tuple[Optional[str], Optional[str], bool]:
  373. username = ask_input(f"User for {netloc}: ") if self.prompting else None
  374. if not username:
  375. return None, None, False
  376. if self.use_keyring:
  377. auth = self._get_keyring_auth(netloc, username)
  378. if auth and auth[0] is not None and auth[1] is not None:
  379. return auth[0], auth[1], False
  380. password = ask_password("Password: ")
  381. return username, password, True
  382. # Factored out to allow for easy patching in tests
  383. def _should_save_password_to_keyring(self) -> bool:
  384. if (
  385. not self.prompting
  386. or not self.use_keyring
  387. or not self.keyring_provider.has_keyring
  388. ):
  389. return False
  390. return ask("Save credentials to keyring [y/N]: ", ["y", "n"]) == "y"
  391. def handle_401(self, resp: Response, **kwargs: Any) -> Response:
  392. # We only care about 401 responses, anything else we want to just
  393. # pass through the actual response
  394. if resp.status_code != 401:
  395. return resp
  396. username, password = None, None
  397. # Query the keyring for credentials:
  398. if self.use_keyring:
  399. username, password = self._get_new_credentials(
  400. resp.url,
  401. allow_netrc=False,
  402. allow_keyring=True,
  403. )
  404. # We are not able to prompt the user so simply return the response
  405. if not self.prompting and not username and not password:
  406. return resp
  407. parsed = urllib.parse.urlparse(resp.url)
  408. # Prompt the user for a new username and password
  409. save = False
  410. if not username and not password:
  411. username, password, save = self._prompt_for_password(parsed.netloc)
  412. # Store the new username and password to use for future requests
  413. self._credentials_to_save = None
  414. if username is not None and password is not None:
  415. self.passwords[parsed.netloc] = (username, password)
  416. # Prompt to save the password to keyring
  417. if save and self._should_save_password_to_keyring():
  418. self._credentials_to_save = Credentials(
  419. url=parsed.netloc,
  420. username=username,
  421. password=password,
  422. )
  423. # Consume content and release the original connection to allow our new
  424. # request to reuse the same one.
  425. # The result of the assignment isn't used, it's just needed to consume
  426. # the content.
  427. _ = resp.content
  428. resp.raw.release_conn()
  429. # Add our new username and password to the request
  430. req = HTTPBasicAuth(username or "", password or "")(resp.request)
  431. req.register_hook("response", self.warn_on_401)
  432. # On successful request, save the credentials that were used to
  433. # keyring. (Note that if the user responded "no" above, this member
  434. # is not set and nothing will be saved.)
  435. if self._credentials_to_save:
  436. req.register_hook("response", self.save_credentials)
  437. # Send our new request
  438. new_resp = resp.connection.send(req, **kwargs)
  439. new_resp.history.append(resp)
  440. return new_resp
  441. def warn_on_401(self, resp: Response, **kwargs: Any) -> None:
  442. """Response callback to warn about incorrect credentials."""
  443. if resp.status_code == 401:
  444. logger.warning(
  445. "401 Error, Credentials not correct for %s",
  446. resp.request.url,
  447. )
  448. def save_credentials(self, resp: Response, **kwargs: Any) -> None:
  449. """Response callback to save credentials on success."""
  450. assert (
  451. self.keyring_provider.has_keyring
  452. ), "should never reach here without keyring"
  453. creds = self._credentials_to_save
  454. self._credentials_to_save = None
  455. if creds and resp.status_code < 400:
  456. try:
  457. logger.info("Saving credentials to keyring")
  458. self.keyring_provider.save_auth_info(
  459. creds.url, creds.username, creds.password
  460. )
  461. except Exception:
  462. logger.exception("Failed to save credentials")