-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.py
101 lines (88 loc) · 2.78 KB
/
User.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
from time import time
import datetime
import jwt
import os
class User:
def __init__(self, id, username, password, role, email):
self.id = id
self.username = username
self.password = password
self.role = role
self.email = email
class UserActions():
userlist = []
admin_user = User(1, 'admin', 'a', 'admin', '[email protected]')
userlist.append(admin_user)
normal_user = User(2, 'user', 'a', 'user', '[email protected]')
userlist.append(normal_user)
SECRET_KEY = 'D\xde@\xe4\x1ch/\xa8\xed\xd3\xc6\x98?D\xbb\xb8Bo\xc0\xb7\xc6\xc7\x92\x19'
@classmethod
def add(cls, user):
cls.userlist.append(user)
print(cls.userlist)
@classmethod
def get(cls, id):
for user in cls.userlist:
if user.id == id:
return user
return None
@classmethod
def getUser(cls, username, password):
# print(cls.userlist)
for user in cls.userlist:
if user.username == username and user.password == password:
return user
return None
@classmethod
def encode_auth_token(cls, user_id):
"""
Generate the auth token
:return: string
"""
try:
payload = {
'exp': datetime.datetime.utcnow() + datetime.timedelta(days=0, seconds=500),
'iat': datetime.datetime.utcnow(),
'sub': user_id
}
return jwt.encode(
payload,
cls.SECRET_KEY,
algorithm='HS256'
)
except Exception as e:
return e
@classmethod
def decode_auth_token(cls, auth_token):
"""
Decodes the auth token
:param auth_token:
:return: integer|string
"""
print('Decoding auth token')
payload = jwt.decode(auth_token, cls.SECRET_KEY)
is_blacklisted_token = BlacklistToken.check_blacklist(auth_token)
if is_blacklisted_token:
print('token is blacklisted!')
raise Exception('token blacklisted. Please log in again')
return payload['sub']
class BlacklistToken:
"""
Token Model for storing JWT tokens
"""
blacklisted_tokens = []
def __init__(self, token):
self.id = int(time())
self.token = token
self.blacklisted_on = datetime.datetime.now()
def __repr__(self):
return '<id: token: {}'.format(self.token)
@classmethod
def check_blacklist(cls, auth_token):
# check whether auth token has been blacklisted
print(cls.blacklisted_tokens)
print('Checking for auth token if blacklisted')
for token in cls.blacklisted_tokens:
if token.token == auth_token:
return True
return False