/breezy-svn/trunk

To get this branch, use:
bzr branch https://code.breezy-vcs.org/breezy-svn/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# Copyright (C) 2005-2009 Jelmer Vernooij <jelmer@samba.org>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


"""Authentication token retrieval."""

from __future__ import absolute_import

import subvertpy
from subvertpy import (
    ra,
    )
import urlparse
import urllib

from breezy.config import (
    AuthenticationConfig,
    CredentialStore,
    )
from breezy.sixish import (
    text_type,
    )
from breezy.trace import (
    mutter,
    )
from breezy.ui import (
    ui_factory,
    )


class SubversionAuthenticationConfig(AuthenticationConfig):
    """Simple extended version of AuthenticationConfig that can provide
    the information Subversion requires.
    """
    def __init__(self, scheme, host, port, path, file=None):
        super(SubversionAuthenticationConfig, self).__init__(file)
        if scheme.startswith('svn+http'):
            self.scheme = scheme[len("svn+"):]
        else:
            self.scheme = scheme
        self.host = host
        self.port = port
        self.path = path

    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.
        """
        mutter("Obtaining username for SVN connection")
        username = self.get_user(self.scheme, host=self.host, path=self.path,
                                 realm=realm, ask=True)
        return (username.encode('utf-8'), False)

    def get_svn_simple(self, realm, username, may_save):
        """Look up a Subversion user name+password combination in the Bazaar
        authentication cache.

        :param realm: Authentication realm (optional)
        :param username: Username, if it is already known, or None.
        :param may_save: Whether or not the username should be saved.
        """
        mutter("Obtaining username and password for SVN connection %r"
               "(username: %r)", realm, username)
        if username is None:
            username = self.get_user(self.scheme,
                    host=self.host, path=self.path, realm=realm, ask=True)
        password = self.get_password(self.scheme, host=self.host,
            path=self.path, user=username,
            realm=realm, prompt=u"%s %s password" % (realm, username))
        if isinstance(password, text_type):
            password = password.encode('utf-8')
        return (username.encode("utf-8"), password, False)

    def get_svn_ssl_server_trust(self, realm, failures, cert_info, may_save):
        """Return a Subversion auth provider that verifies SSL server trust.

        :param realm: Realm name (optional)
        :param failures: Failures to check for (bit field, SVN_AUTH_SSL_*)
        :param cert_info: Certificate information
        :param may_save: Whether this information may be stored.
        """
        mutter("Verifying SSL server: %s", realm)
        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):
            accepted_failures = (
                    subvertpy.SSL_NOTYETVALID +
                    subvertpy.SSL_EXPIRED +
                    subvertpy.SSL_CNMISMATCH +
                    subvertpy.SSL_UNKNOWNCA +
                    subvertpy.SSL_OTHER)
        else:
            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
        accepted by svn_auth_open().

        :param retries: Number of allowed retries.
        """
        return ra.get_username_prompt_provider(self.get_svn_username,
                                                     retries)

    def get_svn_simple_prompt_provider(self, retries):
        """Return a Subversion auth provider for retrieving a
        username+password combination, as accepted by svn_auth_open().

        :param retries: Number of allowed retries.
        """
        return ra.get_simple_prompt_provider(self.get_svn_simple, retries)

    def get_svn_ssl_server_trust_prompt_provider(self):
        """Return a Subversion auth provider for checking
        whether a SSL server is trusted."""
        return ra.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.
        """
        return [self.get_svn_username_prompt_provider(1),
                self.get_svn_simple_prompt_provider(1),
                self.get_svn_ssl_server_trust_prompt_provider()]


def get_ssl_client_cert_pw(realm, may_save):
    """Simple SSL client certificate password prompter.

    :param realm: Realm, optional.
    :param may_save: Whether the password can be cached.
    """
    password = ui_factory.get_password(
            "Please enter password for client certificate[realm=%s]" % realm)
    return (password, False)


def get_ssl_client_cert_pw_provider(tries):
    return ra.get_ssl_client_cert_pw_prompt_provider(
                get_ssl_client_cert_pw, tries)


def get_stock_svn_providers():
    providers = [ra.get_simple_provider(),
            ra.get_username_provider(),
            ra.get_ssl_client_cert_file_provider(),
            ra.get_ssl_client_cert_pw_file_provider(),
            ra.get_ssl_server_trust_file_provider(),
            ]

    if getattr(ra, 'get_windows_simple_provider', None):
        providers.append(ra.get_windows_simple_provider())

    if getattr(ra, 'get_keychain_simple_provider', None):
        providers.append(ra.get_keychain_simple_provider())

    if getattr(ra, 'get_windows_ssl_server_trust_provider', None):
        providers.append(ra.get_windows_ssl_server_trust_provider())

    return providers


def create_auth_baton(url):
    """Create an authentication baton for the specified URL.

    :param url: URL to create auth baton for.
    """
    (scheme, netloc, path, _, _) = urlparse.urlsplit(url)
    (creds, host) = urllib.splituser(netloc)
    (host, port) = urllib.splitport(host)

    auth_config = SubversionAuthenticationConfig(scheme, host, port, path)

    # Specify Subversion providers first, because they use file data
    # rather than prompting the user.
    providers = []
    providers += get_stock_svn_providers()
    providers += auth_config.get_svn_auth_providers()
    providers += [get_ssl_client_cert_pw_provider(1)]

    auth_baton = ra.Auth(providers)
    if creds is not None:
        (user, password) = urllib.splitpasswd(creds)
        if user is not None:
            auth_baton.set_parameter(subvertpy.AUTH_PARAM_DEFAULT_USERNAME, user)
        if password is not None:
            auth_baton.set_parameter(subvertpy.AUTH_PARAM_DEFAULT_PASSWORD, password)
    return auth_baton


class SubversionCredentialStore(CredentialStore):
    """Credentials provider that reads ~/.subversion/auth/"""

    def __init__(self):
        super(SubversionCredentialStore, self).__init__()
        self.auth = ra.Auth([ra.get_simple_provider()])

    def _get_realm(self, credentials):
        if credentials.get('port') is None:
            import socket
            try:
                credentials['port'] = socket.getservbyname(credentials['scheme'])
            except socket.error:
                mutter("Unable to look up default port for %(scheme)s" %
                       credentials)
                return None
        return "<%(scheme)s://%(host)s:%(port)s> %(realm)s" % credentials

    def decode_password(self, credentials):
        svn_realm = self._get_realm(credentials)
        if svn_realm is None:
            return None
        creds = self.auth.credentials("svn.simple", svn_realm)
        try:
            (username, password, may_save) = creds.next()
        except StopIteration:
            return None
        assert type(password) == str
        if credentials.get("user") not in (username, None):
            # Subversion changed the username
            return None
        return password

    def get_credentials(self, scheme, host, port=None, user=None, path=None,
                        realm=None):
        assert isinstance(realm, str) or realm is None
        credentials = { "scheme": scheme, "host": host, "port": port,
            "realm": realm, "user": user}
        svn_realm = self._get_realm(credentials)
        if svn_realm is None:
            return None
        creds = self.auth.credentials("svn.simple", svn_realm)
        try:
            (username, password, may_save) = creds.next()
        except StopIteration:
            return None
        credentials['user'] = username
        credentials['password'] = password
        return credentials