~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Vincent Ladeuil
  • Date: 2007-10-12 16:19:31 UTC
  • mto: (2961.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 2962.
  • Revision ID: v.ladeuil+lp@free.fr-20071012161931-2an2ctxh3n5ibw4k
Credentials matching implementation.

* bzrlib/tests/test_config.py:
(TestAuthenticationConfig): Test the matching mechanism of
the authentication.conf file

* bzrlib/config.py:
(AuthenticationConfig): New class.

* doc/developers/authentication-ring.txt: 
Clarify some details.

Show diffs side-by-side

added added

removed removed

Lines of Context:
787
787
    return osutils.pathjoin(config_dir(), 'locations.conf')
788
788
 
789
789
 
 
790
def authentication_config_filename():
 
791
    """Return per-user authentication ini file filename."""
 
792
    return osutils.pathjoin(config_dir(), 'authentication.conf')
 
793
 
 
794
 
790
795
def user_ignore_config_filename():
791
796
    """Return the user default ignore filename"""
792
797
    return osutils.pathjoin(config_dir(), 'ignore')
878
883
 
879
884
class TreeConfig(IniBasedConfig):
880
885
    """Branch configuration data associated with its contents, not location"""
 
886
 
881
887
    def __init__(self, branch):
882
888
        self.branch = branch
883
889
 
928
934
            self.branch.control_files.put('branch.conf', out_file)
929
935
        finally:
930
936
            self.branch.unlock()
 
937
 
 
938
 
 
939
class AuthenticationConfig(object):
 
940
    """The authentication configuration file based on a ini file.
 
941
 
 
942
    Implements the authentication.conf file described in
 
943
    doc/developers/authentication-ring.txt.
 
944
    """
 
945
 
 
946
    def __init__(self, _file=None):
 
947
        super(AuthenticationConfig, self).__init__()
 
948
        # The ConfigObj
 
949
        self._config = None
 
950
        if _file is None:
 
951
            self._input = authentication_config_filename()
 
952
        else:
 
953
            self._input = _file
 
954
 
 
955
    def _get_config(self):
 
956
        if self._config is not None:
 
957
            return self._config
 
958
        try:
 
959
            self._config = ConfigObj(self._input, encoding='utf-8')
 
960
        except configobj.ConfigObjError, e:
 
961
            raise errors.ParseConfigError(e.errors, e.config.filename)
 
962
        return self._config
 
963
 
 
964
    def get_credentials(self, scheme, host, port=None, user=None, path=None):
 
965
        credentials = None
 
966
        for auth_def_name, auth_def in self._get_config().items():
 
967
            a_scheme, a_user, a_host, a_port, a_path = map(
 
968
                auth_def.get, ['scheme', 'user', 'host', 'port',
 
969
                               'path'])
 
970
            if a_scheme is not None and scheme != a_scheme:
 
971
                continue
 
972
            if a_host is not None:
 
973
                if not (host == a_host
 
974
                        or (a_host.startswith('.') and host.endswith(a_host))):
 
975
                    continue
 
976
            if a_port is not None and port != int(a_port):
 
977
                continue
 
978
            if (a_path is not None and path is not None
 
979
                and not path.startswith(a_path)):
 
980
                continue
 
981
            if (a_user is not None and user is not None
 
982
                and a_user != user):
 
983
                continue
 
984
            user = a_user
 
985
            if user is None:
 
986
                # Can't find a user
 
987
                continue
 
988
            a_password, a_encoding = map(auth_def.get,
 
989
                                         ['password', 'password_encoding'])
 
990
            password = self.decode_password(a_password, a_encoding)
 
991
            try:
 
992
                verify_certificates = auth_def.as_bool('verify_certificates')
 
993
            except KeyError:
 
994
                verify_certificates = True
 
995
            credentials = {'name': auth_def_name,
 
996
                           'user': user, 'password': password,
 
997
                           'verify_certificates': verify_certificates,
 
998
                           }
 
999
            break
 
1000
 
 
1001
        return credentials
 
1002
 
 
1003
    def decode_password(self, password, encoding):
 
1004
        return password