-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsession_exp_auth.py
66 lines (61 loc) · 1.82 KB
/
session_exp_auth.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
#!/usr/bin/env python3
"""
Define SessionExpAuth class
"""
import os
from datetime import (
datetime,
timedelta
)
from .session_auth import SessionAuth
class SessionExpAuth(SessionAuth):
"""
Definition of class SessionExpAuth that adds an
expiration date to a Session ID
"""
def __init__(self):
"""
Initialize the class
"""
try:
duration = int(os.getenv('SESSION_DURATION'))
except Exception:
duration = 0
self.session_duration = duration
def create_session(self, user_id=None):
"""
Create a Session ID for a user_id
Args:
user_id (str): user id
"""
session_id = super().create_session(user_id)
if session_id is None:
return None
session_dictionary = {
"user_id": user_id,
"created_at": datetime.now()
}
self.user_id_by_session_id[session_id] = session_dictionary
return session_id
def user_id_for_session_id(self, session_id=None):
"""
Returns a user ID based on a session ID
Args:
session_id (str): session ID
Return:
user id or None if session_id is None or not a string
"""
if session_id is None:
return None
user_details = self.user_id_by_session_id.get(session_id)
if user_details is None:
return None
if "created_at" not in user_details.keys():
return None
if self.session_duration <= 0:
return user_details.get("user_id")
created_at = user_details.get("created_at")
allowed_window = created_at + timedelta(seconds=self.session_duration)
if allowed_window < datetime.now():
return None
return user_details.get("user_id")