|
@@ -5,9 +5,7 @@ import os
|
5
|
5
|
import hashlib
|
6
|
6
|
import time
|
7
|
7
|
|
8
|
|
-from werkzeug.contrib.sessions import FilesystemSessionStore
|
9
|
8
|
from werkzeug.wrappers import Response
|
10
|
|
-from werkzeug.contrib.securecookie import SecureCookie
|
11
|
9
|
|
12
|
10
|
from lodel.settings import Settings
|
13
|
11
|
from .interface.router import get_controller
|
|
@@ -15,21 +13,18 @@ from .interface.lodelrequest import LodelRequest
|
15
|
13
|
from .exceptions import *
|
16
|
14
|
from .client import WebUiClient
|
17
|
15
|
from lodel.auth.exceptions import *
|
18
|
|
-from lodel.utils.datetime import get_utc_timestamp
|
19
|
|
-from lodel.plugin.hooks import LodelHook
|
20
|
16
|
|
21
|
17
|
SESSION_FILES_BASE_DIR = Settings.webui.sessions.directory
|
22
|
18
|
SESSION_FILES_TEMPLATE = Settings.webui.sessions.file_template
|
23
|
19
|
SESSION_EXPIRATION_LIMIT = Settings.webui.sessions.expiration
|
24
|
20
|
|
25
|
|
-session_store = FilesystemSessionStore(path=SESSION_FILES_BASE_DIR, filename_template=SESSION_FILES_TEMPLATE)
|
26
|
|
-
|
|
21
|
+# TODO Add these informations to the configuration options (lodel2.webui.cookies)
|
27
|
22
|
COOKIE_SESSION_ID = 'toktoken'
|
28
|
23
|
COOKIE_SESSION_HASH = 'nekotkot'
|
29
|
|
-#COOKIE_SESSION_HASH_SALT = [ os.urandom(32) for _ in range(2) ] #Before and after salt (maybe useless)
|
30
|
24
|
COOKIE_SESSION_HASH_SALT = ['salt1', 'salt2']
|
31
|
25
|
COOKIE_SESSION_HASH_ALGO = hashlib.sha512
|
32
|
26
|
|
|
27
|
+
|
33
|
28
|
##@brief Return a salted hash of a cookie
|
34
|
29
|
def cookie_hash(token):
|
35
|
30
|
token = str(token)
|
|
@@ -58,6 +53,7 @@ def load_cookie(request):
|
58
|
53
|
WebUiClient, 'Bad cookies : hash mismatch')
|
59
|
54
|
return token
|
60
|
55
|
|
|
56
|
+
|
61
|
57
|
##@brief Properly set cookies and hash given a token
|
62
|
58
|
#@param response
|
63
|
59
|
#@param token str : the session token
|
|
@@ -65,10 +61,10 @@ def save_cookie(response, token):
|
65
|
61
|
response.set_cookie(COOKIE_SESSION_ID, token)
|
66
|
62
|
response.set_cookie(COOKIE_SESSION_HASH, cookie_hash(token))
|
67
|
63
|
|
|
64
|
+
|
68
|
65
|
def empty_cookie(response):
|
69
|
66
|
response.set_cookie(COOKIE_SESSION_ID, '')
|
70
|
67
|
response.set_cookie(COOKIE_SESSION_HASH, '')
|
71
|
|
-
|
72
|
68
|
|
73
|
69
|
|
74
|
70
|
#Starting instance
|
|
@@ -78,26 +74,6 @@ import lodel
|
78
|
74
|
import leapi_dyncode as dyncode
|
79
|
75
|
lodel.dyncode = dyncode
|
80
|
76
|
|
81
|
|
-# TODO déplacer dans un module "sessions.py"
|
82
|
|
-def delete_old_session_files(timestamp_now):
|
83
|
|
- session_files_path = os.path.abspath(session_store.path)
|
84
|
|
- session_files = [os.path.join(session_files_path, file_object) for file_object in os.listdir(session_files_path)
|
85
|
|
- if os.path.isfile(os.path.join(session_files_path, file_object))]
|
86
|
|
-
|
87
|
|
- for session_file in session_files:
|
88
|
|
- last_modified = os.stat(session_file).st_mtime
|
89
|
|
- expiration_timestamp = last_modified + SESSION_EXPIRATION_LIMIT
|
90
|
|
- if timestamp_now > expiration_timestamp:
|
91
|
|
- os.unlink(session_file)
|
92
|
|
-
|
93
|
|
-
|
94
|
|
-def is_session_file_expired(timestamp_now, sid):
|
95
|
|
- session_file = session_store.get_session_filename(sid)
|
96
|
|
- expiration_timestamp = os.stat(session_file).st_mtime + SESSION_EXPIRATION_LIMIT
|
97
|
|
- if timestamp_now < expiration_timestamp:
|
98
|
|
- return False
|
99
|
|
- return True
|
100
|
|
-
|
101
|
77
|
|
102
|
78
|
# WSGI Application
|
103
|
79
|
def application(env, start_response):
|
|
@@ -130,7 +106,7 @@ def application(env, start_response):
|
130
|
106
|
return res
|
131
|
107
|
session_token = WebUiClient.session_token()
|
132
|
108
|
if session_token is not None:
|
133
|
|
- save_cookie(response,session_token)
|
|
109
|
+ save_cookie(response, session_token)
|
134
|
110
|
session_token = None
|
135
|
111
|
except (ClientError, ClientAuthenticationError):
|
136
|
112
|
response = HttpException(200).render(request)
|