auth.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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(self, url: str, username: Optional[str]) -> Optional[AuthInfo]:
  40. ...
  41. @abstractmethod
  42. def save_auth_info(self, url: str, username: str, password: str) -> None:
  43. ...
  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("utf-8"),
  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. logger.warning(
  222. "Keyring is skipped due to an exception: %s",
  223. str(exc),
  224. )
  225. global KEYRING_DISABLED
  226. KEYRING_DISABLED = True
  227. get_keyring_provider.cache_clear()
  228. return None
  229. def _get_index_url(self, url: str) -> Optional[str]:
  230. """Return the original index URL matching the requested URL.
  231. Cached or dynamically generated credentials may work against
  232. the original index URL rather than just the netloc.
  233. The provided url should have had its username and password
  234. removed already. If the original index url had credentials then
  235. they will be included in the return value.
  236. Returns None if no matching index was found, or if --no-index
  237. was specified by the user.
  238. """
  239. if not url or not self.index_urls:
  240. return None
  241. url = remove_auth_from_url(url).rstrip("/") + "/"
  242. parsed_url = urllib.parse.urlsplit(url)
  243. candidates = []
  244. for index in self.index_urls:
  245. index = index.rstrip("/") + "/"
  246. parsed_index = urllib.parse.urlsplit(remove_auth_from_url(index))
  247. if parsed_url == parsed_index:
  248. return index
  249. if parsed_url.netloc != parsed_index.netloc:
  250. continue
  251. candidate = urllib.parse.urlsplit(index)
  252. candidates.append(candidate)
  253. if not candidates:
  254. return None
  255. candidates.sort(
  256. reverse=True,
  257. key=lambda candidate: commonprefix(
  258. [
  259. parsed_url.path,
  260. candidate.path,
  261. ]
  262. ).rfind("/"),
  263. )
  264. return urllib.parse.urlunsplit(candidates[0])
  265. def _get_new_credentials(
  266. self,
  267. original_url: str,
  268. *,
  269. allow_netrc: bool = True,
  270. allow_keyring: bool = False,
  271. ) -> AuthInfo:
  272. """Find and return credentials for the specified URL."""
  273. # Split the credentials and netloc from the url.
  274. url, netloc, url_user_password = split_auth_netloc_from_url(
  275. original_url,
  276. )
  277. # Start with the credentials embedded in the url
  278. username, password = url_user_password
  279. if username is not None and password is not None:
  280. logger.debug("Found credentials in url for %s", netloc)
  281. return url_user_password
  282. # Find a matching index url for this request
  283. index_url = self._get_index_url(url)
  284. if index_url:
  285. # Split the credentials from the url.
  286. index_info = split_auth_netloc_from_url(index_url)
  287. if index_info:
  288. index_url, _, index_url_user_password = index_info
  289. logger.debug("Found index url %s", index_url)
  290. # If an index URL was found, try its embedded credentials
  291. if index_url and index_url_user_password[0] is not None:
  292. username, password = index_url_user_password
  293. if username is not None and password is not None:
  294. logger.debug("Found credentials in index url for %s", netloc)
  295. return index_url_user_password
  296. # Get creds from netrc if we still don't have them
  297. if allow_netrc:
  298. netrc_auth = get_netrc_auth(original_url)
  299. if netrc_auth:
  300. logger.debug("Found credentials in netrc for %s", netloc)
  301. return netrc_auth
  302. # If we don't have a password and keyring is available, use it.
  303. if allow_keyring:
  304. # The index url is more specific than the netloc, so try it first
  305. # fmt: off
  306. kr_auth = (
  307. self._get_keyring_auth(index_url, username) or
  308. self._get_keyring_auth(netloc, username)
  309. )
  310. # fmt: on
  311. if kr_auth:
  312. logger.debug("Found credentials in keyring for %s", netloc)
  313. return kr_auth
  314. return username, password
  315. def _get_url_and_credentials(
  316. self, original_url: str
  317. ) -> Tuple[str, Optional[str], Optional[str]]:
  318. """Return the credentials to use for the provided URL.
  319. If allowed, netrc and keyring may be used to obtain the
  320. correct credentials.
  321. Returns (url_without_credentials, username, password). Note
  322. that even if the original URL contains credentials, this
  323. function may return a different username and password.
  324. """
  325. url, netloc, _ = split_auth_netloc_from_url(original_url)
  326. # Try to get credentials from original url
  327. username, password = self._get_new_credentials(original_url)
  328. # If credentials not found, use any stored credentials for this netloc.
  329. # Do this if either the username or the password is missing.
  330. # This accounts for the situation in which the user has specified
  331. # the username in the index url, but the password comes from keyring.
  332. if (username is None or password is None) and netloc in self.passwords:
  333. un, pw = self.passwords[netloc]
  334. # It is possible that the cached credentials are for a different username,
  335. # in which case the cache should be ignored.
  336. if username is None or username == un:
  337. username, password = un, pw
  338. if username is not None or password is not None:
  339. # Convert the username and password if they're None, so that
  340. # this netloc will show up as "cached" in the conditional above.
  341. # Further, HTTPBasicAuth doesn't accept None, so it makes sense to
  342. # cache the value that is going to be used.
  343. username = username or ""
  344. password = password or ""
  345. # Store any acquired credentials.
  346. self.passwords[netloc] = (username, password)
  347. assert (
  348. # Credentials were found
  349. (username is not None and password is not None)
  350. # Credentials were not found
  351. or (username is None and password is None)
  352. ), f"Could not load credentials from url: {original_url}"
  353. return url, username, password
  354. def __call__(self, req: Request) -> Request:
  355. # Get credentials for this request
  356. url, username, password = self._get_url_and_credentials(req.url)
  357. # Set the url of the request to the url without any credentials
  358. req.url = url
  359. if username is not None and password is not None:
  360. # Send the basic auth with this request
  361. req = HTTPBasicAuth(username, password)(req)
  362. # Attach a hook to handle 401 responses
  363. req.register_hook("response", self.handle_401)
  364. return req
  365. # Factored out to allow for easy patching in tests
  366. def _prompt_for_password(
  367. self, netloc: str
  368. ) -> Tuple[Optional[str], Optional[str], bool]:
  369. username = ask_input(f"User for {netloc}: ") if self.prompting else None
  370. if not username:
  371. return None, None, False
  372. if self.use_keyring:
  373. auth = self._get_keyring_auth(netloc, username)
  374. if auth and auth[0] is not None and auth[1] is not None:
  375. return auth[0], auth[1], False
  376. password = ask_password("Password: ")
  377. return username, password, True
  378. # Factored out to allow for easy patching in tests
  379. def _should_save_password_to_keyring(self) -> bool:
  380. if (
  381. not self.prompting
  382. or not self.use_keyring
  383. or not self.keyring_provider.has_keyring
  384. ):
  385. return False
  386. return ask("Save credentials to keyring [y/N]: ", ["y", "n"]) == "y"
  387. def handle_401(self, resp: Response, **kwargs: Any) -> Response:
  388. # We only care about 401 responses, anything else we want to just
  389. # pass through the actual response
  390. if resp.status_code != 401:
  391. return resp
  392. username, password = None, None
  393. # Query the keyring for credentials:
  394. if self.use_keyring:
  395. username, password = self._get_new_credentials(
  396. resp.url,
  397. allow_netrc=False,
  398. allow_keyring=True,
  399. )
  400. # We are not able to prompt the user so simply return the response
  401. if not self.prompting and not username and not password:
  402. return resp
  403. parsed = urllib.parse.urlparse(resp.url)
  404. # Prompt the user for a new username and password
  405. save = False
  406. if not username and not password:
  407. username, password, save = self._prompt_for_password(parsed.netloc)
  408. # Store the new username and password to use for future requests
  409. self._credentials_to_save = None
  410. if username is not None and password is not None:
  411. self.passwords[parsed.netloc] = (username, password)
  412. # Prompt to save the password to keyring
  413. if save and self._should_save_password_to_keyring():
  414. self._credentials_to_save = Credentials(
  415. url=parsed.netloc,
  416. username=username,
  417. password=password,
  418. )
  419. # Consume content and release the original connection to allow our new
  420. # request to reuse the same one.
  421. resp.content
  422. resp.raw.release_conn()
  423. # Add our new username and password to the request
  424. req = HTTPBasicAuth(username or "", password or "")(resp.request)
  425. req.register_hook("response", self.warn_on_401)
  426. # On successful request, save the credentials that were used to
  427. # keyring. (Note that if the user responded "no" above, this member
  428. # is not set and nothing will be saved.)
  429. if self._credentials_to_save:
  430. req.register_hook("response", self.save_credentials)
  431. # Send our new request
  432. new_resp = resp.connection.send(req, **kwargs)
  433. new_resp.history.append(resp)
  434. return new_resp
  435. def warn_on_401(self, resp: Response, **kwargs: Any) -> None:
  436. """Response callback to warn about incorrect credentials."""
  437. if resp.status_code == 401:
  438. logger.warning(
  439. "401 Error, Credentials not correct for %s",
  440. resp.request.url,
  441. )
  442. def save_credentials(self, resp: Response, **kwargs: Any) -> None:
  443. """Response callback to save credentials on success."""
  444. assert (
  445. self.keyring_provider.has_keyring
  446. ), "should never reach here without keyring"
  447. creds = self._credentials_to_save
  448. self._credentials_to_save = None
  449. if creds and resp.status_code < 400:
  450. try:
  451. logger.info("Saving credentials to keyring")
  452. self.keyring_provider.save_auth_info(
  453. creds.url, creds.username, creds.password
  454. )
  455. except Exception:
  456. logger.exception("Failed to save credentials")