synapse_djadhere.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. self.bot_user = config.bot_user
  12. self.bot_password = config.bot_password
  13. @defer.inlineCallbacks
  14. def check_password(self, user_id, password):
  15. localpart = user_id.split(":", 1)[0][1:]
  16. bot = (localpart == self.bot_user and password == self.bot_password)
  17. djadhere_response = yield treq.post(
  18. 'https://adherents.tetaneutral.net/accounts/auth_api/%s/' %
  19. self.auth_api_token, {
  20. 'username': localpart,
  21. 'password': password
  22. })
  23. defer.returnValue(bot or djadhere_response.code == http.OK)
  24. @staticmethod
  25. def parse_config(config):
  26. class _DjadhereConf(object):
  27. pass
  28. conf = _DjadhereConf()
  29. if 'auth_api_token' not in config:
  30. raise Exception('Synapse-Djadhere needs AUTH_API_TOKEN')
  31. if 'bot_user' not in config:
  32. raise Exception('Synapse-Djadhere needs BOT_USER')
  33. if 'bot_password' not in config:
  34. raise Exception('Synapse-Djadhere needs BOT_PASSWORD')
  35. conf.auth_api_token = config['auth_api_token']
  36. conf.bot_user = config['bot_user']
  37. conf.bot_password = config['bot_password']
  38. return conf