synapse_djadhere.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. response = yield treq.post(
  15. 'https://adherents.tetaneutral.net/accounts/auth_api/%s/' %
  16. self.auth_api_token, {
  17. 'username': localpart,
  18. 'password': password
  19. })
  20. defer.returnValue(response.code == http.OK)
  21. @staticmethod
  22. def parse_config(config):
  23. class _DjadhereConf(object):
  24. pass
  25. conf = _DjadhereConf()
  26. if 'auth_api_token' not in config:
  27. raise Exception('Synapse-Djadhere needs AUTH_API_TOKEN')
  28. conf.auth_api_token = config['auth_api_token']
  29. return conf