Chris@427: # -*- coding: utf-8 -*- Chris@427: # Chris@427: # EasyMercurial Chris@427: # Chris@427: # Based on hgExplorer by Jari Korhonen Chris@427: # Copyright (c) 2010 Jari Korhonen Chris@560: # Copyright (c) 2010-2012 Chris Cannam Chris@560: # Copyright (c) 2010-2012 Queen Mary, University of London Chris@427: # Chris@427: # This program is free software; you can redistribute it and/or Chris@427: # modify it under the terms of the GNU General Public License as Chris@427: # published by the Free Software Foundation; either version 2 of the Chris@427: # License, or (at your option) any later version. See the file Chris@427: # COPYING included with this distribution for more information. Chris@427: Chris@503: import sys, os, stat, urllib, urllib2, urlparse, hashlib Chris@427: Chris@438: from mercurial.i18n import _ Chris@433: from mercurial import ui, util, error Chris@427: try: Chris@427: from mercurial.url import passwordmgr Chris@427: except: Chris@427: from mercurial.httprepo import passwordmgr Chris@427: Chris@427: # The value assigned here may be modified during installation, by Chris@427: # replacing its default value with another one. We can't compare Chris@427: # against its default value, because then the comparison text would Chris@427: # get modified as well. So, compare using prefix only. Chris@427: # Chris@427: easyhg_import_path = 'NO_EASYHG_IMPORT_PATH' Chris@427: if not easyhg_import_path.startswith('NO_'): Chris@427: # We have an installation path: append it twice, once with Chris@427: # the Python version suffixed Chris@440: version_suffix = 'Py%d.%d' % (sys.version_info[0], sys.version_info[1]) Chris@427: sys.path.append(easyhg_import_path + "/" + version_suffix) Chris@427: sys.path.append(easyhg_import_path) Chris@427: Chris@427: # Try to load the PyQt4 module that we need. If this fails, we should Chris@427: # bail out later (in uisetup), because if we bail out now, Mercurial Chris@427: # will just continue without us and report success. The invoking Chris@427: # application needs to be able to discover whether the module load Chris@427: # succeeded or not, so we need to ensure that Mercurial itself returns Chris@427: # failure if it didn't. Chris@427: # Chris@427: easyhg_pyqt_ok = True Chris@427: try: chris@656: from PyQt4 import QtCore, QtGui Chris@427: except ImportError: Chris@427: easyhg_pyqt_ok = False Chris@427: easyhg_qtapp = None Chris@427: Chris@440: # These imports are optional, we just can't use the authfile (i.e. Chris@438: # "remember this password") feature without them Chris@438: # Chris@437: easyhg_authfile_imports_ok = True Chris@602: Chris@437: try: Chris@437: from Crypto.Cipher import AES Chris@602: except ImportError: Chris@602: print "EasyHg: Failed to import Crypto.Cipher module required for authfile support (try installing PyCrypto?)" Chris@602: easyhg_authfile_imports_ok = False Chris@602: Chris@602: try: Chris@437: import ConfigParser # Mercurial version won't write files Chris@437: import base64 Chris@437: except ImportError: Chris@602: print "EasyHg: Failed to import modules (ConfigParser, base64) required for authfile support" Chris@437: easyhg_authfile_imports_ok = False Chris@433: Chris@431: Chris@452: class EasyHgAuthStore(object): Chris@431: Chris@452: def __init__(self, ui, url, user, passwd): Chris@452: Chris@452: self.ui = ui Chris@452: self.remote_url = url Chris@452: Chris@452: self.user = user Chris@452: self.passwd = passwd Chris@452: Chris@452: self.auth_key = self.ui.config('easyhg', 'authkey') Chris@452: self.auth_file = self.ui.config('easyhg', 'authfile') Chris@452: Chris@452: self.use_auth_file = (easyhg_authfile_imports_ok and Chris@452: self.auth_key and self.auth_file) Chris@452: Chris@458: self.auth_config = None Chris@458: self.auth_cipher = None Chris@458: self.remember = False Chris@458: Chris@452: if self.use_auth_file: Chris@602: self.auth_cipher = AES.new(self.auth_key, AES.MODE_CBC, Chris@602: os.urandom(16)) Chris@452: self.auth_file = os.path.expanduser(self.auth_file) Chris@452: self.load_auth_data() Chris@452: Chris@452: def save(self): Chris@452: if self.use_auth_file: Chris@452: self.save_auth_data() Chris@452: Chris@452: def encrypt(self, text): Chris@452: iv = os.urandom(12) Chris@452: text = '%s.%d.%s.easyhg' % (base64.b64encode(iv), len(text), text) Chris@452: text += (16 - (len(text) % 16)) * ' ' Chris@452: ctext = base64.b64encode(self.auth_cipher.encrypt(text)) Chris@452: return ctext Chris@452: Chris@452: def decrypt(self, ctext): Chris@448: try: Chris@456: text = self.auth_cipher.decrypt(base64.b64decode(ctext)) Chris@456: (iv, d, text) = text.partition('.') Chris@456: (tlen, d, text) = text.partition('.') Chris@452: return text[0:int(tlen)] Chris@448: except: Chris@452: self.ui.write("failed to decrypt/convert cached data!") Chris@452: return '' Chris@452: Chris@452: def argless_url(self): Chris@452: parsed = urlparse.urlparse(self.remote_url) Chris@452: return "%s://%s%s" % (parsed.scheme, parsed.netloc, parsed.path) Chris@452: Chris@452: def pathless_url(self): Chris@452: parsed = urlparse.urlparse(self.remote_url) Chris@452: return "%s://%s" % (parsed.scheme, parsed.netloc) Chris@452: Chris@452: def load_config(self): Chris@453: if not self.auth_config: Chris@453: self.auth_config = ConfigParser.RawConfigParser() Chris@452: fp = None Chris@452: try: Chris@452: fp = open(self.auth_file) Chris@452: except: Chris@452: self.ui.write("unable to read authfile %s, ignoring\n" % self.auth_file) Chris@452: return Chris@452: self.auth_config.readfp(fp) Chris@452: fp.close() Chris@452: Chris@452: def save_config(self): Chris@452: ofp = None Chris@452: try: Chris@452: ofp = open(self.auth_file, 'w') Chris@452: except: Chris@452: self.ui.write("failed to open authfile %s for writing\n" % self.auth_file) Chris@448: raise Chris@503: if os.name == 'posix': Chris@452: try: Chris@452: os.fchmod(ofp.fileno(), stat.S_IRUSR | stat.S_IWUSR) Chris@452: except: Chris@452: ofp.close() Chris@452: self.ui.write("failed to set permissions on authfile %s\n" % self.auth_file) Chris@452: raise Chris@452: self.auth_config.write(ofp) Chris@452: ofp.close() Chris@434: Chris@452: def get_from_config(self, sect, key): Chris@452: data = None Chris@452: try: Chris@452: data = self.auth_config.get(sect, key) Chris@452: except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): Chris@452: pass Chris@452: return data Chris@436: Chris@452: def get_boolean_from_config(self, sect, key, deflt): Chris@452: data = deflt Chris@452: try: Chris@452: data = self.auth_config.getboolean(sect, key) Chris@452: except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): Chris@452: pass Chris@452: return data Chris@436: Chris@452: def set_to_config(self, sect, key, data): Chris@452: if not self.auth_config.has_section(sect): Chris@452: self.auth_config.add_section(sect) Chris@452: self.auth_config.set(sect, key, data) Chris@436: Chris@452: def remote_key(self, url, user): Chris@452: # generate a "safe-for-config-file" key representing uri+user Chris@459: # self.ui.write('generating remote_key for url %s and user %s\n' % (url, user)) Chris@452: s = '%s@@%s' % (url, user) Chris@452: h = hashlib.sha1() Chris@452: h.update(self.auth_key) Chris@452: h.update(s) Chris@452: hx = h.hexdigest() Chris@452: return hx Chris@452: Chris@452: def remote_user_key(self): Chris@453: return self.remote_key(self.pathless_url(), '') Chris@452: Chris@452: def remote_passwd_key(self): Chris@452: return self.remote_key(self.pathless_url(), self.user) Chris@452: Chris@452: def load_auth_data(self): Chris@452: Chris@452: self.load_config() Chris@452: if not self.auth_config: return Chris@452: Chris@452: self.remember = self.get_boolean_from_config( Chris@452: 'preferences', 'remember', False) Chris@452: Chris@452: if not self.user: Chris@452: d = self.get_from_config('user', self.remote_user_key()) Chris@452: if d: Chris@452: self.user = self.decrypt(d) Chris@452: Chris@452: if self.user: Chris@452: d = self.get_from_config('auth', self.remote_passwd_key()) Chris@452: if d: Chris@452: self.passwd = self.decrypt(d) Chris@452: Chris@452: def save_auth_data(self): Chris@452: Chris@453: self.load_config() Chris@452: if not self.auth_config: return Chris@453: Chris@452: self.set_to_config('preferences', 'remember', self.remember) Chris@452: Chris@459: # self.ui.write('aiming to store details for user %s\n' % self.user) Chris@452: Chris@452: if self.remember and self.user: Chris@452: d = self.encrypt(self.user) Chris@452: self.set_to_config('user', self.remote_user_key(), d) Chris@452: else: Chris@452: self.set_to_config('user', self.remote_user_key(), '') Chris@452: Chris@452: if self.remember and self.user and self.passwd: Chris@452: d = self.encrypt(self.passwd) Chris@452: self.set_to_config('auth', self.remote_passwd_key(), d) Chris@452: elif self.user: Chris@452: self.set_to_config('auth', self.remote_passwd_key(), '') Chris@452: Chris@452: self.save_config() Chris@452: Chris@452: class EasyHgAuthDialog(object): Chris@452: Chris@452: auth_store = None Chris@452: Chris@452: def __init__(self, ui, url, user, passwd): Chris@452: self.auth_store = EasyHgAuthStore(ui, url, user, passwd) Chris@452: Chris@470: def ask(self, repeat): Chris@459: Chris@460: if self.auth_store.user and self.auth_store.passwd and self.auth_store.remember: Chris@470: if not repeat: Chris@459: return (self.auth_store.user, self.auth_store.passwd) Chris@459: Chris@452: dialog = QtGui.QDialog() Chris@452: layout = QtGui.QGridLayout() Chris@452: dialog.setLayout(layout) Chris@452: Chris@470: heading = _('Login required') Chris@470: if repeat: Chris@470: heading = _('Login failed: please try again') Chris@470: label_text = _(('
Please provide your login details for the repository at%s
:') % (heading, self.auth_store.argless_url()))
Chris@470: layout.addWidget(QtGui.QLabel(label_text), 0, 0, 1, 2)
Chris@452:
Chris@452: user_field = QtGui.QLineEdit()
Chris@452: if self.auth_store.user: user_field.setText(self.auth_store.user)
Chris@452: layout.addWidget(QtGui.QLabel(_('User:')), 1, 0)
Chris@452: layout.addWidget(user_field, 1, 1)
Chris@452:
Chris@452: passwd_field = QtGui.QLineEdit()
Chris@452: passwd_field.setEchoMode(QtGui.QLineEdit.Password)
Chris@452: if self.auth_store.passwd: passwd_field.setText(self.auth_store.passwd)
Chris@452: layout.addWidget(QtGui.QLabel(_('Password:')), 2, 0)
Chris@452: layout.addWidget(passwd_field, 2, 1)
chris@656: user_field.textChanged.connect(passwd_field.clear)
Chris@452:
Chris@452: remember_field = None
Chris@452: if self.auth_store.use_auth_file:
Chris@452: remember_field = QtGui.QCheckBox()
Chris@452: remember_field.setChecked(self.auth_store.remember)
Chris@452: remember_field.setText(_('Remember these details while EasyMercurial is running'))
Chris@452: layout.addWidget(remember_field, 3, 1)
Chris@457: warning_field = QtGui.QLabel()
Chris@461: warning_field.setText(_('