|
@@ -0,0 +1,76 @@
|
|
1
|
+from flask.sessions import SessionInterface
|
|
2
|
+import os
|
|
3
|
+import re
|
|
4
|
+from lodel.session.lodel_filesystem_session.lodel_filesystem_session import LodelFileSystemSession
|
|
5
|
+from contextlib import suppress
|
|
6
|
+import binascii
|
|
7
|
+
|
|
8
|
+class LodelFileSystemSessionInterface(SessionInterface):
|
|
9
|
+
|
|
10
|
+ __sessions = dict()
|
|
11
|
+
|
|
12
|
+ def __init__(self, directory):
|
|
13
|
+ self.directory = os.path.abspath(directory)
|
|
14
|
+ os.makedirs(self.directory, exist_ok=True)
|
|
15
|
+
|
|
16
|
+ def open_session(self, app, request):
|
|
17
|
+ self.filename_template = app.config['lodel.filesystem_sessions']['filename_template']
|
|
18
|
+ self.session_tokensize = int(app.config['lodel.sessions']['tokensize'])
|
|
19
|
+ sid = request.cookies.get(app.session_cookie_name) or self.generate_token() #or '{}-{}'.format(uuid1(), os.getpid())
|
|
20
|
+ if LodelFileSystemSessionInterface.__sessions.get(sid, None) is None:
|
|
21
|
+ LodelFileSystemSessionInterface.__sessions[sid] = self.generate_file_path(sid, self.filename_template)
|
|
22
|
+ return LodelFileSystemSession(self.directory, sid)
|
|
23
|
+
|
|
24
|
+ def save_session(self, app, session, response):
|
|
25
|
+ domain = self.get_cookie_domain(app)
|
|
26
|
+
|
|
27
|
+ if not session:
|
|
28
|
+ with suppress(FileNotFoundError):
|
|
29
|
+ os.unlink(session.path)
|
|
30
|
+ del(LodelFileSystemSessionInterface.__sessions[session.sid])
|
|
31
|
+ response.delete_cookie(app.session_cookie_name, domain=domain)
|
|
32
|
+ return
|
|
33
|
+
|
|
34
|
+ cookie_exp = self.get_expiration_time(app, session)
|
|
35
|
+ response.set_cookie(app.session_cookie_name, session.sid, expires=cookie_exp, httponly=False,
|
|
36
|
+ domain=domain)
|
|
37
|
+
|
|
38
|
+ def generate_file_path(self, sid, filename_template):
|
|
39
|
+ return os.path.abspath(os.path.join(self.directory, filename_template) % sid)
|
|
40
|
+
|
|
41
|
+ ## @brief Retrieves the token from the file system
|
|
42
|
+ #
|
|
43
|
+ # @param filepath str
|
|
44
|
+ # @return str|None : returns the token or None if no token was found
|
|
45
|
+ def get_token_from_filepath(self, filepath):
|
|
46
|
+ token_regex = re.compile(os.path.abspath(os.path.join(self.directory, self.filename_template % '(?P<token>.*)')))
|
|
47
|
+ token_search_result = token_regex.match(filepath)
|
|
48
|
+ if token_search_result is not None:
|
|
49
|
+ return token_search_result.groupdict()['token']
|
|
50
|
+ return None
|
|
51
|
+
|
|
52
|
+ ## @brief generates a new session token
|
|
53
|
+ #
|
|
54
|
+ # @return str
|
|
55
|
+ #
|
|
56
|
+ # @remarks There is no valid reason for checking the generated token uniqueness:
|
|
57
|
+ # - checking for uniqueness is slow ;
|
|
58
|
+ # - keeping a dict with a few thousand keys of hundred bytes also is
|
|
59
|
+ # memory expensive ;
|
|
60
|
+ # - should the system get distributed while sharing session storage, there
|
|
61
|
+ # would be no reasonable way to efficiently check for uniqueness ;
|
|
62
|
+ # - sessions do have a really short life span, drastically reducing
|
|
63
|
+ # even more an already close to inexistent risk of collision. A 64 bits
|
|
64
|
+ # id would perfectly do the job, or to be really cautious, a 128 bits
|
|
65
|
+ # one (actual size of UUIDs) ;
|
|
66
|
+ # - if we are still willing to ensure uniqueness, then simply salt it
|
|
67
|
+ # with a counter, or a timestamp, and hash the whole thing with a
|
|
68
|
+ # cryptographically secured method such as sha-2 if we are paranoids
|
|
69
|
+ # and trying to avoid what will never happen, ever ;
|
|
70
|
+ # - sure, two hexadecimal characters is one byte long. Simply go for
|
|
71
|
+ # bit length, not chars length.
|
|
72
|
+ def generate_token(self):
|
|
73
|
+ token = binascii.hexlify(os.urandom(self.session_tokensize))
|
|
74
|
+ if LodelFileSystemSessionInterface.__sessions.get(token, None) is not None:
|
|
75
|
+ token = self.generate_token()
|
|
76
|
+ return token.decode('utf-8')
|