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.

main.py 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import pickle
  4. import uuid
  5. from lodel.auth.exceptions import AuthenticationError
  6. from lodel.plugin import LodelHook
  7. from lodel.settings import Settings
  8. from lodel.utils.datetime import get_utc_timestamp
  9. SESSION_FILES_BASE_DIR = Settings.sessions.directory
  10. SESSION_FILES_TEMPLATE = Settings.sessions.file_template
  11. SESSION_EXPIRATION_LIMIT = Settings.sessions.expiration
  12. ## @brief generates a new session id
  13. # @return str
  14. def generate_new_sid():
  15. new_sid = uuid.uuid1()
  16. return new_sid
  17. ## @brief gets the session file path corresponding to a session id
  18. def get_session_file_path(sid):
  19. return os.path.join(SESSION_FILES_BASE_DIR, SESSION_FILES_TEMPLATE) % sid
  20. ## @brief starts a new session and returns its sid
  21. # @param caller *
  22. # @param payload dict
  23. # @return str
  24. @LodelHook('session_start')
  25. def start_session(caller, payload):
  26. sid = generate_new_sid()
  27. session_file = get_session_file_path(sid)
  28. session = dict()
  29. for key, value in payload.items():
  30. session[key] = value
  31. pickle.dump(session, open(session_file, "wb"))
  32. return sid
  33. ## @brief stops a session
  34. # @param caller *
  35. # @param sid str : session id
  36. @LodelHook('session_stop')
  37. def stop_session(caller, sid):
  38. session_file_path = get_session_file_path(sid)
  39. if os.path.isfile(session_file_path):
  40. os.unlink(session_file_path)
  41. else:
  42. raise AuthenticationError("No session file found for the sid : %s" % sid)
  43. ## @brief checks if a session file has expired
  44. # @param sid str : session id
  45. # @return bool
  46. def is_session_file_expired(sid):
  47. session_file = get_session_file_path(sid)
  48. if not os.path.isfile(session_file):
  49. raise AuthenticationError("No session file found for the sid : %s" % sid)
  50. expiration_timestamp = os.stat(session_file).st_mtime + SESSION_EXPIRATION_LIMIT
  51. timestamp_now = get_utc_timestamp()
  52. return timestamp_now >= expiration_timestamp
  53. ## @brief reads a session content
  54. # @param caller *
  55. # @param sid str : session id
  56. @LodelHook('session_read')
  57. def read_session(caller, sid):
  58. session_file = get_session_file_path(sid)
  59. if os.path.isfile(session_file):
  60. if not is_session_file_expired(sid):
  61. session = pickle.load(open(session_file, "rb"))
  62. else:
  63. LodelHook.call_hook('session_stop', __file__, sid)
  64. session = {}
  65. else:
  66. raise AuthenticationError("No session file found for the sid : %s" % sid)
  67. return session
  68. ## @brief deletes all old session files (expired ones)
  69. # @param caller *
  70. @LodelHook('lodel_delete_old_session_files')
  71. def delete_old_session_files(caller):
  72. session_files_path = os.path.abspath(SESSION_FILES_BASE_DIR)
  73. session_files = [os.path.join(session_files_path, file_object) for file_object in os.listdir(session_files_path) if os.path.isfile(os.path.join(session_files_path, file_object))]
  74. timestamp_now = get_utc_timestamp()
  75. for session_file in session_files:
  76. last_modified = os.stat(session_file).st_mtime
  77. expiration_timestamp = last_modified + SESSION_EXPIRATION_LIMIT
  78. if timestamp_now > expiration_timestamp:
  79. os.unlink(session_file)