-
Notifications
You must be signed in to change notification settings - Fork 23
/
EnmodalRedis.py
118 lines (90 loc) · 3.85 KB
/
EnmodalRedis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import uuid
import os
import sys
import datetime
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'lib', 'transit')))
import Transit
import ConfigParser
config = ConfigParser.RawConfigParser()
config.read(os.path.abspath(os.path.join(os.path.dirname(__file__), 'settings.cfg')))
SESSIONS_HOST = config.get('sessions', 'host')
SESSIONS_PORT = config.get('sessions', 'port')
SESSIONS_DBNAME = config.get('sessions', 'dbname')
SESSIONS_USER = config.get('sessions', 'user')
SESSIONS_PASSWORD = config.get('sessions', 'password')
SESSIONS_CONN_STRING = "host='"+SESSIONS_HOST+"' port='"+SESSIONS_PORT+"' dbname='"+SESSIONS_DBNAME+"' user='"+SESSIONS_USER+"' password='"+SESSIONS_PASSWORD+"'"
SESSIONS_SECRET_KEY_PUBLIC = int(config.get('sessions', 'secret_key_public'), 16)
SESSIONS_SECRET_KEY_PRIVATE = int(config.get('sessions', 'secret_key_private'), 16)
SESSION_EXPIRATION_TIME = int(config.get('sessions', 'expiration_time'))
class EnmodalSessionManager(object):
def __init__(self):
self.sessions = []
def get_by_sid(self, sid):
return redis_store.get(str(sid))
def get_by_public_key(self, h):
sid = self.get_sid_from_public_key(h)
return redis_store.get(str(sid))
def get_by_private_key(self, h):
sid = self.get_sid_from_private_key(h)
return redis_store.get(str(sid))
def get_sid_from_public_key(self, h):
return h ^ SESSIONS_SECRET_KEY_PUBLIC
def get_sid_from_private_key(self, h):
return h ^ SESSIONS_SECRET_KEY_PRIVATE
def add(self, s):
self.sessions.append(s)
# Whenever we add a new session, check for old ones to remove.
purged = self.purge()
print str(len(self.sessions))+" active sessions, "+str(purged)+" purged."
def save(self, s):
def remove_by_sid(self, sid):
s = self.get_by_sid(sid)
if s is not None:
self.sessions.remove(s)
def auth_by_key(self, h):
public_session = self.get_by_public_key(h)
if public_session is not None:
public_session.keep_alive()
a = EnmodalSessionAuthentication(public_session, False)
return a
private_session = self.get_by_private_key(h)
if private_session is not None:
private_session.keep_alive()
a = EnmodalSessionAuthentication(private_session, True)
return a
return None
def purge(self):
num_sessions_start = len(self.sessions)
for session in self.sessions:
if session.is_expired():
save_session(session, None, False)
self.sessions = [x for x in self.sessions if not x.is_expired()]
return num_sessions_start - len(self.sessions)
class EnmodalSession(object):
def __init__(self):
self.sid = uuid.uuid4().int & (1<<63)-1
self.map = Transit.Map(0)
self.last_edit_time = datetime.datetime.now()
def public_key(self):
return self.sid ^ SESSIONS_SECRET_KEY_PUBLIC
def private_key(self):
return self.sid ^ SESSIONS_SECRET_KEY_PRIVATE
def keep_alive(self):
self.last_edit_time = datetime.datetime.now()
def is_expired(self):
return (datetime.datetime.now() - self.last_edit_time).total_seconds() > SESSION_EXPIRATION_TIME
class EnmodalSessionAuthentication(object):
def __init__(self, s, editable):
self.session = s
self.editable = editable
def returnable_key(self):
if self.editable:
return '{:16x}'.format(self.session.private_key())
else:
return '{:16x}'.format(self.session.public_key())
def check_for_session_errors(h):
if session_manager.auth_by_key(h) is None:
print("session auth problem with key "+str(h))
return json.dumps({"error": "Invalid session"})
return 0
session_manager = EnmodalSessionManager()