~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugins/launchpad/lp_registration.py

  • Committer: Ian Clatworthy
  • Date: 2007-11-30 04:28:32 UTC
  • mto: (3054.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 3055.
  • Revision ID: ian.clatworthy@internode.on.net-20071130042832-6prruj0kzg3fodm8
chapter 2 tweaks

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 by Canonical Ltd
 
1
# Copyright (C) 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
7
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
11
 
# # GNU General Public License for more details.
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
21
21
import urllib
22
22
import xmlrpclib
23
23
 
24
 
import bzrlib.config
25
 
import bzrlib.errors as errors
 
24
from bzrlib import (
 
25
    config,
 
26
    errors,
 
27
    __version__ as _bzrlib_version,
 
28
    )
26
29
 
27
30
# for testing, do
28
31
'''
53
56
            else:
54
57
                transport = xmlrpclib.Transport()
55
58
            transport.user_agent = 'bzr/%s (xmlrpclib/%s)' \
56
 
                    % (bzrlib.__version__, xmlrpclib.__version__)
 
59
                    % (_bzrlib_version, xmlrpclib.__version__)
57
60
        self.transport = transport
58
61
 
59
62
 
69
72
        else:
70
73
            return self.DEFAULT_SERVICE_URL
71
74
 
72
 
    def get_proxy(self):
 
75
    def get_proxy(self, authenticated):
73
76
        """Return the proxy for XMLRPC requests."""
74
 
        # auth info must be in url
75
 
        # TODO: if there's no registrant email perhaps we should just connect
76
 
        # anonymously?
77
 
        scheme, hostinfo, path = urlsplit(self.service_url)[:3]
78
 
        assert '@' not in hostinfo
79
 
        assert self.registrant_email is not None
80
 
        assert self.registrant_password is not None
81
 
        # TODO: perhaps fully quote the password to make it very slightly
82
 
        # obscured
83
 
        # TODO: can we perhaps add extra Authorization headers directly to the 
84
 
        # request, rather than putting this into the url?  perhaps a bit more 
85
 
        # secure against accidentally revealing it.  std66 s3.2.1 discourages putting
86
 
        # the password in the url.
87
 
        hostinfo = '%s:%s@%s' % (urllib.quote(self.registrant_email),
88
 
                                 urllib.quote(self.registrant_password),
89
 
                                 hostinfo)
90
 
        url = urlunsplit((scheme, hostinfo, path, '', ''))
 
77
        if authenticated:
 
78
            # auth info must be in url
 
79
            # TODO: if there's no registrant email perhaps we should
 
80
            # just connect anonymously?
 
81
            scheme, hostinfo, path = urlsplit(self.service_url)[:3]
 
82
            assert '@' not in hostinfo
 
83
            assert self.registrant_email is not None
 
84
            assert self.registrant_password is not None
 
85
            # TODO: perhaps fully quote the password to make it very slightly
 
86
            # obscured
 
87
            # TODO: can we perhaps add extra Authorization headers
 
88
            # directly to the request, rather than putting this into
 
89
            # the url?  perhaps a bit more secure against accidentally
 
90
            # revealing it.  std66 s3.2.1 discourages putting the
 
91
            # password in the url.
 
92
            hostinfo = '%s:%s@%s' % (urllib.quote(self.registrant_email),
 
93
                                     urllib.quote(self.registrant_password),
 
94
                                     hostinfo)
 
95
            url = urlunsplit((scheme, hostinfo, path, '', ''))
 
96
        else:
 
97
            url = self.service_url
91
98
        return xmlrpclib.ServerProxy(url, transport=self.transport)
92
99
 
93
100
    def gather_user_credentials(self):
94
101
        """Get the password from the user."""
95
 
        config = bzrlib.config.GlobalConfig()
96
 
        self.registrant_email = config.user_email()
 
102
        the_config = config.GlobalConfig()
 
103
        self.registrant_email = the_config.user_email()
97
104
        if self.registrant_password is None:
 
105
            auth = config.AuthenticationConfig()
 
106
            scheme, hostinfo = urlsplit(self.service_url)[:2]
98
107
            prompt = 'launchpad.net password for %s: ' % \
99
108
                    self.registrant_email
100
 
            self.registrant_password = getpass(prompt)
 
109
            # We will reuse http[s] credentials if we can, prompt user
 
110
            # otherwise
 
111
            self.registrant_password = auth.get_password(scheme, hostinfo,
 
112
                                                         self.registrant_email,
 
113
                                                         prompt=prompt)
101
114
 
102
 
    def send_request(self, method_name, method_params):
103
 
        proxy = self.get_proxy()
 
115
    def send_request(self, method_name, method_params, authenticated):
 
116
        proxy = self.get_proxy(authenticated)
104
117
        assert method_name
105
118
        method = getattr(proxy, method_name)
106
119
        try:
126
139
 
127
140
    # Set this to the XMLRPC method name.
128
141
    _methodname = None
 
142
    _authenticated = True
129
143
 
130
144
    def _request_params(self):
131
145
        """Return the arguments to pass to the method"""
137
151
        :param service: LaunchpadService indicating where to send
138
152
            the request and the authentication credentials.
139
153
        """
140
 
        return service.send_request(self._methodname, self._request_params())
 
154
        return service.send_request(self._methodname, self._request_params(),
 
155
                                    self._authenticated)
141
156
 
142
157
 
143
158
class DryRunLaunchpadService(LaunchpadService):
146
161
    The dummy service does not need authentication.
147
162
    """
148
163
 
149
 
    def send_request(self, method_name, method_params):
 
164
    def send_request(self, method_name, method_params, authenticated):
150
165
        pass
151
166
 
152
167
    def gather_user_credentials(self):
208
223
        # This must match the parameter tuple expected by Launchpad for this
209
224
        # method
210
225
        return (self.branch_url, self.bug_id, '')
 
226
 
 
227
 
 
228
class ResolveLaunchpadPathRequest(BaseRequest):
 
229
    """Request to resolve the path component of an lp: URL."""
 
230
 
 
231
    _methodname = 'resolve_lp_path'
 
232
    _authenticated = False
 
233
 
 
234
    def __init__(self, path):
 
235
        assert path
 
236
        self.path = path
 
237
 
 
238
    def _request_params(self):
 
239
        """Return xmlrpc request parameters"""
 
240
        return (self.path,)