def authenticate(self, username: str, password: str) -> tuple[bool, str]:
"""Authenticate a user from its login and password.
:param username: (str) the username of the user
:param password: (str) the password of the user
:return: (bool) User is successfully authenticated
:raises: TypeError: given ``username`` or ``password`` are not strings
"""
if self._available is False:
return (False, 'LdapAuthentication unavailable')
if isinstance(username, str) is False:
raise TypeError("LdapAuthentication 'username' must be a string. Got {} instead.".format(type(username).__name__))
if isinstance(password, str) is False:
raise TypeError("LdapAuthentication 'password' must be a string. Got {} instead.".format(type(password)))
try:
self.__connexion = self.__create_ldap_connection(username, password)
return (True, 'User successfully authenticated')
except Exception as e:
logging.error("Connection to server '{:s}' in domain '{:s}' failed due to the following error: {:s}".format(repr(self.__server), self.__domain, str(e)))
return (False, f'User not authenticated due to the following error: {e}')