Merge 0.4.
[jelmer/subvertpy.git] / auth.py
diff --git a/auth.py b/auth.py
index b99c1d5a2048327c88b5d3b1d177b07418b99a1b..5887199abe9c840d1a8b55e2e0c705be56942a7d 100644 (file)
--- a/auth.py
+++ b/auth.py
 
 from bzrlib.config import AuthenticationConfig
 from bzrlib.ui import ui_factory
-
 from bzrlib.plugins.svn.ra import (get_username_prompt_provider, 
                                    get_simple_prompt_provider,
                                    get_ssl_server_trust_prompt_provider,
-                                   get_ssl_client_cert_pw_prompt_provider)
-
-import svn.core
-from svn.core import (svn_auth_cred_username_t, 
-                      svn_auth_cred_simple_t,
-                      svn_auth_cred_ssl_client_cert_t,
-                      svn_auth_cred_ssl_client_cert_pw_t,
-                      svn_auth_cred_ssl_server_trust_t,
-                      svn_auth_open)
+                                   get_ssl_client_cert_pw_prompt_provider,
+                                   get_simple_provider, get_username_provider, 
+                                   get_ssl_client_cert_file_provider, 
+                                   get_ssl_client_cert_pw_file_provider,
+                                   get_ssl_server_trust_file_provider,
+                                   Auth
+                                   )
+from bzrlib.plugins.svn import ra
 import urlparse
 import urllib
 
+AUTH_PARAM_DEFAULT_USERNAME = 'svn:auth:username'
+AUTH_PARAM_DEFAULT_PASSWORD = 'svn:auth:password'
+
 SSL_NOTYETVALID = 0x00000001
 SSL_EXPIRED     = 0x00000002
 SSL_CNMISMATCH  = 0x00000004
@@ -50,20 +51,14 @@ class SubversionAuthenticationConfig(AuthenticationConfig):
         self.port = port
         self.path = path
        
-    def get_svn_username(self, realm, may_save, pool=None):
+    def get_svn_username(self, realm, may_save):
         """Look up a Subversion user name in the Bazaar authentication cache.
 
         :param realm: Authentication realm (optional)
         :param may_save: Whether or not the username should be saved.
-        :param pool: Allocation pool, is ignored.
-        :param default: Assumed username
         """
-        username_cred = svn_auth_cred_username_t()
-        username_cred.username = self.get_user(self.scheme, 
-                host=self.host, path=self.path, 
-                realm=realm)
-        username_cred.may_save = False
-        return username_cred
+        username = self.get_user(self.scheme, host=self.host, path=self.path, realm=realm)
+        return (username, False)
 
     def get_svn_simple(self, realm, username, may_save, pool):
         """Look up a Subversion user name+password combination in the Bazaar 
@@ -74,14 +69,12 @@ class SubversionAuthenticationConfig(AuthenticationConfig):
         :param may_save: Whether or not the username should be saved.
         :param pool: Allocation pool, is ignored.
         """
