authentification.py 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # coding: utf8
  2. from tracim_backend.exceptions import DigestAuthNotImplemented
  3. from tracim_backend.lib.core.user import UserApi
  4. DEFAULT_TRACIM_WEBDAV_REALM = '/'
  5. class TracimDomainController(object):
  6. """
  7. The domain controller is used by http_authenticator to authenticate the user every time a request is
  8. sent
  9. """
  10. def __init__(self, app_config, presetdomain=None, presetserver=None):
  11. self.app_config = app_config
  12. def getDomainRealm(self, inputURL, environ):
  13. return DEFAULT_TRACIM_WEBDAV_REALM
  14. def getRealmUserPassword(self, realmname, username, environ):
  15. """
  16. This method is normally only use for digest auth. wsgidav need
  17. plain password to deal with it. as we didn't
  18. provide support for this kind of auth, this method raise an exception.
  19. """
  20. raise DigestAuthNotImplemented
  21. def requireAuthentication(self, realmname, environ):
  22. return True
  23. def isRealmUser(self, realmname, username, environ):
  24. """
  25. Called to check if for a given root, the username exists (though here we don't make difference between
  26. root as we're always starting at tracim's root
  27. """
  28. api = UserApi(None, environ['tracim_dbsession'], self.app_config)
  29. try:
  30. api.get_one_by_email(username)
  31. return True
  32. except:
  33. return False
  34. def authDomainUser(self, realmname, username, password, environ):
  35. """
  36. If you ever feel the need to send a request al-mano with a curl, this is the function that'll be called by
  37. http_authenticator to validate the password sent
  38. """
  39. api = UserApi(None, environ['tracim_dbsession'], self.app_config)
  40. return self.isRealmUser(realmname, username, environ) and \
  41. api.get_one_by_email(username).validate_password(password)