Guest User

Untitled

a guest
Nov 20th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. def load(self):
  2.  
  3. dd = self._session_key.split("--")
  4.  
  5. # make sure we got both elements
  6. if len(dd) == 2:
  7. data = re.sub('%3D','=',dd[0])
  8. # now make sure digest is valid
  9. if dd[1] == self.generate_digest(data):
  10. # valid. decode and load data
  11. decoded_data = base64.b64decode(data)
  12.  
  13. # First load with YAML, if there is a YAML syntax error, then load with JSON
  14. try:
  15. print "trying yaml..."
  16. obj = yaml.load(decoded_data)
  17. except ValueError:
  18. print "trying json..."
  19. obj = simplejson.loads(decoded_data)
  20. except:
  21. print "Couldn't load data. A new session will be created."
  22. obj = False
  23.  
  24. if obj:
  25. print "got object"
  26. # intercept _session_expiry
  27. if obj.has_key("_session_expiry"):
  28. obj['_session_expiry'] = datetime.datetime.fromtimestamp(int(obj['_session_expiry']))
  29. return obj
  30. else:
  31. # if we get here, it was invalid and we should generate a new session
  32. self.create()
  33. return {}
  34.  
  35.  
  36. def _get_session_key(self):
  37.  
  38. obj = getattr(self, '_session_cache', {})
  39.  
  40. # intercept _session_expiry
  41. if obj.has_key("_session_expiry") and isinstance(obj['_session_expiry'],datetime.datetime):
  42. obj['_session_expiry'] = obj['_session_expiry'].strftime("%s")
  43.  
  44. # add session_id if it's not present
  45. if not obj.has_key("session_id"):
  46. obj['session_id'] = rand.bytes(16).encode('hex_codec')
  47.  
  48. # Dump to YAML, then encode as base64
  49. enc = base64.b64encode(yaml.dump(obj))
  50.  
  51. return "--".join([re.sub('=','%3D',enc),self.generate_digest(enc)])
Add Comment
Please, Sign In to add comment