comparison easyhg2.py @ 438:a5696a1f2dc5

Regenerate authfile key after dialog accepted (user might have changed); don't save if no user
author Chris Cannam
date Tue, 28 Jun 2011 13:50:49 +0100
parents 765255e9cc92
children 51c5332aa957
comparison
equal deleted inserted replaced
437:765255e9cc92 438:a5696a1f2dc5
11 # modify it under the terms of the GNU General Public License as 11 # modify it under the terms of the GNU General Public License as
12 # published by the Free Software Foundation; either version 2 of the 12 # published by the Free Software Foundation; either version 2 of the
13 # License, or (at your option) any later version. See the file 13 # License, or (at your option) any later version. See the file
14 # COPYING included with this distribution for more information. 14 # COPYING included with this distribution for more information.
15 15
16 import sys, os, stat 16 import sys, os, stat, urllib, urllib2, urlparse
17 17
18 import urllib, urllib2, urlparse 18 from mercurial.i18n import _
19
20 from mercurial import ui, util, error 19 from mercurial import ui, util, error
21 try: 20 try:
22 from mercurial.url import passwordmgr 21 from mercurial.url import passwordmgr
23 except: 22 except:
24 from mercurial.httprepo import passwordmgr 23 from mercurial.httprepo import passwordmgr
25
26 from mercurial.i18n import _
27 24
28 # The value assigned here may be modified during installation, by 25 # The value assigned here may be modified during installation, by
29 # replacing its default value with another one. We can't compare 26 # replacing its default value with another one. We can't compare
30 # against its default value, because then the comparison text would 27 # against its default value, because then the comparison text would
31 # get modified as well. So, compare using prefix only. 28 # get modified as well. So, compare using prefix only.
50 from PyQt4 import Qt, QtGui 47 from PyQt4 import Qt, QtGui
51 except ImportError: 48 except ImportError:
52 easyhg_pyqt_ok = False 49 easyhg_pyqt_ok = False
53 easyhg_qtapp = None 50 easyhg_qtapp = None
54 51
52 # These imports are optional, we just can't use the authfile (=
53 # "remember this password") feature without them
54 #
55 easyhg_authfile_imports_ok = True 55 easyhg_authfile_imports_ok = True
56 try: 56 try:
57 from Crypto.Cipher import AES 57 from Crypto.Cipher import AES
58 import ConfigParser # Mercurial version won't write files 58 import ConfigParser # Mercurial version won't write files
59 import base64 59 import base64
116 ofp = open(pfile, 'w') 116 ofp = open(pfile, 'w')
117 except: 117 except:
118 self.ui.write("failed to open authfile %s for writing\n" % pfile) 118 self.ui.write("failed to open authfile %s for writing\n" % pfile)
119 raise 119 raise
120 try: 120 try:
121 os.fchmod(ofp.fileno(), stat.S_IRUSR | stat.S_IWUSR) #!!! Windows equivalent? 121 #!!! Windows equivalent?
122 os.fchmod(ofp.fileno(), stat.S_IRUSR | stat.S_IWUSR)
122 except: 123 except:
123 ofp.close() 124 ofp.close()
124 ofp = None 125 ofp = None
125 self.ui.write("failed to set proper permissions on authfile %s\n" % pfile) 126 self.ui.write("failed to set permissions on authfile %s\n" % pfile)
126 raise 127 raise
127 pcfg.write(ofp) 128 pcfg.write(ofp)
128 ofp.close() 129 ofp.close()
129 130
130 def get_from_config(pcfg, sect, key): 131 def get_from_config(pcfg, sect, key):
146 def set_to_config(pcfg, sect, key, data): 147 def set_to_config(pcfg, sect, key, data):
147 if not pcfg.has_section(sect): 148 if not pcfg.has_section(sect):
148 pcfg.add_section(sect) 149 pcfg.add_section(sect)
149 pcfg.set(sect, key, data) 150 pcfg.set(sect, key, data)
150 151
152 def remote_key(uri, user):
153 return base64.b64encode('%s@@%s' % (uri, user)).replace('=', '_')
154
151 @monkeypatch_method(passwordmgr) 155 @monkeypatch_method(passwordmgr)
152 def find_user_password(self, realm, authuri): 156 def find_user_password(self, realm, authuri):
153 157
154 if not self.ui.interactive(): 158 if not self.ui.interactive():
155 return orig_find(self, realm, authuri) 159 return orig_find(self, realm, authuri)
165 169
166 self.ui.write("want username and/or password for %s\n" % authuri) 170 self.ui.write("want username and/or password for %s\n" % authuri)
167 171
168 uri = canonical_url(authuri) 172 uri = canonical_url(authuri)
169 173
170 pkey = None
171 pekey = self.ui.config('easyhg', 'authkey') 174 pekey = self.ui.config('easyhg', 'authkey')
172 pfile = self.ui.config('easyhg', 'authfile') 175 pfile = self.ui.config('easyhg', 'authfile')
173 use_authfile = (easyhg_authfile_imports_ok and pekey and pfile) 176 use_authfile = (easyhg_authfile_imports_ok and pekey and pfile)
174 if use_authfile:
175 pkey = base64.b64encode('%s@@%s' % (uri, user)).replace('=', '_')
176 if pfile: 177 if pfile:
177 pfile = os.path.expanduser(pfile) 178 pfile = os.path.expanduser(pfile)
178 pdata = None 179 pdata = None
179 180
180 self.ui.write("pekey is %s\n" % pekey) 181 self.ui.write("pekey is %s\n" % pekey)
207 if use_authfile: 208 if use_authfile:
208 # load pwd from our cache file, decrypt with given key 209 # load pwd from our cache file, decrypt with given key
209 pcfg = ConfigParser.RawConfigParser() 210 pcfg = ConfigParser.RawConfigParser()
210 load_config(pcfg, pfile) 211 load_config(pcfg, pfile)
211 remember_default = get_boolean_from_config(pcfg, 'preferences', 'remember', False) 212 remember_default = get_boolean_from_config(pcfg, 'preferences', 'remember', False)
212 pdata = get_from_config(pcfg, 'auth', pkey) 213 pdata = get_from_config(pcfg, 'auth', remote_key(uri, user))
213 if pdata: 214 if pdata:
214 cachedpwd = decrypt(pdata, pekey) 215 cachedpwd = decrypt(pdata, pekey)
215 passfield.setText(cachedpwd) 216 passfield.setText(cachedpwd)
216 remember = QtGui.QCheckBox() 217 remember = QtGui.QCheckBox()
217 remember.setChecked(remember_default) 218 remember.setChecked(remember_default)
245 user = userfield.text() 246 user = userfield.text()
246 passwd = passfield.text() 247 passwd = passfield.text()
247 248
248 if use_authfile: 249 if use_authfile:
249 set_to_config(pcfg, 'preferences', 'remember', remember.isChecked()) 250 set_to_config(pcfg, 'preferences', 'remember', remember.isChecked())
250 if remember.isChecked(): 251 if user:
251 pdata = encrypt(passwd, pekey) 252 if passwd and remember.isChecked():
252 set_to_config(pcfg, 'auth', pkey, pdata) 253 pdata = encrypt(passwd, pekey)
253 else: 254 set_to_config(pcfg, 'auth', remote_key(uri, user), pdata)
254 set_to_config(pcfg, 'auth', pkey, '') 255 else:
256 set_to_config(pcfg, 'auth', remote_key(uri, user), '')
255 save_config(pcfg, pfile) 257 save_config(pcfg, pfile)
256 258
257 self.add_password(realm, authuri, user, passwd) 259 self.add_password(realm, authuri, user, passwd)
258 return (user, passwd) 260 return (user, passwd)
259 261