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