소스 검색

Raise a HTTPUnauthorized error when the server responds with a 401.

Fixes #691

This allows clients to retry after e.g. looking up or prompting for credentials. It also allows them to manage their own strategy wrt retries or trying different passwords.
Jelmer Vernooij 5 년 전
부모
커밋
523533614d
2개의 변경된 파일14개의 추가작업 그리고 0개의 파일을 삭제
  1. 4 0
      NEWS
  2. 10 0
      dulwich/client.py

+ 4 - 0
NEWS

@@ -10,6 +10,10 @@
    no new objects to be sent.
    (Jelmer Vernooij, #739)
 
+ * Raise new error HTTPUnauthorized when the server sends
+   back a 401. The client can then retry with credentials.
+   (Jelmer Vernooij, #691)
+
 0.20.2	2020-06-01
 
  * Brown bag release to fix uploads of Windows wheels.

+ 10 - 0
dulwich/client.py

@@ -120,6 +120,14 @@ class InvalidWants(Exception):
             "requested wants not in server provided refs: %r" % wants)
 
 
+class HTTPUnauthorized(Exception):
+    """Raised when authentication fails."""
+
+    def __init__(self, www_authenticate):
+        Exception.__init__(self, "No valid credentials provided")
+        self.www_authenticate = www_authenticate
+
+
 def _fileno_can_read(fileno):
     """Check if a file descriptor is readable.
     """
@@ -1599,6 +1607,8 @@ class HttpGitClient(GitClient):
 
         if resp.status == 404:
             raise NotGitRepository()
+        elif resp.status == 401:
+            raise HTTPUnauthorized(resp.getheader('WWW-Authenticate'))
         elif resp.status != 200:
             raise GitProtocolError("unexpected http resp %d for %s" %
                                    (resp.status, url))