# HG changeset patch # User Mitsuhiro Koga # Date 1351012755 -32400 # Node ID b748a0eed09aff62fc1a55388a78d253652947fc # Parent 27fec8bf9273c5fe0384b67ef487d8d8686806d8 svnwrap: Refactor the svn_auth_ssl_server_trust_prompt to add other handler - Since there are five handler, change to SubversionPrompt class from svn_auth_ssl_server_trust_prompt(). - Moved out of the handler to check the callback function is non-None. diff --git a/hgsubversion/svnrepo.py b/hgsubversion/svnrepo.py --- a/hgsubversion/svnrepo.py +++ b/hgsubversion/svnrepo.py @@ -195,11 +195,14 @@ def instance(ui, url, create): if create: raise hgutil.Abort('cannot create new remote Subversion repository') - svnwrap.ssl_server_trust_prompt_callback(svn_auth_ssl_server_trust_prompt(ui)) + svnwrap.prompt_callback(SubversionPrompt(ui)) return svnremoterepo(ui, url) -def svn_auth_ssl_server_trust_prompt(ui): - def callback(realm, failures, cert_info, may_save, pool=None): +class SubversionPrompt(object): + def __init__(self, ui): + self.ui = ui + + def ssl_server_trust(self, realm, failures, cert_info, may_save, pool=None): msg = 'Error validating server certificate for \'%s\':\n' % (realm,) if failures & svnwrap.SSL_UNKNOWNCA: msg += ( @@ -233,14 +236,12 @@ def svn_auth_ssl_server_trust_prompt(ui) else: msg += '(R)eject or accept (t)emporarily? ' choices = (('&Reject'), ('&Temporarily')) - choice = ui.promptchoice(msg, choices, default=0) + choice = self.ui.promptchoice(msg, choices, default=0) if choice == 1: creds = (failures, False) elif may_save and choice == 2: creds = (failures, True) else: creds = None - return creds - return callback diff --git a/hgsubversion/svnwrap/subvertpy_wrapper.py b/hgsubversion/svnwrap/subvertpy_wrapper.py --- a/hgsubversion/svnwrap/subvertpy_wrapper.py +++ b/hgsubversion/svnwrap/subvertpy_wrapper.py @@ -52,11 +52,6 @@ def version(): svnvers += '-' + subversion_version[3] return (svnvers, 'Subvertpy ' + _versionstr(subvertpy.__version__)) -_ssl_server_trust_prompt_callback = None -def ssl_server_trust_prompt_callback(callback): - global _ssl_server_trust_prompt_callback - _ssl_server_trust_prompt_callback = callback - # exported values ERR_FS_CONFLICT = subvertpy.ERR_FS_CONFLICT ERR_FS_NOT_FOUND = subvertpy.ERR_FS_NOT_FOUND @@ -83,6 +78,11 @@ def ieditor(fn): return fn +_prompt = None +def prompt_callback(callback): + global _prompt + _prompt = callback + _svntypes = { subvertpy.NODE_DIR: 'd', subvertpy.NODE_FILE: 'f', @@ -212,21 +212,15 @@ class SubversionRepo(object): return self.username or username, self.password or '', False def getuser(realm, may_save): return self.username or '', False - def svn_auth_ssl_server_trust_prompt(realm, failures, cert_info, may_save): - global _ssl_server_trust_prompt_callback - if _ssl_server_trust_prompt_callback: - ret = _ssl_server_trust_prompt_callback(realm, failures, cert_info, may_save) - if ret: - creds = ret - else: - # We need to reject the certificate, but subvertpy doesn't - # handle None as a return value here, and requires - # we instead return a tuple of (int, bool). Because of that, - # we return (0, False) instead. - creds = (0, False) - else: - creds = (0, False) + def ssl_server_trust(realm, failures, cert_info, may_save): + creds = _prompt.ssl_server_trust(realm, failures, cert_info, may_save) + if creds is None: + # We need to reject the certificate, but subvertpy doesn't + # handle None as a return value here, and requires + # we instead return a tuple of (int, bool). Because of that, + # we return (0, False) instead. + creds = (0, False) return creds providers = ra.get_platform_specific_client_providers() @@ -238,8 +232,11 @@ class SubversionRepo(object): ra.get_ssl_server_trust_file_provider(), ra.get_username_prompt_provider(getuser, 0), ra.get_simple_prompt_provider(getpass, 0), - ra.get_ssl_server_trust_prompt_provider(svn_auth_ssl_server_trust_prompt), ] + if _prompt: + providers += [ + ra.get_ssl_server_trust_prompt_provider(ssl_server_trust), + ] auth = ra.Auth(providers) if self.username: diff --git a/hgsubversion/svnwrap/svn_swig_wrapper.py b/hgsubversion/svnwrap/svn_swig_wrapper.py --- a/hgsubversion/svnwrap/svn_swig_wrapper.py +++ b/hgsubversion/svnwrap/svn_swig_wrapper.py @@ -108,27 +108,23 @@ def user_pass_prompt(realm, default_user creds.password = getpass.getpass('Password for %s: ' % creds.username) return creds -_ssl_server_trust_prompt_callback = None -def ssl_server_trust_prompt_callback(callback): - global _ssl_server_trust_prompt_callback - _ssl_server_trust_prompt_callback = callback - -def _ssl_server_trust_prompt(realm, failures, cert_info, may_save, pool): - global _ssl_server_trust_prompt_callback - if _ssl_server_trust_prompt_callback: - cert = [ - cert_info.hostname, - cert_info.fingerprint, - cert_info.valid_from, - cert_info.valid_until, - cert_info.issuer_dname, - ] - ret = _ssl_server_trust_prompt_callback(realm, failures, cert, may_save, pool) - if ret: - creds = core.svn_auth_cred_ssl_server_trust_t() - (creds.accepted_failures, creds.may_save) = ret - else: - creds = None +_prompt = None +def prompt_callback(callback): + global _prompt + _prompt = callback + +def _ssl_server_trust(realm, failures, cert_info, may_save, pool): + cert = [ + cert_info.hostname, + cert_info.fingerprint, + cert_info.valid_from, + cert_info.valid_until, + cert_info.issuer_dname, + ] + ret = _prompt.ssl_server_trust(realm, failures, cert, may_save, pool) + if ret: + creds = core.svn_auth_cred_ssl_server_trust_t() + (creds.accepted_failures, creds.may_save) = ret else: creds = None return creds @@ -176,9 +172,13 @@ def _create_auth_baton(pool, password_st client.get_ssl_client_cert_pw_file_provider(), client.get_ssl_server_trust_file_provider(), client.get_simple_prompt_provider(user_pass_prompt, 2), - client.get_ssl_server_trust_prompt_provider(_ssl_server_trust_prompt), ] + if _prompt: + providers += [ + client.get_ssl_server_trust_prompt_provider(_ssl_server_trust), + ] + return core.svn_auth_open(providers, pool) _svntypes = {