# HG changeset patch # User Marcin Kasperski # Date 2015-11-20 20:56:14 # Node ID bfd781fe0e7b5e2944dbb651d3828023ac63b1f6 # Parent 72e0fbdb178a109f4e004086a3f9d4eda8dc36a2 Partial bugfix for ignoring usernames specified in url (not yet finished). Normalized debug/status texts to start with 'keyring: ' (part started so, part from 'mercurial_keyring: ') diff --git a/mercurial_keyring.py b/mercurial_keyring.py --- a/mercurial_keyring.py +++ b/mercurial_keyring.py @@ -58,7 +58,6 @@ from mercurial import mail from mercurial.mail import SMTPS, STARTTLS from mercurial import encoding -from urlparse import urlparse import urllib2 import smtplib import socket @@ -298,6 +297,7 @@ class HTTPPasswordHandler(object): SRC_URL = "repository URL" SRC_CFGAUTH = "hgrc" SRC_MEMCACHE = "temporary cache" + SRC_URLCACHE = "urllib temporary cache" SRC_KEYRING = "keyring" def get_credentials(self, pwmgr, realm, authuri, skip_caches=False): @@ -317,22 +317,27 @@ class HTTPPasswordHandler(object): """ ui = pwmgr.ui - # Strip arguments to get actual remote repository url. - base_url = self.canonical_url(authuri) + base_url, url_user, url_passwd = self.unpack_url(authuri) + ui.debug(_('keyring: base url: %s, url user: %s, url pwd: %s\n') % + (base_url, url_user or '', url_passwd and '******' or '')) + + # Extract username (or password) stored directly in url + if url_user and url_passwd: + return url_user, url_passwd, self.SRC_URL, base_url - # Extract username (or password) stored directly in url - url_user, url_pwd \ + # Extract data from urllib (in case it was already stored) + urllib_user, urllib_pwd \ = urllib2.HTTPPasswordMgrWithDefaultRealm.find_user_password( pwmgr, realm, authuri) - if url_user and url_pwd: - return url_user, url_pwd, self.SRC_URL, base_url + if urllib_user and urllib_pwd: + return urllib_user, urllib_pwd, self.SRC_URLCACHE, base_url # Consult configuration to normalize url to prefix, and find username # (and maybe password) auth_user, auth_pwd, keyring_url = self.get_url_config( ui, base_url, url_user) if auth_user and url_user and (url_user != auth_user): - raise util.Abort(_('mercurial_keyring: username for %s specified both in repository path (%s) and in .hg/hgrc/[auth] (%s). Please, leave only one of those' % (base_url, url_user, auth_user))) + raise util.Abort(_('keyring: username for %s specified both in repository path (%s) and in .hg/hgrc/[auth] (%s). Please, leave only one of those' % (base_url, url_user, auth_user))) if auth_user and auth_pwd: return auth_user, auth_pwd, self.SRC_CFGAUTH, keyring_url @@ -354,12 +359,11 @@ class HTTPPasswordHandler(object): return auth_user, None, None, keyring_url - @staticmethod def prompt_interactively(ui, user, realm, url): """Actual interactive prompt""" if not ui.interactive(): - raise util.Abort(_('mercurial_keyring: http authorization required but program used in non-interactive mode')) + raise util.Abort(_('keyring: http authorization required but program used in non-interactive mode')) if not user: ui.status(_("keyring: username not specified in hgrc (or in url). Password will not be saved.\n")) @@ -482,16 +486,29 @@ class HTTPPasswordHandler(object): return password_url @staticmethod - def canonical_url(authuri): + def unpack_url(authuri): """ - Strips query params from url. Used to convert urls like + Does two things: + + 1. Strips query params from url. Used to convert urls like https://repo.machine.com/repos/apps/module?pairs=0000000000000000000000000000000000000000-0000000000000000000000000000000000000000&cmd=between to https://repo.machine.com/repos/apps/module + + 2. Extracts username and password, if present. + + Returns url, user, password """ - parsed_url = urlparse(authuri) - return "%s://%s%s" % (parsed_url.scheme, parsed_url.netloc, - parsed_url.path) + # mercurial.util.url, rather handy url parser + parsed_url = util.url(authuri) + parsed_url.query = '' + parsed_url.fragment = '' + # Strip arguments to get actual remote repository url. + # base_url = "%s://%s%s" % (parsed_url.scheme, parsed_url.netloc, + # parsed_url.path) + + return str(parsed_url), parsed_url.user, parsed_url.passwd + ############################################################ # Mercurial monkey-patching