common.py 1.6 KB

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