1
# Copyright (C) 2007-2010 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
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.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Functions to manage the user's Launchpad user ID.
19
This allows the user to configure their Launchpad user ID once, rather
20
than once for each place that needs to take it into account.
23
from __future__ import absolute_import
30
from bzrlib.config import AuthenticationConfig, GlobalStack
31
from bzrlib.i18n import gettext
33
LAUNCHPAD_BASE = 'https://launchpad.net/'
36
class UnknownLaunchpadUsername(errors.BzrError):
37
_fmt = "The user name %(user)s is not registered on Launchpad."
40
class NoRegisteredSSHKeys(errors.BzrError):
41
_fmt = "The user %(user)s has not registered any SSH keys with Launchpad.\n" \
42
"See <https://launchpad.net/people/+me>"
45
class MismatchedUsernames(errors.BzrError):
47
_fmt = ('bazaar.conf and authentication.conf disagree about launchpad'
48
' account name. Please re-run launchpad-login.')
51
def get_lp_login(_config=None):
52
"""Return the user's Launchpad username.
54
:raises: MismatchedUsername if authentication.conf and bazaar.conf
55
disagree about username.
58
_config = GlobalStack()
60
username = _config.get('launchpad_username')
61
if username is not None:
62
auth = AuthenticationConfig()
63
auth_username = _get_auth_user(auth)
65
if auth_username is None:
66
trace.note(gettext('Setting ssh/sftp usernames for launchpad.net.'))
67
_set_auth_user(username, auth)
68
elif auth_username != username:
69
raise MismatchedUsernames()
73
def _set_global_option(username, _config=None):
75
_config = GlobalStack()
76
_config.set('launchpad_username', username)
79
def set_lp_login(username, _config=None):
80
"""Set the user's Launchpad username"""
81
_set_global_option(username, _config)
82
_set_auth_user(username)
85
def _get_auth_user(auth=None):
87
auth = AuthenticationConfig()
88
username = auth.get_user('ssh', '.launchpad.net')
91
def _set_auth_user(username, auth=None):
93
auth = AuthenticationConfig()
95
'Launchpad', '.launchpad.net', username, 'ssh')
98
def check_lp_login(username, _transport=None):
99
"""Check whether the given Launchpad username is okay.
101
This will check for both existence and whether the user has
104
if _transport is None:
105
_transport = transport.get_transport_from_url(LAUNCHPAD_BASE)
108
data = _transport.get_bytes('~%s/+sshkeys' % username)
109
except errors.NoSuchFile:
110
raise UnknownLaunchpadUsername(user=username)
113
raise NoRegisteredSSHKeys(user=username)