[PATCH 1 of 3 RFC] url: extract password database from password manager

liscju piotr.listkiewicz at gmail.com
Thu Jun 9 11:02:55 UTC 2016


# HG changeset patch
# User liscju <piotr.listkiewicz at gmail.com>
# Date 1465162583 -7200
#      Sun Jun 05 23:36:23 2016 +0200
# Node ID 45be12e882c975ff1acfa368d65bff7729eae593
# Parent  90d84e1e427a9d65aedd870cdb7283f84bb30141
url: extract password database from password manager

So far password manager was keeping authentication
information so opening new connection and creating
new password manager made all saved authentication
information lost.

This commit separates password manager and
password database to make it possible to reuse
saved authentication information.

diff --git a/hgext/factotum.py b/hgext/factotum.py
--- a/hgext/factotum.py
+++ b/hgext/factotum.py
@@ -102,8 +102,7 @@ def monkeypatch_method(cls):
 
 @monkeypatch_method(passwordmgr)
 def find_user_password(self, realm, authuri):
-    user, passwd = urlreq.httppasswordmgrwithdefaultrealm.find_user_password(
-        self, realm, authuri)
+    user, passwd = self.passwddb.find_user_password(realm, authuri)
     if user and passwd:
         self._writedebug(user, passwd)
         return (user, passwd)
diff --git a/mercurial/url.py b/mercurial/url.py
--- a/mercurial/url.py
+++ b/mercurial/url.py
@@ -27,14 +27,19 @@ stringio = util.stringio
 urlerr = util.urlerr
 urlreq = util.urlreq
 
+class passwordmgrdb(urlreq.httppasswordmgrwithdefaultrealm):
+    pass
+
 class passwordmgr(urlreq.httppasswordmgrwithdefaultrealm):
-    def __init__(self, ui):
-        urlreq.httppasswordmgrwithdefaultrealm.__init__(self)
+    def __init__(self, ui, passwddb):
         self.ui = ui
+        self.passwddb = passwddb
+
+    def add_password(self, realm, uri, user, passwd):
+        return self.passwddb.add_password(realm, uri, user, passwd)
 
     def find_user_password(self, realm, authuri):
-        authinfo = urlreq.httppasswordmgrwithdefaultrealm.find_user_password(
-            self, realm, authuri)
+        authinfo = self.passwddb.find_user_password(realm, authuri)
         user, passwd = authinfo
         if user and passwd:
             self._writedebug(user, passwd)
@@ -64,7 +69,7 @@ class passwordmgr(urlreq.httppasswordmgr
             if not passwd:
                 passwd = self.ui.getpass()
 
-        self.add_password(realm, authuri, user, passwd)
+        self.passwddb.add_password(realm, authuri, user, passwd)
         self._writedebug(user, passwd)
         return (user, passwd)
 
@@ -73,8 +78,7 @@ class passwordmgr(urlreq.httppasswordmgr
         self.ui.debug(msg % (user, passwd and '*' * len(passwd) or 'not set'))
 
     def find_stored_password(self, authuri):
-        return urlreq.httppasswordmgrwithdefaultrealm.find_user_password(
-            self, None, authuri)
+        return self.passwddb.find_user_password(None, authuri)
 
 class proxyhandler(urlreq.proxyhandler):
     def __init__(self, ui):
@@ -363,7 +367,7 @@ if has_https:
             keepalive.KeepAliveHandler.__init__(self)
             urlreq.httpshandler.__init__(self)
             self.ui = ui
-            self.pwmgr = passwordmgr(self.ui)
+            self.pwmgr = passwordmgr(self.ui, passwordmgrdb())
 
         def _start_transaction(self, h, req):
             _generic_start_transaction(self, h, req)
@@ -477,7 +481,9 @@ def opener(ui, authinfo=None):
     '''
     # experimental config: ui.usehttp2
     if ui.configbool('ui', 'usehttp2', False):
-        handlers = [httpconnectionmod.http2handler(ui, passwordmgr(ui))]
+        handlers = [
+            httpconnectionmod.http2handler(ui, passwordmgr(ui, passwordmgrdb()))
+        ]
     else:
         handlers = [httphandler()]
         if has_https:
@@ -485,7 +491,7 @@ def opener(ui, authinfo=None):
 
     handlers.append(proxyhandler(ui))
 
-    passmgr = passwordmgr(ui)
+    passmgr = passwordmgr(ui, passwordmgrdb())
     if authinfo is not None:
         passmgr.add_password(*authinfo)
         user, passwd = authinfo[2:4]
diff --git a/tests/test-hgweb-auth.py b/tests/test-hgweb-auth.py
--- a/tests/test-hgweb-auth.py
+++ b/tests/test-hgweb-auth.py
@@ -43,7 +43,7 @@ def test(auth, urls=None):
     def _test(uri):
         print('URI:', uri)
         try:
-            pm = url.passwordmgr(ui)
+            pm = url.passwordmgr(ui, url.passwordmgrdb())
             u, authinfo = util.url(uri).authinfo()
             if authinfo is not None:
                 pm.add_password(*authinfo)


More information about the Mercurial-devel mailing list