|
@@ -0,0 +1,71 @@
|
|
1
|
+# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+import os
|
|
4
|
+from werkzeug.contrib.sessions import FilesystemSessionStore
|
|
5
|
+
|
|
6
|
+from lodel.plugin import LodelHook
|
|
7
|
+from lodel.settings import Settings
|
|
8
|
+from lodel.utils.datetime import get_utc_timestamp
|
|
9
|
+
|
|
10
|
+SESSION_FILES_BASE_DIR = Settings.sessions.directory
|
|
11
|
+SESSION_FILES_TEMPLATE = Settings.sessions.file_template
|
|
12
|
+SESSION_EXPIRATION_LIMIT = Settings.sessions.expiration
|
|
13
|
+
|
|
14
|
+session_store = FilesystemSessionStore(path=SESSION_FILES_BASE_DIR, filename_template=SESSION_FILES_TEMPLATE)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+@LodelHook('lodel_delete_old_session_files')
|
|
18
|
+def delete_old_session_files(caller, timestamp_now):
|
|
19
|
+ session_files_path = os.path.abspath(session_store.path)
|
|
20
|
+ 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))]
|
|
21
|
+
|
|
22
|
+ for session_file in session_files:
|
|
23
|
+ last_modified = os.stat(session_file).st_mtime
|
|
24
|
+ expiration_timestamp = last_modified + SESSION_EXPIRATION_LIMIT
|
|
25
|
+ if timestamp_now > expiration_timestamp:
|
|
26
|
+ os.unlink(session_file)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+def is_session_file_expired(timestamp_now, sid):
|
|
30
|
+ session_file = session_store.get_session_filename(sid)
|
|
31
|
+ expiration_timestamp = os.stat(session_file).st_mtime + SESSION_EXPIRATION_LIMIT
|
|
32
|
+ return timestamp_now >= expiration_timestamp
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+## @brief starts a new session
|
|
36
|
+# @param caller *
|
|
37
|
+# @param payload dict : dictionary containing the content of the session
|
|
38
|
+@LodelHook('lodel_start_session')
|
|
39
|
+def start_session(caller, payload):
|
|
40
|
+ session = session_store.new()
|
|
41
|
+ for key, value in payload.items():
|
|
42
|
+ session[key] = value
|
|
43
|
+ session_store.save(session)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+## @brief reads the content of the session
|
|
47
|
+# @param caller *
|
|
48
|
+# @param sid str
|
|
49
|
+# @return dict
|
|
50
|
+@LodelHook('lodel_read_session')
|
|
51
|
+def read_session(caller, sid):
|
|
52
|
+ timestamp = get_utc_timestamp()
|
|
53
|
+ session = None
|
|
54
|
+ if not is_session_file_expired(timestamp, sid):
|
|
55
|
+ session = session_store.get(sid)
|
|
56
|
+
|
|
57
|
+ return session
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+@LodelHook('lodel_update_session')
|
|
61
|
+def update_session(caller, timestamp_now, sid):
|
|
62
|
+ if sid is None or sid not in session_store.list():
|
|
63
|
+ session = session_store.new()
|
|
64
|
+ session['last_accessed'] = timestamp_now
|
|
65
|
+ else:
|
|
66
|
+ session = session_store.get(sid)
|
|
67
|
+ if is_session_file_expired(timestamp_now, sid):
|
|
68
|
+ session_store.delete(session)
|
|
69
|
+ session = session_store.new()
|
|
70
|
+ session['user_context'] = None
|
|
71
|
+ session['last_accessed'] = timestamp_now
|