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.
28
from bzrlib.config import AuthenticationConfig, GlobalConfig
31
LAUNCHPAD_BASE = 'https://launchpad.net/'
34
class UnknownLaunchpadUsername(errors.BzrError):
35
_fmt = "The user name %(user)s is not registered on Launchpad."
38
class NoRegisteredSSHKeys(errors.BzrError):
39
_fmt = "The user %(user)s has not registered any SSH keys with Launchpad.\n" \
40
"See <https://launchpad.net/people/+me>"
43
class MismatchedUsernames(errors.BzrError):
45
_fmt = ('bazaar.conf and authentication.conf disagree about launchpad'
46
' account name. Please re-run launchpad-login.')
49
def get_lp_login(_config=None):
50
"""Return the user's Launchpad username.
52
:raises: MismatchedUsername if authentication.conf and bazaar.conf
53
disagree about username.
56
_config = GlobalConfig()
58
username = _config.get_user_option('launchpad_username')
59
if username is not None:
60
auth = AuthenticationConfig()
61
auth_username = _get_auth_user(auth)
63
if auth_username is None:
64
trace.note('Setting ssh/sftp usernames for launchpad.net.')
65
_set_auth_user(username, auth)
66
elif auth_username != username:
67
raise MismatchedUsernames()
71
def _set_global_option(username, _config=None):
73
_config = GlobalConfig()
74
_config.set_user_option('launchpad_username', username)
77
def set_lp_login(username, _config=None):
78
"""Set the user's Launchpad username"""
79
_set_global_option(username, _config)
80
_set_auth_user(username)
83
def _get_auth_user(auth=None):
85
auth = AuthenticationConfig()
86
username = auth.get_user('ssh', '.launchpad.net')
89
def _set_auth_user(username, auth=None):
91
auth = AuthenticationConfig()
93
'Launchpad', '.launchpad.net', username, 'ssh')
96
def check_lp_login(username, _transport=None):
97
"""Check whether the given Launchpad username is okay.
99
This will check for both existence and whether the user has
102
if _transport is None:
103
_transport = transport.get_transport_from_url(LAUNCHPAD_BASE)
106
data = _transport.get_bytes('~%s/+sshkeys' % username)
107
except errors.NoSuchFile:
108
raise UnknownLaunchpadUsername(user=username)
111
raise NoRegisteredSSHKeys(user=username)