auth.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 urllib.parse
  7. from typing import Any, Dict, List, Optional, Tuple
  8. from pip._vendor.requests.auth import AuthBase, HTTPBasicAuth
  9. from pip._vendor.requests.models import Request, Response
  10. from pip._vendor.requests.utils import get_netrc_auth
  11. from pip._internal.utils.misc import (
  12. ask,
  13. ask_input,
  14. ask_password,
  15. remove_auth_from_url,
  16. split_auth_netloc_from_url,
  17. )
  18. from pip._internal.vcs.versioncontrol import AuthInfo
  19. logger = logging.getLogger(__name__)
  20. Credentials = Tuple[str, str, str]
  21. try:
  22. import keyring
  23. except ImportError:
  24. keyring = None
  25. except Exception as exc:
  26. logger.warning(
  27. "Keyring is skipped due to an exception: %s", str(exc),
  28. )
  29. keyring = None
  30. def get_keyring_auth(url, username):
  31. # type: (Optional[str], Optional[str]) -> Optional[AuthInfo]
  32. """Return the tuple auth for a given url from keyring."""
  33. global keyring
  34. if not url or not keyring:
  35. return None
  36. try:
  37. try:
  38. get_credential = keyring.get_credential
  39. except AttributeError:
  40. pass
  41. else:
  42. logger.debug("Getting credentials from keyring for %s", url)
  43. cred = get_credential(url, username)
  44. if cred is not None:
  45. return cred.username, cred.password
  46. return None
  47. if username:
  48. logger.debug("Getting password from keyring for %s", url)
  49. password = keyring.get_password(url, username)
  50. if password:
  51. return username, password
  52. except Exception as exc:
  53. logger.warning(
  54. "Keyring is skipped due to an exception: %s", str(exc),
  55. )
  56. keyring = None
  57. return None
  58. class MultiDomainBasicAuth(AuthBase):
  59. def __init__(self, prompting=True, index_urls=None):
  60. # type: (bool, Optional[List[str]]) -> None
  61. self.prompting = prompting
  62. self.index_urls = index_urls
  63. self.passwords = {} # type: Dict[str, AuthInfo]
  64. # When the user is prompted to enter credentials and keyring is
  65. # available, we will offer to save them. If the user accepts,
  66. # this value is set to the credentials they entered. After the
  67. # request authenticates, the caller should call
  68. # ``save_credentials`` to save these.
  69. self._credentials_to_save = None # type: Optional[Credentials]
  70. def _get_index_url(self, url):
  71. # type: (str) -> Optional[str]
  72. """Return the original index URL matching the requested URL.
  73. Cached or dynamically generated credentials may work against
  74. the original index URL rather than just the netloc.
  75. The provided url should have had its username and password
  76. removed already. If the original index url had credentials then
  77. they will be included in the return value.
  78. Returns None if no matching index was found, or if --no-index
  79. was specified by the user.
  80. """
  81. if not url or not self.index_urls:
  82. return None
  83. for u in self.index_urls:
  84. prefix = remove_auth_from_url(u).rstrip("/") + "/"
  85. if url.startswith(prefix):
  86. return u
  87. return None
  88. def _get_new_credentials(self, original_url, allow_netrc=True,
  89. allow_keyring=False):
  90. # type: (str, bool, bool) -> AuthInfo
  91. """Find and return credentials for the specified URL."""
  92. # Split the credentials and netloc from the url.
  93. url, netloc, url_user_password = split_auth_netloc_from_url(
  94. original_url,
  95. )
  96. # Start with the credentials embedded in the url
  97. username, password = url_user_password
  98. if username is not None and password is not None:
  99. logger.debug("Found credentials in url for %s", netloc)
  100. return url_user_password
  101. # Find a matching index url for this request
  102. index_url = self._get_index_url(url)
  103. if index_url:
  104. # Split the credentials from the url.
  105. index_info = split_auth_netloc_from_url(index_url)
  106. if index_info:
  107. index_url, _, index_url_user_password = index_info
  108. logger.debug("Found index url %s", index_url)
  109. # If an index URL was found, try its embedded credentials
  110. if index_url and index_url_user_password[0] is not None:
  111. username, password = index_url_user_password
  112. if username is not None and password is not None:
  113. logger.debug("Found credentials in index url for %s", netloc)
  114. return index_url_user_password
  115. # Get creds from netrc if we still don't have them
  116. if allow_netrc:
  117. netrc_auth = get_netrc_auth(original_url)
  118. if netrc_auth:
  119. logger.debug("Found credentials in netrc for %s", netloc)
  120. return netrc_auth
  121. # If we don't have a password and keyring is available, use it.
  122. if allow_keyring:
  123. # The index url is more specific than the netloc, so try it first
  124. kr_auth = (
  125. get_keyring_auth(index_url, username) or
  126. get_keyring_auth(netloc, username)
  127. )
  128. if kr_auth:
  129. logger.debug("Found credentials in keyring for %s", netloc)
  130. return kr_auth
  131. return username, password
  132. def _get_url_and_credentials(self, original_url):
  133. # type: (str) -> Tuple[str, Optional[str], Optional[str]]
  134. """Return the credentials to use for the provided URL.
  135. If allowed, netrc and keyring may be used to obtain the
  136. correct credentials.
  137. Returns (url_without_credentials, username, password). Note
  138. that even if the original URL contains credentials, this
  139. function may return a different username and password.
  140. """
  141. url, netloc, _ = split_auth_netloc_from_url(original_url)
  142. # Use any stored credentials that we have for this netloc
  143. username, password = self.passwords.get(netloc, (None, None))
  144. if username is None and password is None:
  145. # No stored credentials. Acquire new credentials without prompting
  146. # the user. (e.g. from netrc, keyring, or the URL itself)
  147. username, password = self._get_new_credentials(original_url)
  148. if username is not None or password is not None:
  149. # Convert the username and password if they're None, so that
  150. # this netloc will show up as "cached" in the conditional above.
  151. # Further, HTTPBasicAuth doesn't accept None, so it makes sense to
  152. # cache the value that is going to be used.
  153. username = username or ""
  154. password = password or ""
  155. # Store any acquired credentials.
  156. self.passwords[netloc] = (username, password)
  157. assert (
  158. # Credentials were found
  159. (username is not None and password is not None) or
  160. # Credentials were not found
  161. (username is None and password is None)
  162. ), f"Could not load credentials from url: {original_url}"
  163. return url, username, password
  164. def __call__(self, req):
  165. # type: (Request) -> Request
  166. # Get credentials for this request
  167. url, username, password = self._get_url_and_credentials(req.url)
  168. # Set the url of the request to the url without any credentials
  169. req.url = url
  170. if username is not None and password is not None:
  171. # Send the basic auth with this request
  172. req = HTTPBasicAuth(username, password)(req)
  173. # Attach a hook to handle 401 responses
  174. req.register_hook("response", self.handle_401)
  175. return req
  176. # Factored out to allow for easy patching in tests
  177. def _prompt_for_password(self, netloc):
  178. # type: (str) -> Tuple[Optional[str], Optional[str], bool]
  179. username = ask_input(f"User for {netloc}: ")
  180. if not username:
  181. return None, None, False
  182. auth = get_keyring_auth(netloc, username)
  183. if auth and auth[0] is not None and auth[1] is not None:
  184. return auth[0], auth[1], False
  185. password = ask_password("Password: ")
  186. return username, password, True
  187. # Factored out to allow for easy patching in tests
  188. def _should_save_password_to_keyring(self):
  189. # type: () -> bool
  190. if not keyring:
  191. return False
  192. return ask("Save credentials to keyring [y/N]: ", ["y", "n"]) == "y"
  193. def handle_401(self, resp, **kwargs):
  194. # type: (Response, **Any) -> Response
  195. # We only care about 401 responses, anything else we want to just
  196. # pass through the actual response
  197. if resp.status_code != 401:
  198. return resp
  199. # We are not able to prompt the user so simply return the response
  200. if not self.prompting:
  201. return resp
  202. parsed = urllib.parse.urlparse(resp.url)
  203. # Query the keyring for credentials:
  204. username, password = self._get_new_credentials(resp.url,
  205. allow_netrc=False,
  206. allow_keyring=True)
  207. # Prompt the user for a new username and password
  208. save = False
  209. if not username and not password:
  210. username, password, save = self._prompt_for_password(parsed.netloc)
  211. # Store the new username and password to use for future requests
  212. self._credentials_to_save = None
  213. if username is not None and password is not None:
  214. self.passwords[parsed.netloc] = (username, password)
  215. # Prompt to save the password to keyring
  216. if save and self._should_save_password_to_keyring():
  217. self._credentials_to_save = (parsed.netloc, username, password)
  218. # Consume content and release the original connection to allow our new
  219. # request to reuse the same one.
  220. resp.content
  221. resp.raw.release_conn()
  222. # Add our new username and password to the request
  223. req = HTTPBasicAuth(username or "", password or "")(resp.request)
  224. req.register_hook("response", self.warn_on_401)
  225. # On successful request, save the credentials that were used to
  226. # keyring. (Note that if the user responded "no" above, this member
  227. # is not set and nothing will be saved.)
  228. if self._credentials_to_save:
  229. req.register_hook("response", self.save_credentials)
  230. # Send our new request
  231. new_resp = resp.connection.send(req, **kwargs)
  232. new_resp.history.append(resp)
  233. return new_resp
  234. def warn_on_401(self, resp, **kwargs):
  235. # type: (Response, **Any) -> None
  236. """Response callback to warn about incorrect credentials."""
  237. if resp.status_code == 401:
  238. logger.warning(
  239. '401 Error, Credentials not correct for %s', resp.request.url,
  240. )
  241. def save_credentials(self, resp, **kwargs):
  242. # type: (Response, **Any) -> None
  243. """Response callback to save credentials on success."""
  244. assert keyring is not None, "should never reach here without keyring"
  245. if not keyring:
  246. return
  247. creds = self._credentials_to_save
  248. self._credentials_to_save = None
  249. if creds and resp.status_code < 400:
  250. try:
  251. logger.info('Saving credentials to keyring')
  252. keyring.set_password(*creds)
  253. except Exception:
  254. logger.exception('Failed to save credentials')