comparison easyhg2.py @ 432:4d0f151fca08

Half-baked auth file read/write (using Hg config, which can't write)
author Chris Cannam
date Tue, 28 Jun 2011 10:08:53 +0100
parents e57de4e97056
children c20da4213406
comparison
equal deleted inserted replaced
431:e57de4e97056 432:4d0f151fca08
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 16 import sys, os, stat
17 17
18 import urllib, urllib2, urlparse 18 import urllib, urllib2, urlparse
19 19
20 from mercurial import ui, util, config, error 20 from mercurial import ui, util, config, error
21 try: 21 try:
51 except ImportError: 51 except ImportError:
52 easyhg_pyqt_ok = False 52 easyhg_pyqt_ok = False
53 53
54 easyhg_qtapp = None 54 easyhg_qtapp = None
55 55
56 #!!! same as above for this? 56 #!!! same as above for this? or just continue without remember feature?
57 from Crypto.Cipher import AES 57 from Crypto.Cipher import AES
58 import base64 58 import base64
59 59
60 #!!! should be in a class here 60 #!!! should be in a class here
61 61
116 116
117 uri = canonical_url(authuri) 117 uri = canonical_url(authuri)
118 118
119 pkey = ('%s@@%s' % (uri, user)).replace('=', '__') 119 pkey = ('%s@@%s' % (uri, user)).replace('=', '__')
120 pekey = self.ui.config('easyhg', 'authkey') 120 pekey = self.ui.config('easyhg', 'authkey')
121 pfile = self.ui.config('easyhg', 'authfile') 121 pfile = os.path.expanduser(self.ui.config('easyhg', 'authfile'))
122 pdata = None 122 pdata = None
123 123
124 self.ui.write("pekey is %s\n" % pekey) 124 self.ui.write("pekey is %s\n" % pekey)
125 self.ui.write("pfile is %s\n" % pfile) 125 self.ui.write("pfile is %s\n" % pfile)
126 126
127 dialog = QtGui.QDialog() 127 dialog = QtGui.QDialog()
128 layout = QtGui.QGridLayout() 128 layout = QtGui.QGridLayout()
129 dialog.setLayout(layout) 129 dialog.setLayout(layout)
130 130
131 layout.addWidget(QtGui.QLabel(_('<h3>Login required</h3><p>Please log in to the repository at<br><code>%s</code>') % uri), 0, 0, 1, 2) 131 layout.addWidget(QtGui.QLabel(_('<h3>Login required</h3><p>Please provide your login details for the repository at<br><code>%s</code>:') % uri), 0, 0, 1, 2)
132 132
133 userfield = QtGui.QLineEdit() 133 userfield = QtGui.QLineEdit()
134 if user: 134 if user:
135 userfield.setText(user) 135 userfield.setText(user)
136 layout.addWidget(QtGui.QLabel(_('User:')), 1, 0) 136 layout.addWidget(QtGui.QLabel(_('User:')), 1, 0)
150 fp = None 150 fp = None
151 try: 151 try:
152 fp = open(pfile) 152 fp = open(pfile)
153 except: 153 except:
154 self.ui.write("failed to open authfile %s\n" % pfile) 154 self.ui.write("failed to open authfile %s\n" % pfile)
155 if fp: 155 if fp and not passwd:
156 pcfg.read(pfile) 156 pcfg.read(pfile)
157 pdata = pcfg.get('auth', pkey) 157 pdata = pcfg.get('auth', pkey)
158 cachedpwd = decrypt(pdata, pekey) 158 if pdata:
159 if not passwd: 159 cachedpwd = decrypt(pdata, pekey)
160 passfield.setText(cachedpwd) 160 passfield.setText(cachedpwd)
161 fp.close()
161 remember = QtGui.QCheckBox() 162 remember = QtGui.QCheckBox()
162 remember.setText(_('Remember this password until EasyMercurial exits')) 163 remember.setText(_('Remember this password until EasyMercurial exits'))
163 layout.addWidget(remember, 3, 1) 164 layout.addWidget(remember, 3, 1)
164 165
165 bb = QtGui.QDialogButtonBox() 166 bb = QtGui.QDialogButtonBox()
186 self.ui.write('Dialog accepted\n') 187 self.ui.write('Dialog accepted\n')
187 user = userfield.text() 188 user = userfield.text()
188 passwd = passfield.text() 189 passwd = passfield.text()
189 190
190 #!!! create pfile if necessary (with proper permissions), append auth data to it 191 #!!! create pfile if necessary (with proper permissions), append auth data to it
192 if pekey and pfile:
193
194 ofp = None
195
196 try:
197 ofp = open(pfile, 'a')
198 except:
199 self.ui.write("failed to open authfile %s for writing\n" % pfile)
200 raise
201
202 try:
203 os.fchmod(ofp.fileno(), stat.S_IRUSR | stat.S_IWUSR) #!!! Windows equivalent?
204 except:
205 ofp.close()
206 ofp = None
207 self.ui.write("failed to set proper permissions on authfile %s\n" % pfile)
208 raise
209
210 if ofp:
211 pdata = encrypt(passwd, pekey)
212 ofp.write('[auth]\n')
213 ofp.write(pkey + '=' + pdata + '\n')
214 ofp.close()
215
191 216
192 # if passwd and keyring_key != '' and not from_keyring: 217 # if passwd and keyring_key != '' and not from_keyring:
193 # keyring_key = '%s@@%s' % (uri, user) 218 # keyring_key = '%s@@%s' % (uri, user)
194 ## keyring.set_password('Mercurial', keyring_key, passwd) 219 ## keyring.set_password('Mercurial', keyring_key, passwd)
195 self.add_password(realm, authuri, user, passwd) 220 self.add_password(realm, authuri, user, passwd)