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.

filesystem_session_store.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import pickle
  4. import re
  5. from lodel.auth.session import LodelSession
  6. from lodel.settings import Settings
  7. class FileSystemSession(LodelSession):
  8. __sessions = {}
  9. EXPIRATION_LIMIT = Settings.sessions.expiration
  10. BASE_DIRECTORY = Settings.sessions.directory
  11. FILENAME_TEMPLATE = Settings.sessions.file_template
  12. def __init__(self):
  13. self.path = None # Is initialized in the store method
  14. super().__init__()
  15. ## @brief stores the session
  16. def store(self):
  17. self.path = self.__class__.generate_session_file_name(self.sid)
  18. self.save()
  19. ## @brief registers the session in the active sessions list
  20. def register_session(self):
  21. self.__class__.__sessions[self.sid] = self.path
  22. ## @brief saves the session
  23. # @todo security
  24. def save(self):
  25. with open(self.path, 'wb') as session_file:
  26. pickle.dump(self, session_file)
  27. ## @brief loads a session
  28. # @param sid str : session id
  29. # @return FileSystemSession
  30. @classmethod
  31. def load(cls, sid):
  32. if sid in cls.__sessions.keys():
  33. session_file_path = cls.__sessions[sid]
  34. with open(session_file_path, 'rb') as session_file:
  35. session = pickle.load(session_file)
  36. return session
  37. return None
  38. ## @brief cleans the session store
  39. # @todo add more checks
  40. @classmethod
  41. def clean(cls):
  42. # unregistered files in the session directory (if any)
  43. session_dir_files = cls.list_all_sessions()
  44. for session_dir_file in session_dir_files:
  45. sid = cls.filename_to_sid(session_dir_file)
  46. if sid is None or sid not in cls.__sessions.keys():
  47. os.unlink(session_dir_file)
  48. # registered sessions
  49. for sid in cls.__sessions.keys():
  50. if cls.is_expired(sid):
  51. cls.destroy(sid)
  52. ## @brief gets the last modified date of a session
  53. # @param sid str : session id
  54. @classmethod
  55. def get_last_modified(cls, sid):
  56. return os.stat(cls.__sessions[sid]).st_mtime
  57. ## @brief lists all the files contained in the session directory
  58. # @return list
  59. @classmethod
  60. def list_all_sessions(cls):
  61. session_files_directory = os.abspath(cls.BASE_DIRECTORY)
  62. files_list = [file_path for file_path in os.listdir(session_files_directory) if os.path.isfile(os.path.join(session_files_directory, file_path))]
  63. return files_list
  64. ## @brief returns the session id from the filename
  65. # @param filename str : session file's name
  66. # @return str
  67. @classmethod
  68. def filename_to_sid(cls, filename):
  69. sid_regex = re.compile(cls.FILENAME_TEMPLATE % '(?P<sid>.*)')
  70. sid_searching_result = sid_regex.match(filename)
  71. if sid_searching_result is not None:
  72. return sid_searching_result.groupdict()['sid']
  73. return None
  74. ## @brief deletes a session's informations
  75. @classmethod
  76. def delete_session(cls, sid):
  77. if os.path.isfile(cls.__sessions[sid]):
  78. # Deletes the session file
  79. os.unlink(cls.__sessions[sid])
  80. ## @brief generates session file name
  81. # @param sid str : session id
  82. # @return str
  83. @classmethod
  84. def generate_session_file_name(cls, sid):
  85. return os.path.join(cls.BASE_DIRECTORY, cls.FILENAME_TEMPLATE) % sid
  86. ## @brief checks if a session exists
  87. # @param sid str: session id
  88. # @return bool
  89. @classmethod
  90. def exists(cls, sid):
  91. return cls.is_registered(sid) and (os.path.isfile(cls.__sessions[sid]))