synapse_djadhere.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """
  2. try to use djadhere to authenticate users in matrix
  3. cf https://github.com/matrix-org/matrix-synapse-ldap3/blob/master/ldap_auth_provider.py
  4. """
  5. import treq
  6. from twisted.internet import defer
  7. from twisted.web import http
  8. class SynapseDjadhere(object):
  9. def __init__(self, config, account_handler):
  10. self.auth_api_token = config.auth_api_token
  11. @defer.inlineCallbacks
  12. def check_password(self, user_id, password):
  13. localpart = user_id.split(":", 1)[0][1:]
  14. bot = user_id == self.bot_user and password == self.password
  15. djadhere_response = yield treq.post(
  16. 'https://adherents.tetaneutral.net/accounts/auth_api/%s/' %
  17. self.auth_api_token, {
  18. 'username': localpart,
  19. 'password': password
  20. })
  21. defer.returnValue(bot or djadhere_response.code == http.OK)
  22. @staticmethod
  23. def parse_config(config):
  24. class _DjadhereConf(object):
  25. pass
  26. conf = _DjadhereConf()
  27. if 'auth_api_token' not in config:
  28. raise Exception('Synapse-Djadhere needs AUTH_API_TOKEN')
  29. if 'bot_user' not in config:
  30. raise Exception('Synapse-Djadhere needs BOT_USER')
  31. if 'bot_password' not in config:
  32. raise Exception('Synapse-Djadhere needs BOT_PASSWORD')
  33. conf.auth_api_token = config['auth_api_token']
  34. conf.bot_user = config['bot_user']
  35. conf.bot_password = config['bot_password']
  36. return conf