~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Vincent Ladeuil
  • Date: 2008-10-05 21:32:00 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-20081005213200-tbgbnnhq6rjabnlz
Add credential stores plugging.

* tests/test_config.py:
(TestAuthenticationConfigFile.test_unknown_password_encoding):
Test that the user get a meaningful error on typos or installation
problems.
(TestCredentialStoreRegistry): Basic tests for the registry.
(TestPlainTextCredentialStore): Test plain text credential store.

* config.py:
(AuthenticationConfig.decode_password): Go through the credential
store registry to decode the password.
(CredentialStoreRegistry): New registry for credential stores.
(CredentialStore): Abstract base class for credential stores.
(PlainTextCredentialStore): Default, plain text, credential store,
using authentication.conf file itself as storage(i.e actual
behavior).

Show diffs side-by-side

added added

removed removed

Lines of Context:
78
78
    errors,
79
79
    mail_client,
80
80
    osutils,
 
81
    registry,
81
82
    symbol_versioning,
82
83
    trace,
83
84
    ui,
1131
1132
        return password
1132
1133
 
1133
1134
    def decode_password(self, credentials, encoding):
 
1135
        try:
 
1136
            cs = credential_store_registry.get_credential_store(encoding)
 
1137
        except KeyError:
 
1138
            raise ValueError('%r is not a known password_encoding' % encoding)
 
1139
        credentials['password'] = cs.decode_password(credentials)
1134
1140
        return credentials
1135
1141
 
1136
1142
 
 
1143
class CredentialStoreRegistry(registry.Registry):
 
1144
    """A class that registers credential stores.
 
1145
 
 
1146
    A credential store provides access to credentials via the password_encoding
 
1147
    field in authentication.conf sections.
 
1148
 
 
1149
    Except for stores provided by bzr itself,most stores are expected to be
 
1150
    provided by plugins that will therefore use
 
1151
    register_lazy(password_encoding, module_name, member_name, help=help,
 
1152
    info=info) to install themselves.
 
1153
    """
 
1154
 
 
1155
    def get_credential_store(self, encoding=None):
 
1156
        cs = self.get(encoding)
 
1157
        if callable(cs):
 
1158
            cs = cs()
 
1159
        return cs
 
1160
 
 
1161
 
 
1162
credential_store_registry = CredentialStoreRegistry()
 
1163
 
 
1164
 
 
1165
class CredentialStore(object):
 
1166
    """An abstract class to implement storage for credentials"""
 
1167
 
 
1168
    def decode_password(self, credentials):
 
1169
        """Returns a password for the provided credentials in clear text."""
 
1170
        raise NotImplementedError(self.decode_password)
 
1171
 
 
1172
 
 
1173
class PlainTextCredentialStore(CredentialStore):
 
1174
    """Plain text credential store for the authentication.conf file."""
 
1175
 
 
1176
    def decode_password(self, credentials):
 
1177
        """See CredentialStore.decode_password."""
 
1178
        return credentials['password']
 
1179
 
 
1180
 
 
1181
credential_store_registry.register('plain', PlainTextCredentialStore,
 
1182
                                   help=PlainTextCredentialStore.__doc__)
 
1183
credential_store_registry.default_key = 'plain'
 
1184
 
 
1185
 
1137
1186
class BzrDirConfig(object):
1138
1187
 
1139
1188
    def __init__(self, transport):