""" try to use djadhere to authenticate users in matrix cf https://github.com/matrix-org/matrix-synapse-ldap3/blob/master/ldap_auth_provider.py """ import treq from twisted.internet import defer from twisted.web import http class SynapseDjadhere(object): def __init__(self, config, account_handler): self.auth_api_token = config.auth_api_token self.bot_user = config.bot_user self.bot_password = config.bot_password @defer.inlineCallbacks def check_password(self, user_id, password): localpart = user_id.split(":", 1)[0][1:] bot = (localpart == self.bot_user and password == self.bot_password) djadhere_response = yield treq.post( 'https://adherents.tetaneutral.net/accounts/auth_api/%s/' % self.auth_api_token, { 'username': localpart, 'password': password }) defer.returnValue(bot or djadhere_response.code == http.OK) @staticmethod def parse_config(config): class _DjadhereConf(object): pass conf = _DjadhereConf() if 'auth_api_token' not in config: raise Exception('Synapse-Djadhere needs AUTH_API_TOKEN') if 'bot_user' not in config: raise Exception('Synapse-Djadhere needs BOT_USER') if 'bot_password' not in config: raise Exception('Synapse-Djadhere needs BOT_PASSWORD') conf.auth_api_token = config['auth_api_token'] conf.bot_user = config['bot_user'] conf.bot_password = config['bot_password'] return conf