956
955
if self._config is not None:
957
956
return self._config
958
# FIXME: Should we validate something here ? Includes: port must be
959
# numeric, empty sections are useless, verify_certificates is
960
# boolean, at least one of user/password/password_encoding should
963
# Note: the encoding below declares that the file itself is utf-8
964
# encoded, but the values in the ConfigObj are always Unicode.
959
965
self._config = ConfigObj(self._input, encoding='utf-8')
960
966
except configobj.ConfigObjError, e:
961
967
raise errors.ParseConfigError(e.errors, e.config.filename)
962
968
return self._config
964
970
def get_credentials(self, scheme, host, port=None, user=None, path=None):
971
"""Returns the matching credentials from authentication.conf file.
973
:param scheme: protocol
975
:param host: the server address
977
:param port: the associated port (optional)
979
:param user: login (optional)
981
:param path: the absolute path on the server (optional)
983
:return: A dict with containing thematching credentials or None.
985
- name: the section name of the credentials in the
986
authentication.conf file,
987
- user: can't de different from the provided user if any,
988
- password: the decoded password,
989
- verify_certificates: https specific, True if the server
990
certificate should be verified, False otherwise.
965
992
credentials = None
966
993
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',
994
a_scheme, a_host, a_port, a_user, a_path = map(
995
auth_def.get, ['scheme', 'host', 'port', 'user', 'path'])
997
# Sanitize credentials
998
if a_port is not None: a_port = int(a_port)
1000
a_verify_certificates = auth_def.as_bool('verify_certificates')
1002
a_verify_certificates = True
970
1005
if a_scheme is not None and scheme != a_scheme:
972
1007
if a_host is not None:
973
1008
if not (host == a_host
974
1009
or (a_host.startswith('.') and host.endswith(a_host))):
976
if a_port is not None and port != int(a_port):
1011
if a_port is not None and port != a_port:
978
1013
if (a_path is not None and path is not None
979
1014
and not path.startswith(a_path)):
988
1023
a_password, a_encoding = map(auth_def.get,
989
1024
['password', 'password_encoding'])
990
1025
password = self.decode_password(a_password, a_encoding)
992
verify_certificates = auth_def.as_bool('verify_certificates')
994
verify_certificates = True
995
1026
credentials = {'name': auth_def_name,
996
1027
'user': user, 'password': password,
997
'verify_certificates': verify_certificates,
1028
'verify_certificates': a_verify_certificates,