changeset 635:ca7fc2d23a21

support for http(s) basic authentication This is an adaptation of the original patch submitted in [1], without the monkey-patching: a patch has been committed in dulwich [2] which allows clients to supply a custom urllib2 "opener" for opening the url; here, we provide such an opener, which provides authentication information obtained from the hg config. [1] https://groups.google.com/forum/#!topic/hg-git/9clPr1wdtiw [2] https://bugs.launchpad.net/dulwich/+bug/909037
author Dov Feldstern <dovdevel@gmail.com>
date Thu, 13 Feb 2014 01:37:22 +0200
parents a836fc8f6c76
children f933e3930f78
files hggit/git_handler.py
diffstat 1 files changed, 32 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/hggit/git_handler.py
+++ b/hggit/git_handler.py
@@ -1,4 +1,4 @@
-import os, math, urllib, re
+import os, math, urllib, urllib2, re
 import stat, posixpath, StringIO
 
 from dulwich.errors import HangupException, GitProtocolError, UpdateRefsError
@@ -1376,7 +1376,37 @@
             if not httpclient:
                 raise RepoError('git via HTTP requires dulwich 0.8.1 or later')
             else:
-                return client.HttpGitClient(uri, thin_packs=False), uri
+                auth_handler = urllib2.HTTPBasicAuthHandler(AuthManager(self.ui))
+                opener = urllib2.build_opener(auth_handler)
+                return client.HttpGitClient(uri, opener=opener, thin_packs=False), uri
 
         # if its not git or git+ssh, try a local url..
         return client.SubprocessGitClient(thin_packs=False), uri
+
+class AuthManager(object):
+    def __init__(self, ui):
+        self.ui = ui
+
+    def add_password(self, realm, uri, user, passwd):
+        raise NotImplementedError(
+            'AuthManager currently gets passwords from hg repo config')
+
+    def find_user_password(self, realm, authuri):
+
+        # find a stanza in the auth section which matches this uri
+        for item in self.ui.configitems('auth'):
+            if len(item) < 2:
+                continue
+            if item[0].endswith('.prefix') and authuri.startswith(item[1]):
+                prefix = item[0][:-len('.prefix')]
+                break
+        else:
+            # no matching stanza found!
+            return (None,None)
+
+        self.ui.note(_('using "%s" auth credentials\n') % (prefix,))
+        username = self.ui.config('auth', '%s.username' % prefix)
+        password = self.ui.config('auth', '%s.password' % prefix)
+
+        return (username,password)
+