|
@@ -1,58 +1,11 @@
|
1
|
1
|
# -*- coding: utf-8 -*-
|
2
|
2
|
|
3
|
|
-import os
|
4
|
|
-import pickle
|
5
|
|
-import uuid
|
6
|
|
-
|
7
|
3
|
from lodel.auth.exceptions import AuthenticationError
|
8
|
4
|
from lodel.plugin import LodelHook
|
9
|
|
-from lodel.settings import Settings
|
10
|
|
-from lodel.utils.datetime import get_utc_timestamp
|
11
|
|
-
|
12
|
|
-SESSION_FILES_BASE_DIR = Settings.sessions.directory
|
13
|
|
-SESSION_FILES_TEMPLATE = Settings.sessions.file_template
|
14
|
|
-SESSION_EXPIRATION_LIMIT = Settings.sessions.expiration
|
15
|
|
-
|
16
|
|
-
|
17
|
|
-## @brief generates a new session id
|
18
|
|
-def generate_new_sid():
|
19
|
|
- return uuid.uuid1()
|
20
|
|
-
|
21
|
|
-
|
22
|
|
-## @brief gets the session file path, given a session id
|
23
|
|
-# @param sid str : session id
|
24
|
|
-# @return str
|
25
|
|
-def get_session_file_path(sid):
|
26
|
|
- return os.path.join(SESSION_FILES_BASE_DIR, SESSION_FILES_TEMPLATE) % sid
|
27
|
|
-
|
28
|
|
-
|
29
|
|
-## @brief saves the session content to a session file
|
30
|
|
-# @param sid str: session id
|
31
|
|
-# @param session dict : session content to be saved
|
32
|
|
-def save_session(sid, session):
|
33
|
|
- session_file_path = get_session_file_path(sid)
|
34
|
|
- pickle.dump(session, open(session_file_path, "wb"))
|
35
|
5
|
|
|
6
|
+from .filesystem_session_store import FileSystemSessionStore
|
36
|
7
|
|
37
|
|
-## @brief checks if a session file has expired
|
38
|
|
-# @param sid str : session id
|
39
|
|
-# @return bool
|
40
|
|
-def is_session_expired(sid):
|
41
|
|
- session_file = get_session_file_path(sid)
|
42
|
|
- if not os.path.isfile(session_file):
|
43
|
|
- raise AuthenticationError("No session file found for the sid : %s" % sid)
|
44
|
|
-
|
45
|
|
- expiration_timestamp = os.stat(session_file).st_mtime + SESSION_EXPIRATION_LIMIT
|
46
|
|
- now_timestamp = get_utc_timestamp()
|
47
|
|
- return now_timestamp >= expiration_timestamp
|
48
|
|
-
|
49
|
|
-
|
50
|
|
-## @brief reads a session content
|
51
|
|
-# @param sid str: session id
|
52
|
|
-# @return dict
|
53
|
|
-def get_session_content(sid):
|
54
|
|
- session_file_path = get_session_file_path(sid)
|
55
|
|
- return pickle.load(open(session_file_path), 'rb')
|
|
8
|
+session_store = FileSystemSessionStore()
|
56
|
9
|
|
57
|
10
|
|
58
|
11
|
## @brief starts a new session and returns its sid
|
|
@@ -61,66 +14,41 @@ def get_session_content(sid):
|
61
|
14
|
# @return str
|
62
|
15
|
@LodelHook('session_start')
|
63
|
16
|
def start_session(caller, payload):
|
64
|
|
- sid = generate_new_sid()
|
65
|
|
- session_file = get_session_file_path(sid)
|
66
|
|
- session = dict()
|
67
|
|
- for key, value in payload.items():
|
68
|
|
- session[key] = value
|
69
|
|
- save_session(sid, session)
|
70
|
|
- return sid
|
|
17
|
+ return session_store.create_new_session(payload)
|
71
|
18
|
|
72
|
|
-## @brief stops a session
|
|
19
|
+
|
|
20
|
+## @brief destroys a session
|
73
|
21
|
# @param caller *
|
74
|
22
|
# @param sid str : session id
|
75
|
23
|
@LodelHook('session_destroy')
|
76
|
24
|
def stop_session(caller, sid):
|
77
|
|
- session_file_path = get_session_file_path(sid)
|
78
|
|
- if os.path.isfile(session_file_path):
|
79
|
|
- os.unlink(session_file_path)
|
80
|
|
- else:
|
81
|
|
- raise AuthenticationError("No session file found for the sid %s" % sid)
|
|
25
|
+ session_store.delete_session(sid)
|
82
|
26
|
|
83
|
27
|
|
84
|
28
|
## @brief reads a session content
|
85
|
29
|
# @param caller *
|
86
|
30
|
# @param sid str: session id
|
|
31
|
+# @return dict
|
87
|
32
|
@LodelHook('session_load')
|
88
|
33
|
def read_session(caller, sid):
|
89
|
|
- session_file = get_session_file_path(sid)
|
90
|
|
- if os.path.isfile(session_file):
|
91
|
|
- if not is_session_expired(sid):
|
92
|
|
- session = get_session_content(sid)
|
93
|
|
- else:
|
94
|
|
- LodelHook.call_hook('session_stop', __file__, sid)
|
95
|
|
- session = {}
|
96
|
|
- else:
|
97
|
|
- raise AuthenticationError("No session file found for the sid %s" % sid)
|
98
|
|
- return session
|
|
34
|
+ return session_store.get_session(sid)
|
99
|
35
|
|
100
|
36
|
|
101
|
|
-## @brief deletes all old session files (expired ones)
|
|
37
|
+## @brief destroys all the old sessions (expired ones)
|
102
|
38
|
# @param caller *
|
103
|
39
|
@LodelHook('session_clean')
|
104
|
40
|
def clean_sessions(caller):
|
105
|
|
- session_files_path = os.path.abspath(SESSION_FILES_BASE_DIR)
|
106
|
|
- 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))]
|
107
|
|
- now_timestamp = get_utc_timestamp()
|
108
|
|
- for session_file in session_files:
|
109
|
|
- last_modified = os.stat(session_file).st_mtime
|
110
|
|
- expiration_timestamp = last_modified + SESSION_EXPIRATION_LIMIT
|
111
|
|
- if now_timestamp > expiration_timestamp:
|
112
|
|
- os.unlink(session_file)
|
|
41
|
+ session_store.clean()
|
|
42
|
+
|
113
|
43
|
|
114
|
|
-## @brief updates the content session
|
|
44
|
+## @brief updates the content of the session
|
115
|
45
|
# @param caller *
|
116
|
|
-# @param payload dict: datas to insert/update in the session
|
|
46
|
+# @param payload dict : datas to insert/update in the session
|
117
|
47
|
@LodelHook('update_session')
|
118
|
48
|
def update_session_content(caller, payload):
|
119
|
49
|
if 'sid' in payload:
|
120
|
50
|
sid = payload['sid']
|
121
|
|
- session = LodelHook.call_hook('session_load', __file__, sid)
|
122
|
|
- for key, value in payload.items():
|
123
|
|
- session[key] = value
|
124
|
|
- save_session(sid, session)
|
|
51
|
+ del payload['sid']
|
|
52
|
+ session_store.update_session(sid, payload)
|
125
|
53
|
else:
|
126
|
|
- raise AuthenticationError("Missing session id in the request")
|
|
54
|
+ raise AuthenticationError("Session Update: Missing sid (session id) in the given payload argument")
|