-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdatabase.py
More file actions
111 lines (90 loc) · 2.91 KB
/
Copy pathdatabase.py
File metadata and controls
111 lines (90 loc) · 2.91 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
# database
import dataset
import hashlib
import os
import codecs
import string
import random
## connecting to database
## make sure database = ali ##
# Add your own password for localhost
db = dataset.connect("mysql://root:@localhost/ali")
## Testing database connection ##
# Create a reference to table 'sessions'
users_table = db["users_table"]
companies = db["companies"]
chart_table = db["chart_table"]
sessions = db["sessions"]
## creates a company ID
def companyIdGenerator(size=4, uchars=string.digits):
return ''.join(random.choice(uchars) for _ in range(size))
#if at the very least, just have ALI in the database
if(companies.__len__()== 0):
companies.insert(
{
"company_id": "0001",
"company_name": "ALI",
"company_key": "123thjmv79cdfj3ki5tye",
}
)
#finds user through the username inside of database
def getUser(username):
try:
user = list(users_table.find(username=username))
username = user[0].get("username")
password = user[0].get("password")
company = user[0].get("company_name")
data = {"username": username, "password": password, "company_name": company}
return data
except: # if not found return empty Dictionary
data = {"username": '', "password": '', "company_name": ''}
return data
#saves user into database
def saveUser(data):
assert type(data) is dict
users_table.insert(
{
"username": data["username"],
"password": data["password"],
"company_name": data["company_name"]
}
)
return
#saves company into database
def saveCompany(data):
assert type(data) is dict
companies.insert(
{
"company_id": data["company_id"],
"company_name": data["company_name"],
"company_key": data["company_key"]
}
)
return
#checks to see if user is an Admin
def isAdmin(username):
if username == "admin":
return True
else:
return False
# function for hashing process
def bytesToString(byte):
string = str(
codecs.encode(byte, "hex"), "utf-8"
) # using utf-8 to change bytes to a string
assert type(string) is str # making sure its a string
return string
# function for hashing process
def stringToBytes(string):
byte = codecs.decode(bytes(string, "utf-8"), "hex") # changing from string to bytes
assert type(byte) is bytes # making sure its bytes
return byte
def generateCredentials(Userpassword):
salt = os.urandom(32)
key = hashlib.pbkdf2_hmac(
"sha256", # The hash digest algorithm for HMAC
Userpassword.encode("utf-8"), # Makes password byes
salt, # Provide the salt
100000, # It is recommended to use at least 100,000 iterations of SHA-256
)
return '{"salt": "' + str(bytesToString(salt)) + '", "key": "' + str(bytesToString(key)) + '"}'