~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugins/netrc_credential_store/__init__.py

  • Committer: Vincent Ladeuil
  • Date: 2008-10-06 10:06:53 UTC
  • mto: (3926.1.1 bzr.integration)
  • mto: This revision was merged to the branch mainline in revision 3928.
  • Revision ID: v.ladeuil+lp@free.fr-20081006100653-m74ulbg7rlfcuqox
Add a credential store for '.netrc'.

* plugins/netrc_credential_store/tests/test_netrc.py: 
Associated tests.

* plugins/netrc_credential_store/tests/__init__.py: 
Associated tests.

* plugins/netrc_credential_store/__init__.py: 
Provides a credential store for .netrc. This also documents how to
plug a credential store for authentication.conf.

* tests/__init__.py:
(TestCaseInTempDir.build_tree): Drive-by fix, use assertIsInstance
instead of assert_ to get a meaningful error message.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Use ~/.netrc as a credential store for authentication.conf."""
 
18
 
 
19
from bzrlib import (
 
20
    config,
 
21
    lazy_import,
 
22
    )
 
23
 
 
24
lazy_import.lazy_import(globals(), """
 
25
    import errno
 
26
    import netrc
 
27
 
 
28
    from bzrlib import (
 
29
        errors,
 
30
        )
 
31
""")
 
32
 
 
33
 
 
34
class NetrcCredentialStore(config.CredentialStore):
 
35
 
 
36
    def __init__(self):
 
37
        super(NetrcCredentialStore, self).__init__()
 
38
        try:
 
39
            self._netrc = netrc.netrc()
 
40
        except IOError, e:
 
41
            if e.args[0] == errno.ENOENT:
 
42
                raise errors.NoSuchFile(e.filename)
 
43
            else:
 
44
                raise
 
45
 
 
46
    def decode_password(self, credentials):
 
47
        auth = self._netrc.authenticators(credentials['host'])
 
48
        password = None
 
49
        if auth is not None:
 
50
            user, account, password = auth
 
51
            cred_user = credentials.get('user', None)
 
52
            if cred_user is None or user != cred_user:
 
53
                # We don't use the netrc ability to provide a user since this
 
54
                # is not handled by authentication.conf. So if the user doesn't
 
55
                # match, we don't return a password.
 
56
                password = None
 
57
        return password
 
58
 
 
59
 
 
60
config.credential_store_registry.register_lazy(
 
61
    'netrc', __name__, 'NetrcCredentialStore', help=__doc__)
 
62
 
 
63
 
 
64
def load_tests(basic_tests, module, loader):
 
65
    testmod_names = [
 
66
        'tests',
 
67
        ]
 
68
    basic_tests.addTest(loader.loadTestsFromModuleNames(
 
69
            ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
 
70
    return basic_tests