-        simple_cred = svn_auth_cred_simple_t()
-        simple_cred.username = self.get_user(self.scheme, 
+        username = self.get_user(self.scheme, 
                 host=self.host, path=self.path, realm=realm) or username
-        simple_cred.password = self.get_password(self.scheme, host=self.host, 
+        password = self.get_password(self.scheme, host=self.host, 
             path=self.path, user=simple_cred.username, 
             realm=realm, prompt="%s %s password" % (realm, simple_cred.username))
-        simple_cred.may_save = False
-        return simple_cred
+        return (username, password, False)
 
     def get_svn_ssl_server_trust(self, realm, failures, cert_info, may_save, 
                                      pool):
@@ -92,21 +85,19 @@ class SubversionAuthenticationConfig(AuthenticationConfig):
         :param cert_info: Certificate information
         :param may_save: Whether this information may be stored.
         """
-        ssl_server_trust = svn_auth_cred_ssl_server_trust_t()
         credentials = self.get_credentials(self.scheme, host=self.host)
         if (credentials is not None and 
             credentials.has_key("verify_certificates") and 
             credentials["verify_certificates"] == False):
-            ssl_server_trust.accepted_failures = (
+            accepted_failures = (
                     SSL_NOTYETVALID + 
                     SSL_EXPIRED +
                     SSL_CNMISMATCH +
                     SSL_UNKNOWNCA +
                     SSL_OTHER)
         else:
-            ssl_server_trust.accepted_failures = 0
-        ssl_server_trust.may_save = False
-        return ssl_server_trust
+            accepted_failures = 0
+        return (accepted_failures, False)
 
     def get_svn_username_prompt_provider(self, retries):
         """Return a Subversion auth provider for retrieving the username, as 
@@ -128,7 +119,8 @@ class SubversionAuthenticationConfig(AuthenticationConfig):
     def get_svn_ssl_server_trust_prompt_provider(self):
         """Return a Subversion auth provider for checking 
         whether a SSL server is trusted."""
-        return get_ssl_server_trust_prompt_provider(self.get_svn_ssl_server_trust)
+        return get_ssl_server_trust_prompt_provider(
+                    self.get_svn_ssl_server_trust)
 
     def get_svn_auth_providers(self):
         """Return a list of auth providers for this authentication file.
@@ -143,34 +135,31 @@ def get_ssl_client_cert_pw(realm, may_save, pool):
     :param realm: Realm, optional.
     :param may_save: Whether the password can be cached.
     """
-    ssl_cred_pw = svn_auth_cred_ssl_client_cert_pw_t()
-    ssl_cred_pw.password = ui_factory.get_password(
+    password = ui_factory.get_password(
             "Please enter password for client certificate[realm=%s]" % realm)
-    ssl_cred_pw.may_save = False
-    return ssl_cred_pw
+    return (password, False)
 
 
 def get_ssl_client_cert_pw_provider(tries):
     return get_ssl_client_cert_pw_prompt_provider(
                 get_ssl_client_cert_pw, tries)
 
-
 def get_stock_svn_providers():
-    providers = [svn.client.get_simple_provider(),
-            svn.client.get_username_provider(),
-            svn.client.get_ssl_client_cert_file_provider(),
-            svn.client.get_ssl_client_cert_pw_file_provider(),
-            svn.client.get_ssl_server_trust_file_provider(),
+    providers = [get_simple_provider(),
+            get_username_provider(),
+            get_ssl_client_cert_file_provider(),
+            get_ssl_client_cert_pw_file_provider(),
+            get_ssl_server_trust_file_provider(),
             ]
 
-    if hasattr(svn.client, 'get_windows_simple_provider'):
-        providers.append(svn.client.get_windows_simple_provider())
+    if hasattr(ra, 'get_windows_simple_provider'):
+        providers.append(ra.get_windows_simple_provider())
 
-    if hasattr(svn.client, 'get_keychain_simple_provider'):
-        providers.append(svn.client.get_keychain_simple_provider())
+    if hasattr(ra, 'get_keychain_simple_provider'):
+        providers.append(ra.get_keychain_simple_provider())
 
-    if hasattr(svn.client, 'get_windows_ssl_server_trust_provider'):
-        providers.append(svn.client.get_windows_ssl_server_trust_provider())
+    if hasattr(ra, 'get_windows_ssl_server_trust_provider'):
+        providers.append(ra.get_windows_ssl_server_trust_provider())
 
     return providers
 
@@ -188,17 +177,16 @@ def create_auth_baton(url):
     # rather than prompting the user.
     providers = get_stock_svn_providers()
 
-    if svn.core.SVN_VER_MAJOR == 1 and svn.core.SVN_VER_MINOR >= 5:
+    (major, minor, patch, tag) = ra.version()
+    if major == 1 and minor >= 5:
         providers += auth_config.get_svn_auth_providers()
         providers += [get_ssl_client_cert_pw_provider(1)]
 
-    auth_baton = svn.core.svn_auth_open(providers)
+    auth_baton = Auth(providers)
     if creds is not None:
-        (auth_baton.user, auth_baton.password) = urllib.splitpasswd(creds)
-        if auth_baton.user is not None:
-            svn.core.svn_auth_set_parameter(auth_baton, 
-                svn.core.SVN_AUTH_PARAM_DEFAULT_USERNAME, auth_baton.user)
-        if auth_baton.password is not None:
-            svn.core.svn_auth_set_parameter(auth_baton, 
-                svn.core.SVN_AUTH_PARAM_DEFAULT_PASSWORD, auth_baton.password)
+        (user, password) = urllib.splitpasswd(creds)
+        if user is not None:
+            auth_baton.set_parameter(AUTH_PARAM_DEFAULT_USERNAME, user)
+        if password is not None:
+            auth_baton.set_parameter(AUTH_PARAM_DEFAULT_PASSWORD, password)
     return auth_baton