From aef521ee6132c071b2a599540cc994f12feef8e0 Mon Sep 17 00:00:00 2001 From: Kevin R Patterson Date: Thu, 26 Jul 2018 17:14:16 -0500 Subject: [PATCH 1/4] give 401 warning if username/password do not work for URL --- news/4833.bugfix | 1 + src/pip/_internal/download.py | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 news/4833.bugfix diff --git a/news/4833.bugfix b/news/4833.bugfix new file mode 100644 index 00000000000..9bb9fdaa93b --- /dev/null +++ b/news/4833.bugfix @@ -0,0 +1 @@ +give 401 warning if username/password do not work for URL diff --git a/src/pip/_internal/download.py b/src/pip/_internal/download.py index 96f3b65c6c4..9506c17d45a 100644 --- a/src/pip/_internal/download.py +++ b/src/pip/_internal/download.py @@ -199,6 +199,7 @@ def handle_401(self, resp, **kwargs): # Add our new username and password to the request req = HTTPBasicAuth(username or "", password or "")(resp.request) + req.register_hook("response", self.handle_401_again) # Send our new request new_resp = resp.connection.send(req, **kwargs) @@ -206,6 +207,16 @@ def handle_401(self, resp, **kwargs): return new_resp + def handle_401_again(self, resp, **kwargs): + # warn user that they provided incorrect credentials + if resp.status_code != 401: + return resp + + logger.warning('401 Error, Credentials not correct for %s', + resp.request.url) + return resp + + def parse_credentials(self, netloc): if "@" in netloc: userinfo = netloc.rsplit("@", 1)[0] From 4e0c433c37a7713fde246fcc2706e0185bcaeef4 Mon Sep 17 00:00:00 2001 From: Kevin R Patterson Date: Thu, 26 Jul 2018 17:17:40 -0500 Subject: [PATCH 2/4] fix extra line after handle_401_again function --- src/pip/_internal/download.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pip/_internal/download.py b/src/pip/_internal/download.py index 9506c17d45a..a003a1496ea 100644 --- a/src/pip/_internal/download.py +++ b/src/pip/_internal/download.py @@ -215,7 +215,6 @@ def handle_401_again(self, resp, **kwargs): logger.warning('401 Error, Credentials not correct for %s', resp.request.url) return resp - def parse_credentials(self, netloc): if "@" in netloc: From b7222f3d264983e69ee3ffd14a0dc1e1d07417e3 Mon Sep 17 00:00:00 2001 From: Kevin R Patterson Date: Fri, 17 Aug 2018 08:25:53 -0500 Subject: [PATCH 3/4] handle_401_warn for when invalid credentials are given --- src/pip/_internal/download.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/pip/_internal/download.py b/src/pip/_internal/download.py index a003a1496ea..2bc9d422448 100644 --- a/src/pip/_internal/download.py +++ b/src/pip/_internal/download.py @@ -199,7 +199,7 @@ def handle_401(self, resp, **kwargs): # Add our new username and password to the request req = HTTPBasicAuth(username or "", password or "")(resp.request) - req.register_hook("response", self.handle_401_again) + req.register_hook("response", self.handle_401_warn) # Send our new request new_resp = resp.connection.send(req, **kwargs) @@ -207,13 +207,11 @@ def handle_401(self, resp, **kwargs): return new_resp - def handle_401_again(self, resp, **kwargs): + def handle_401_warn(self, resp, **kwargs): # warn user that they provided incorrect credentials - if resp.status_code != 401: - return resp - - logger.warning('401 Error, Credentials not correct for %s', - resp.request.url) + if resp.status_code == 401: + logger.warning('401 Error, Credentials not correct for %s', + resp.request.url) return resp def parse_credentials(self, netloc): From e3c869fa60f99573db50291492c4edc21b78892f Mon Sep 17 00:00:00 2001 From: Kevin R Patterson Date: Fri, 17 Aug 2018 11:07:45 -0500 Subject: [PATCH 4/4] rename to warn_on_401, remove extra return --- src/pip/_internal/download.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/pip/_internal/download.py b/src/pip/_internal/download.py index 2bc9d422448..6fc8c7e3f3e 100644 --- a/src/pip/_internal/download.py +++ b/src/pip/_internal/download.py @@ -199,7 +199,7 @@ def handle_401(self, resp, **kwargs): # Add our new username and password to the request req = HTTPBasicAuth(username or "", password or "")(resp.request) - req.register_hook("response", self.handle_401_warn) + req.register_hook("response", self.warn_on_401) # Send our new request new_resp = resp.connection.send(req, **kwargs) @@ -207,12 +207,11 @@ def handle_401(self, resp, **kwargs): return new_resp - def handle_401_warn(self, resp, **kwargs): + def warn_on_401(self, resp, **kwargs): # warn user that they provided incorrect credentials if resp.status_code == 401: logger.warning('401 Error, Credentials not correct for %s', resp.request.url) - return resp def parse_credentials(self, netloc): if "@" in netloc: