Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

session.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # -*- coding; utf-8 -*-
  2. from abc import ABC, abstractmethod
  3. from uuid import uuid1
  4. from werkzeug.contrib.sessions import generate_key, Session
  5. #from lodel.auth.exceptions import AuthenticationError
  6. from lodel.utils.datetime import get_utc_timestamp
  7. class LodelSession(ABC):
  8. expiration_limit = 900
  9. __sessions = {}
  10. ## @brief creates a session
  11. # @param contests dict : session initialization keys
  12. def __init__(self, contents={}):
  13. self.sid = self.__class__.generate_new_sid()
  14. for key, value in contents.items():
  15. self.set_key(key, value)
  16. self.store()
  17. self.register_session()
  18. ## @brief stores the session
  19. @abstractmethod
  20. def store(self):
  21. pass
  22. ## @brief registers the session
  23. @abstractmethod
  24. def register_session(self):
  25. pass
  26. ## @brief saves a session
  27. @abstractmethod
  28. def save(self):
  29. pass
  30. ## @brief loads a session
  31. # @param sid str : session id
  32. # @return LodelSession
  33. @classmethod
  34. @abstractmethod
  35. def load(cls, sid):
  36. return None
  37. ## @brief cleans the session store
  38. @classmethod
  39. @abstractmethod
  40. def clean(cls):
  41. pass
  42. ## @brief destroys a session
  43. # @param sid str : session id
  44. @classmethod
  45. def destroy(cls, sid):
  46. cls.delete_session(sid)
  47. cls.unregister_session(sid)
  48. ## @brief deletes a session
  49. # @param sid str : session id
  50. @classmethod
  51. @abstractmethod
  52. def delete_session(cls, sid):
  53. pass
  54. ## @brief unregisters a session from the session store list
  55. # @param sid str : session id
  56. @classmethod
  57. def unregister_session(cls, sid):
  58. if sid in cls.__sessions:
  59. del cls.__sessions[sid]
  60. ## @brief checks if a session if registered
  61. # @param sid str : session id
  62. # @return bool
  63. @classmethod
  64. def is_registered(cls, sid):
  65. return (sid in cls.__sessions)
  66. ## @brief checks if a session is expired
  67. # @param sid str : session id
  68. @classmethod
  69. def is_expired(cls, sid):
  70. expiration_timestamp = cls.get_last_modified(sid) + cls.EXPIRATION_LIMIT
  71. now_timestamp = get_utc_timestamp()
  72. return now_timestamp > expiration_timestamp
  73. ## @brief gets the timestamp of the last modification
  74. # @param sid str : session id
  75. # @return float
  76. @classmethod
  77. @abstractmethod
  78. def get_last_modified(cls, sid):
  79. return 0.0
  80. ## @brief generates a new session id
  81. # @return sid
  82. # @todo find a more secure way of building a session id
  83. @classmethod
  84. def generate_new_sid(cls):
  85. return uuid1()
  86. ## @brief sets a key's value in the session
  87. # @param key str
  88. # @param value unknown
  89. def set_key(self, key, value):
  90. setattr(self, key, value)
  91. self.save()
  92. ## @brief gets a key's value from the session
  93. # @param key str
  94. # @return unknown
  95. def get_key(self, key):
  96. return getattr(object=self, name=key, default=None)
  97. ## @brief deletes a given key in the session
  98. # @param key str
  99. def delete_key(self, key):
  100. delattr(self, key)
  101. self.save()
  102. ## @brief lists all the sessions in the session store
  103. # @return list
  104. @classmethod
  105. @abstractmethod
  106. def list_all_sessions(cls):
  107. return []
  108. ## @brief checks if a given session id corresponds to an existing session
  109. # @param sid str: session id
  110. # @return bool
  111. @classmethod
  112. @abstractmethod
  113. def exists(cls, sid):
  114. return True