auth.py 11 KB

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