comparison mercurial/url.py @ 29377:2c019aac6b99

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. This commit violates code checker because it adds add_password method (name with underscore) to passwordmgr object to provide method required by urllib2.
author liscju <piotr.listkiewicz@gmail.com>
date Sun, 05 Jun 2016 23:36:23 +0200
parents f1fe92c6c03c
children fea71f66ebff
comparison
equal deleted inserted replaced
29376:113d0b23321a 29377:2c019aac6b99
25 stringio = util.stringio 25 stringio = util.stringio
26 26
27 urlerr = util.urlerr 27 urlerr = util.urlerr
28 urlreq = util.urlreq 28 urlreq = util.urlreq
29 29
30 class passwordmgr(urlreq.httppasswordmgrwithdefaultrealm): 30 class passwordmgr(object):
31 def __init__(self, ui): 31 def __init__(self, ui, passwddb):
32 urlreq.httppasswordmgrwithdefaultrealm.__init__(self)
33 self.ui = ui 32 self.ui = ui
33 self.passwddb = passwddb
34
35 def add_password(self, realm, uri, user, passwd):
36 return self.passwddb.add_password(realm, uri, user, passwd)
34 37
35 def find_user_password(self, realm, authuri): 38 def find_user_password(self, realm, authuri):
36 authinfo = urlreq.httppasswordmgrwithdefaultrealm.find_user_password( 39 authinfo = self.passwddb.find_user_password(realm, authuri)
37 self, realm, authuri)
38 user, passwd = authinfo 40 user, passwd = authinfo
39 if user and passwd: 41 if user and passwd:
40 self._writedebug(user, passwd) 42 self._writedebug(user, passwd)
41 return (user, passwd) 43 return (user, passwd)
42 44
62 user = self.ui.prompt(_("user:"), default=None) 64 user = self.ui.prompt(_("user:"), default=None)
63 65
64 if not passwd: 66 if not passwd:
65 passwd = self.ui.getpass() 67 passwd = self.ui.getpass()
66 68
67 self.add_password(realm, authuri, user, passwd) 69 self.passwddb.add_password(realm, authuri, user, passwd)
68 self._writedebug(user, passwd) 70 self._writedebug(user, passwd)
69 return (user, passwd) 71 return (user, passwd)
70 72
71 def _writedebug(self, user, passwd): 73 def _writedebug(self, user, passwd):
72 msg = _('http auth: user %s, password %s\n') 74 msg = _('http auth: user %s, password %s\n')
73 self.ui.debug(msg % (user, passwd and '*' * len(passwd) or 'not set')) 75 self.ui.debug(msg % (user, passwd and '*' * len(passwd) or 'not set'))
74 76
75 def find_stored_password(self, authuri): 77 def find_stored_password(self, authuri):
76 return urlreq.httppasswordmgrwithdefaultrealm.find_user_password( 78 return self.passwddb.find_user_password(None, authuri)
77 self, None, authuri)
78 79
79 class proxyhandler(urlreq.proxyhandler): 80 class proxyhandler(urlreq.proxyhandler):
80 def __init__(self, ui): 81 def __init__(self, ui):
81 proxyurl = ui.config("http_proxy", "host") or os.getenv('http_proxy') 82 proxyurl = ui.config("http_proxy", "host") or os.getenv('http_proxy')
82 # XXX proxyauthinfo = None 83 # XXX proxyauthinfo = None
361 class httpshandler(keepalive.KeepAliveHandler, urlreq.httpshandler): 362 class httpshandler(keepalive.KeepAliveHandler, urlreq.httpshandler):
362 def __init__(self, ui): 363 def __init__(self, ui):
363 keepalive.KeepAliveHandler.__init__(self) 364 keepalive.KeepAliveHandler.__init__(self)
364 urlreq.httpshandler.__init__(self) 365 urlreq.httpshandler.__init__(self)
365 self.ui = ui 366 self.ui = ui
366 self.pwmgr = passwordmgr(self.ui) 367 self.pwmgr = passwordmgr(self.ui,
368 urlreq.httppasswordmgrwithdefaultrealm())
367 369
368 def _start_transaction(self, h, req): 370 def _start_transaction(self, h, req):
369 _generic_start_transaction(self, h, req) 371 _generic_start_transaction(self, h, req)
370 return keepalive.KeepAliveHandler._start_transaction(self, h, req) 372 return keepalive.KeepAliveHandler._start_transaction(self, h, req)
371 373
475 construct an opener suitable for urllib2 477 construct an opener suitable for urllib2
476 authinfo will be added to the password manager 478 authinfo will be added to the password manager
477 ''' 479 '''
478 # experimental config: ui.usehttp2 480 # experimental config: ui.usehttp2
479 if ui.configbool('ui', 'usehttp2', False): 481 if ui.configbool('ui', 'usehttp2', False):
480 handlers = [httpconnectionmod.http2handler(ui, passwordmgr(ui))] 482 handlers = [
483 httpconnectionmod.http2handler(
484 ui,
485 passwordmgr(ui, urlreq.httppasswordmgrwithdefaultrealm()))
486 ]
481 else: 487 else:
482 handlers = [httphandler()] 488 handlers = [httphandler()]
483 if has_https: 489 if has_https:
484 handlers.append(httpshandler(ui)) 490 handlers.append(httpshandler(ui))
485 491
486 handlers.append(proxyhandler(ui)) 492 handlers.append(proxyhandler(ui))
487 493
488 passmgr = passwordmgr(ui) 494 passmgr = passwordmgr(ui, urlreq.httppasswordmgrwithdefaultrealm())
489 if authinfo is not None: 495 if authinfo is not None:
490 passmgr.add_password(*authinfo) 496 passmgr.add_password(*authinfo)
491 user, passwd = authinfo[2:4] 497 user, passwd = authinfo[2:4]
492 ui.debug('http auth: user %s, password %s\n' % 498 ui.debug('http auth: user %s, password %s\n' %
493 (user, passwd and '*' * len(passwd) or 'not set')) 499 (user, passwd and '*' * len(passwd) or 'not set'))