comparison mercurial/url.py @ 8333:89c80c3dc584

allow http authentication information to be specified in the configuration
author Sune Foldager <cryo@cyanite.org>
date Mon, 04 May 2009 20:26:27 +0200
parents 46293a0c7e9f
children 873429914ec5
comparison
equal deleted inserted replaced
8332:3e544c074459 8333:89c80c3dc584
103 def find_user_password(self, realm, authuri): 103 def find_user_password(self, realm, authuri):
104 authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm.find_user_password( 104 authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm.find_user_password(
105 self, realm, authuri) 105 self, realm, authuri)
106 user, passwd = authinfo 106 user, passwd = authinfo
107 if user and passwd: 107 if user and passwd:
108 self._writedebug(user, passwd)
108 return (user, passwd) 109 return (user, passwd)
109 110
110 if not self.ui.interactive(): 111 user, passwd = self._readauthtoken(authuri)
111 raise util.Abort(_('http authorization required')) 112 if not user or not passwd:
112 113 if not self.ui.interactive():
113 self.ui.write(_("http authorization required\n")) 114 raise util.Abort(_('http authorization required'))
114 self.ui.status(_("realm: %s\n") % realm) 115
115 if user: 116 self.ui.write(_("http authorization required\n"))
116 self.ui.status(_("user: %s\n") % user) 117 self.ui.status(_("realm: %s\n") % realm)
117 else: 118 if user:
118 user = self.ui.prompt(_("user:"), default=None) 119 self.ui.status(_("user: %s\n") % user)
119 120 else:
120 if not passwd: 121 user = self.ui.prompt(_("user:"), default=None)
121 passwd = self.ui.getpass() 122
123 if not passwd:
124 passwd = self.ui.getpass()
122 125
123 self.add_password(realm, authuri, user, passwd) 126 self.add_password(realm, authuri, user, passwd)
127 self._writedebug(user, passwd)
124 return (user, passwd) 128 return (user, passwd)
129
130 def _writedebug(self, user, passwd):
131 msg = _('http auth: user %s, password %s\n')
132 self.ui.debug(msg % (user, passwd and '*' * len(passwd) or 'not set'))
133
134 def _readauthtoken(self, uri):
135 # Read configuration
136 config = dict()
137 for key, val in self.ui.configitems('auth'):
138 group, setting = key.split('.', 1)
139 gdict = config.setdefault(group, dict())
140 gdict[setting] = val
141
142 # Find the best match
143 scheme, hostpath = uri.split('://', 1)
144 bestlen = 0
145 bestauth = None, None
146 for auth in config.itervalues():
147 prefix = auth.get('prefix')
148 if not prefix: continue
149 p = prefix.split('://', 1)
150 if len(p) > 1:
151 schemes, prefix = [p[0]], p[1]
152 else:
153 schemes = (auth.get('schemes') or 'https').split()
154 if (prefix == '*' or hostpath.startswith(prefix)) and \
155 len(prefix) > bestlen and scheme in schemes:
156 bestlen = len(prefix)
157 bestauth = auth.get('username'), auth.get('password')
158 return bestauth
125 159
126 class proxyhandler(urllib2.ProxyHandler): 160 class proxyhandler(urllib2.ProxyHandler):
127 def __init__(self, ui): 161 def __init__(self, ui):
128 proxyurl = ui.config("http_proxy", "host") or os.getenv('http_proxy') 162 proxyurl = ui.config("http_proxy", "host") or os.getenv('http_proxy')
129 # XXX proxyauthinfo = None 163 # XXX proxyauthinfo = None