-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.py
More file actions
114 lines (102 loc) · 2.78 KB
/
Token.py
File metadata and controls
114 lines (102 loc) · 2.78 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
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
import os
from Pass import Pass
import time
import random
import hashlib
from replit import db
#TODO: make more secure by storing an encrypted uuid for eatch token
class TokenStore:
"""
A class to handle tokens
"""
def __init__(self, filename, token_length=10, token_time=30, name=""):
self.viewname = name
self.name = filename # Filename for token store
self.token_time = token_time # Valid token time (in minutes)
self.tl = token_length # Length of tokens in characters
self.data = {}
if not os.path.exists(self.name):
with open(self.name, "w") as e:
e.write("{}")
def _timestamp(self):
"""
Returns a tuple that includes the time information needed for tokens.
"""
cur = time.gmtime()
form = (cur.tm_yday, cur.tm_min, cur.tm_hour)
return form
def generate_token(self):
"""
Generates a token, registers it, and returns it.
"""
token_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890=+-_()*!@#.><[]{}|"
token = ""
for i in range(self.tl):
token = token + random.choice(token_chars)
self.write(token)
return token
def remove_token(self, token, encrypt=True):
"""
Tries to remove a token
"""
if encrypt:
tok = hashlib.md5(token.encode()).hexdigest()
else:
tok = token
try:
del self.data[tok]
self.save()
return True
except:
try:
del self.data[token]
return True
except:
return False
def save(self):
"""
Write the data to the save file
"""
with open(self.name, "w") as e:
e.write(str(self.data))
def write(self, token):
"""
Register the token and write data to save file
"""
token = hashlib.md5(
token.encode()).hexdigest() # Encrypt token for storage
token = str(token).replace("b'", "") #GEt rid of bytes identifier
token = token.replace("'", "")
self.data[f'{token}'] = self._timestamp()
self.save()
def check(self, token, encrypt=True):
"""
Checks if a token has expired. True if the token is valid, False if not.
"""
if encrypt:
token = hashlib.md5(
token.encode()).hexdigest() # Encrypt token for comparing
token = str(token).replace("b'", "") # Get rid of bytes identifier
token = token.replace("'", "")
cur = self._timestamp()
result = True
if token in self.data:
if self.data[token][0] != cur[0]:
result = False
elif self.data[token][2] != cur[2]:
result = False
elif cur[1] >= self.data[token][1] + self.token_time:
result = False
else:
result = False
return result
def read(self):
"""
Read the savefile data.
"""
with open(self.name, "r") as e:
try:
b = eval(e.read())
except:
b = {}
self.data = b