3834.1.1
by Martin Pool
Better message when you have no Launchpad SSH keys (#289148) |
1 |
# Copyright (C) 2007, 2008 Canonical Ltd
|
2898.3.5
by James Henstridge
Add copyright headers to new files |
2 |
#
|
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.
|
|
7 |
#
|
|
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.
|
|
12 |
#
|
|
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
|
|
4183.7.1
by Sabin Iacob
update FSF mailing address |
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2898.3.5
by James Henstridge
Add copyright headers to new files |
16 |
|
17 |
"""Functions to manage the user's Launchpad user ID.
|
|
18 |
||
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.
|
|
21 |
"""
|
|
22 |
||
3777.1.12
by Aaron Bentley
Enable configuring ssh auth from launchpad-login, with auto-upgrade. |
23 |
from bzrlib import errors, trace |
24 |
from bzrlib.config import AuthenticationConfig, GlobalConfig |
|
2898.3.1
by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used |
25 |
from bzrlib.transport import get_transport |
26 |
||
27 |
||
28 |
LAUNCHPAD_BASE = 'https://launchpad.net/' |
|
29 |
||
30 |
||
31 |
class UnknownLaunchpadUsername(errors.BzrError): |
|
32 |
_fmt = "The user name %(user)s is not registered on Launchpad." |
|
33 |
||
34 |
||
35 |
class NoRegisteredSSHKeys(errors.BzrError): |
|
3834.1.1
by Martin Pool
Better message when you have no Launchpad SSH keys (#289148) |
36 |
_fmt = "The user %(user)s has not registered any SSH keys with Launchpad.\n" \ |
37 |
"See <https://launchpad.net/people/+me>"
|
|
2898.3.1
by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used |
38 |
|
39 |
||
3777.1.12
by Aaron Bentley
Enable configuring ssh auth from launchpad-login, with auto-upgrade. |
40 |
class MismatchedUsernames(errors.BzrError): |
41 |
||
42 |
_fmt = ('bazaar.conf and authentication.conf disagree about launchpad' |
|
43 |
' account name. Please re-run launchpad-login.') |
|
44 |
||
45 |
||
2898.3.6
by James Henstridge
Rename function arguments that are only intended for use by the tests. |
46 |
def get_lp_login(_config=None): |
3777.1.15
by Aaron Bentley
Update docs |
47 |
"""Return the user's Launchpad username.
|
48 |
||
49 |
:raises: MismatchedUsername if authentication.conf and bazaar.conf
|
|
50 |
disagree about username.
|
|
51 |
"""
|
|
2898.3.6
by James Henstridge
Rename function arguments that are only intended for use by the tests. |
52 |
if _config is None: |
53 |
_config = GlobalConfig() |
|
54 |
||
3777.1.12
by Aaron Bentley
Enable configuring ssh auth from launchpad-login, with auto-upgrade. |
55 |
username = _config.get_user_option('launchpad_username') |
3777.1.18
by Aaron Bentley
Fix None handling wrt auth upgrades |
56 |
if username is not None: |
57 |
auth = AuthenticationConfig() |
|
3777.1.24
by Aaron Bentley
Use .launchpad.net instead of bazaar. and bazaar.staging. |
58 |
auth_username = _get_auth_user(auth) |
3777.1.18
by Aaron Bentley
Fix None handling wrt auth upgrades |
59 |
# Auto-upgrading
|
3777.1.24
by Aaron Bentley
Use .launchpad.net instead of bazaar. and bazaar.staging. |
60 |
if auth_username is None: |
3777.1.20
by Aaron Bentley
Include staging in launchpad-login authentication. |
61 |
trace.note('Setting ssh/sftp usernames for launchpad.net.') |
3777.1.18
by Aaron Bentley
Fix None handling wrt auth upgrades |
62 |
_set_auth_user(username, auth) |
3777.1.24
by Aaron Bentley
Use .launchpad.net instead of bazaar. and bazaar.staging. |
63 |
elif auth_username != username: |
64 |
raise MismatchedUsernames() |
|
3777.1.12
by Aaron Bentley
Enable configuring ssh auth from launchpad-login, with auto-upgrade. |
65 |
return username |
66 |
||
67 |
||
68 |
def _set_global_option(username, _config=None): |
|
69 |
if _config is None: |
|
70 |
_config = GlobalConfig() |
|
71 |
_config.set_user_option('launchpad_username', username) |
|
2898.3.6
by James Henstridge
Rename function arguments that are only intended for use by the tests. |
72 |
|
73 |
||
74 |
def set_lp_login(username, _config=None): |
|
2898.3.1
by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used |
75 |
"""Set the user's Launchpad username"""
|
3777.1.12
by Aaron Bentley
Enable configuring ssh auth from launchpad-login, with auto-upgrade. |
76 |
_set_global_option(username, _config) |
77 |
_set_auth_user(username) |
|
78 |
||
79 |
||
80 |
def _get_auth_user(auth=None): |
|
81 |
if auth is None: |
|
82 |
auth = AuthenticationConfig() |
|
4304.2.1
by Vincent Ladeuil
Fix bug #367726 by reverting some default user handling introduced |
83 |
username = auth.get_user('ssh', '.launchpad.net') |
4222.3.15
by Jelmer Vernooij
Fix launchpad username prompting in a similar way to smtp. |
84 |
return username |
3777.1.12
by Aaron Bentley
Enable configuring ssh auth from launchpad-login, with auto-upgrade. |
85 |
|
86 |
def _set_auth_user(username, auth=None): |
|
87 |
if auth is None: |
|
88 |
auth = AuthenticationConfig() |
|
89 |
auth.set_credentials( |
|
3777.1.24
by Aaron Bentley
Use .launchpad.net instead of bazaar. and bazaar.staging. |
90 |
'Launchpad', '.launchpad.net', username, 'ssh') |
2898.3.6
by James Henstridge
Rename function arguments that are only intended for use by the tests. |
91 |
|
92 |
||
93 |
def check_lp_login(username, _transport=None): |
|
2898.3.1
by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used |
94 |
"""Check whether the given Launchpad username is okay.
|
95 |
||
2934.1.1
by Ian Clatworthy
(James Henstridge) add a command for managing the Launchpad user ID |
96 |
This will check for both existence and whether the user has
|
2898.3.1
by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used |
97 |
uploaded SSH keys.
|
98 |
"""
|
|
2898.3.6
by James Henstridge
Rename function arguments that are only intended for use by the tests. |
99 |
if _transport is None: |
100 |
_transport = get_transport(LAUNCHPAD_BASE) |
|
2898.3.1
by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used |
101 |
|
102 |
try: |
|
2898.3.6
by James Henstridge
Rename function arguments that are only intended for use by the tests. |
103 |
data = _transport.get_bytes('~%s/+sshkeys' % username) |
2898.3.1
by James Henstridge
API for storing and retrieving the Launchpad user ID that will be used |
104 |
except errors.NoSuchFile: |
105 |
raise UnknownLaunchpadUsername(user=username) |
|
106 |
||
107 |
if not data: |
|
108 |
raise NoRegisteredSSHKeys(user=username) |