No Description
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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # -*- coding; utf-8 -*-
  2. from abc import ABC, abstractmethod
  3. from uuid import uuid1
  4. from lodel.auth.exceptions import AuthenticationError
  5. from lodel.utils.datetime import get_utc_timestamp
  6. class SessionStore(ABC):
  7. expiration_limit = 900
  8. ## @brief Generates a session unique ID
  9. # @param cls
  10. # @return str
  11. @classmethod
  12. def generate_new_sid(cls):
  13. return uuid1()
  14. ## @brief Creates a new session
  15. # @param content dict : session content (default: {})
  16. # @return str
  17. def create_new_session(self, content={}):
  18. sid = self.__class__.generate_new_sid()
  19. self.save_session(sid, content)
  20. return sid
  21. ## @brief Destroys a session
  22. # @param sid str : session id
  23. @abstractmethod
  24. def delete_session(self, sid):
  25. pass
  26. ## @brief Reads a session content
  27. # @param sid str : session id
  28. # @return dict
  29. @abstractmethod
  30. def read_session(self, sid):
  31. return {}
  32. ## @brief saves a session to a file
  33. # @param sid str : session id
  34. # @param session dict : content to be saved
  35. @abstractmethod
  36. def save_session(self, sid, session):
  37. pass
  38. ## @brief lists all the sessions ids
  39. # @return list
  40. @abstractmethod
  41. def list_all_sessions(self):
  42. return []
  43. ## @brief cleans the session store's by destroying the expired sessions
  44. def clean(self):
  45. sessions_list = self.list_all_sessions()
  46. for sid in sessions_list:
  47. if self.has_session_expired(sid):
  48. self.delete_session(sid)
  49. ## @brief checks if a session exists
  50. # @param sid str : session id
  51. # @return bool
  52. @abstractmethod
  53. def is_session_existing(self, sid):
  54. return True
  55. ## @brief gets a session's last modified timestamp
  56. # @param sid str: session id
  57. # @return float
  58. @abstractmethod
  59. def get_session_last_modified(self, sid):
  60. pass
  61. ## @brief checks if a session has expired
  62. # @param sid str: session id
  63. # @return bool
  64. def has_session_expired(self, sid):
  65. session_last_modified = self.get_session_last_modified(sid)
  66. expiration_timestamp = session_last_modified + self.expiration_limit
  67. now_timestamp = get_utc_timestamp()
  68. return now_timestamp > expiration_timestamp
  69. ## @brief updates a session's content
  70. # @param sid str : session's id
  71. # @param content dict : items to update with their new value
  72. def update_session(self, sid, content):
  73. session = self.get_session(sid)
  74. for key, value in content.items():
  75. if key != 'sid':
  76. session[key] = value
  77. self.save_session(sid, session)
  78. ## @brief gets a session's content
  79. # @param sid str : id of the session to read
  80. # @return dict
  81. def get_session(self, sid):
  82. if self.is_session_existing(sid):
  83. if not self.has_session_expired(sid):
  84. session = self.read_session(sid)
  85. else:
  86. self.delete_session(sid)
  87. session = {}
  88. else:
  89. raise AuthenticationError("No session file found for the sid %s" % sid)
  90. return session