common.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import logging
  2. from sydent.db.valsession import ThreePidValSessionStore
  3. from sydent.validators import ValidationSession
  4. from sydent.util import time_msec
  5. logger = logging.getLogger(__name__)
  6. def validateSessionWithToken(sydent, sid, clientSecret, token):
  7. """
  8. Attempt to validate a session, identified by the sid, using
  9. the token from out-of-band. The client secret is given to
  10. prevent attempts to guess the token for a sid.
  11. If the session was sucessfully validated, return a dict
  12. with 'success': True that can be sent to the client,
  13. otherwise return False.
  14. """
  15. valSessionStore = ThreePidValSessionStore(sydent)
  16. s = valSessionStore.getTokenSessionById(sid)
  17. if not s:
  18. logger.info("Session ID %s not found", (sid))
  19. return False
  20. if not clientSecret == s.clientSecret:
  21. logger.info("Incorrect client secret", (sid))
  22. raise IncorrectClientSecretException()
  23. if s.mtime + ValidationSession.THREEPID_SESSION_VALIDATION_TIMEOUT_MS < time_msec():
  24. logger.info("Session expired")
  25. raise SessionExpiredException()
  26. # TODO once we can validate the token oob
  27. #if tokenObj.validated and clientSecret == tokenObj.clientSecret:
  28. # return True
  29. if s.token == token:
  30. logger.info("Setting session %s as validated", (s.id))
  31. valSessionStore.setValidated(s.id, True)
  32. return {'success': True}
  33. else:
  34. logger.info("Incorrect token submitted")
  35. return False