/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/plugins/launchpad/account.py

  • Committer: Jelmer Vernooij
  • Date: 2020-05-24 00:39:50 UTC
  • mto: This revision was merged to the branch mainline in revision 7504.
  • Revision ID: jelmer@jelmer.uk-20200524003950-bbc545r76vc5yajg
Add github action.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007-2010 Canonical Ltd
 
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
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
 
 
23
from ... import (
 
24
    errors,
 
25
    trace,
 
26
    transport,
 
27
    )
 
28
from ...config import AuthenticationConfig, GlobalStack
 
29
from ...i18n import gettext
 
30
 
 
31
LAUNCHPAD_BASE = 'https://launchpad.net/'
 
32
 
 
33
 
 
34
class UnknownLaunchpadUsername(errors.BzrError):
 
35
    _fmt = "The user name %(user)s is not registered on Launchpad."
 
36
 
 
37
 
 
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>"
 
41
 
 
42
 
 
43
class MismatchedUsernames(errors.BzrError):
 
44
 
 
45
    _fmt = ('breezy.conf and authentication.conf disagree about launchpad'
 
46
            ' account name.  Please re-run launchpad-login.')
 
47
 
 
48
 
 
49
def get_lp_login(_config=None):
 
50
    """Return the user's Launchpad username.
 
51
 
 
52
    :raises: MismatchedUsername if authentication.conf and breezy.conf
 
53
        disagree about username.
 
54
    """
 
55
    if _config is None:
 
56
        _config = GlobalStack()
 
57
 
 
58
    username = _config.get('launchpad_username')
 
59
    if username is not None:
 
60
        auth = AuthenticationConfig()
 
61
        auth_username = _get_auth_user(auth)
 
62
        # Auto-upgrading
 
63
        if auth_username is None:
 
64
            trace.note(gettext('Setting ssh/sftp usernames for launchpad.net.'))
 
65
            _set_auth_user(username, auth)
 
66
        elif auth_username != username:
 
67
            raise MismatchedUsernames()
 
68
    return username
 
69
 
 
70
 
 
71
def _set_global_option(username, _config=None):
 
72
    if _config is None:
 
73
        _config = GlobalStack()
 
74
    if username is None:
 
75
        _config.remove('launchpad_username')
 
76
    else:
 
77
        _config.set('launchpad_username', username)
 
78
 
 
79
 
 
80
def set_lp_login(username, _config=None):
 
81
    """Set the user's Launchpad username"""
 
82
    _set_global_option(username, _config)
 
83
    if username is not None:
 
84
        _set_auth_user(username)
 
85
 
 
86
 
 
87
def _get_auth_user(auth=None):
 
88
    if auth is None:
 
89
        auth = AuthenticationConfig()
 
90
    username = auth.get_user('ssh', '.launchpad.net')
 
91
    return username
 
92
 
 
93
 
 
94
def _set_auth_user(username, auth=None):
 
95
    if auth is None:
 
96
        auth = AuthenticationConfig()
 
97
    auth.set_credentials(
 
98
        'Launchpad', '.launchpad.net', username, 'ssh')
 
99
 
 
100
 
 
101
def check_lp_login(username, _transport=None):
 
102
    """Check whether the given Launchpad username is okay.
 
103
 
 
104
    This will check for both existence and whether the user has
 
105
    uploaded SSH keys.
 
106
    """
 
107
    if _transport is None:
 
108
        _transport = transport.get_transport_from_url(LAUNCHPAD_BASE)
 
109
 
 
110
    try:
 
111
        data = _transport.get_bytes('~%s/+sshkeys' % username)
 
112
    except errors.NoSuchFile:
 
113
        raise UnknownLaunchpadUsername(user=username)
 
114
 
 
115
    if not data:
 
116
        raise NoRegisteredSSHKeys(user=username)