~bzr-pqm/bzr/bzr.dev

5752.3.8 by John Arbash Meinel
Merge bzr.dev 5764 to resolve release-notes (aka NEWS) conflicts
1
# Copyright (C) 2008-2011 Canonical Ltd
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
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
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
16
6379.6.1 by Jelmer Vernooij
Import absolute_import in a few places.
17
from __future__ import absolute_import
18
5131.2.2 by Martin
Catch a couple of missed plugin module docstrings, note need for assignment to __doc__ in developer documentation and NEWS
19
__doc__ = """Use ~/.netrc as a credential store for authentication.conf."""
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
20
4283.3.1 by Vincent Ladeuil
Make built-in plugins display the same version than bzrlib.
21
# Since we are a built-in plugin we share the bzrlib version
22
from bzrlib import version_info
23
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
24
from bzrlib import (
25
    config,
26
    lazy_import,
27
    )
28
29
lazy_import.lazy_import(globals(), """
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
30
import errno
31
import netrc
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
32
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
33
from bzrlib import (
34
    errors,
35
    )
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
36
""")
37
38
39
class NetrcCredentialStore(config.CredentialStore):
40
41
    def __init__(self):
42
        super(NetrcCredentialStore, self).__init__()
43
        try:
44
            self._netrc = netrc.netrc()
45
        except IOError, e:
46
            if e.args[0] == errno.ENOENT:
47
                raise errors.NoSuchFile(e.filename)
48
            else:
49
                raise
50
51
    def decode_password(self, credentials):
52
        auth = self._netrc.authenticators(credentials['host'])
53
        password = None
54
        if auth is not None:
55
            user, account, password = auth
56
            cred_user = credentials.get('user', None)
57
            if cred_user is None or user != cred_user:
3757.3.3 by Vincent Ladeuil
Aaron's feedback.
58
                # We don't use the netrc ability to provide a user since there
59
                # is no way to give it back to AuthConfig. So if the user
60
                # doesn't match, we don't return a password.
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
61
                password = None
62
        return password
63
64
65
config.credential_store_registry.register_lazy(
66
    'netrc', __name__, 'NetrcCredentialStore', help=__doc__)
67
68
69
def load_tests(basic_tests, module, loader):
70
    testmod_names = [
71
        'tests',
72
        ]
73
    basic_tests.addTest(loader.loadTestsFromModuleNames(
74
            ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
75
    return basic_tests