-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsession_db_auth.py
More file actions
59 lines (54 loc) · 1.57 KB
/
session_db_auth.py
File metadata and controls
59 lines (54 loc) · 1.57 KB
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
#!/usr/bin/env python3
"""
Define class SessionDButh
"""
from .session_exp_auth import SessionExpAuth
from models.user_session import UserSession
class SessionDBAuth(SessionExpAuth):
"""
Definition of SessionDBAuth class that persists session data
in a database
"""
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 not session_id:
return None
kw = {
"user_id": user_id,
"session_id": session_id
}
user = UserSession(**kw)
user.save()
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
"""
user_id = UserSession.search({"session_id": session_id})
if user_id:
return user_id
return None
def destroy_session(self, request=None):
"""
Destroy a UserSession instance based on a
Session ID from a request cookie
"""
if request is None:
return False
session_id = self.session_cookie(request)
if not session_id:
return False
user_session = UserSession.search({"session_id": session_id})
if user_session:
user_session[0].remove()
return True
return False