-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
124 lines (93 loc) · 3.31 KB
/
main.py
File metadata and controls
124 lines (93 loc) · 3.31 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
115
116
117
118
119
120
121
122
123
124
# Run using $ source .venv/Scripts/activate
import bcrypt
import json
def exception_factory(exception, message):
return exception(message)
class Register():
def __init__(self, username="", password=""):
self.username = username
self.password = password
pass
def check_username_exists(self, filename="database.json"):
if self.username not in filename:
self.valid_username = True
return
else:
raise Exception("an error occurred")
def hash_salt_password(self):
password_bytes = self.password.encode('utf-8')
self.password = bcrypt.hashpw(password_bytes, bcrypt.gensalt())
self.password = self.password.decode()
def create_user(self, filename="database.json"):
user = {
"username": self.username,
"password": self.password,
}
with open(filename, 'r+') as file:
# Load existing data into a dictionary
file_data = json.load(file)
# Append new data to the list
file_data["user"].append(user)
# Move the cursor to the beginning of the file
file.seek(0)
# Write the updated data back to the file
json.dump(file_data, file, indent=4)
pass
# encrypt password
# check whether username exists
# validate password
# write username to db file
class Login(Register):
def __init__(self, username="", password="", username_match=False, password_verified=False):
self.username = username
self.password = password
pass
def check_password(self):
hashed = bcrypt.hashpw(self.password, bcrypt.gensalt())
if bcrypt.checkpw(self.password, hashed):
self.password_verified = True
return self.password_verified
else:
return self.password_verified
def check_username(self):
if self.check_username_exists(self):
self.username_match = True
return self.username_match
else:
return self.username_match
# encrypt username and password
# check whether username exists
# write username to db file
# Function to append new data to JSON file
def write_json(new_data, filename='database.json'):
with open(filename, 'r+') as file:
# Load existing data into a dictionary
file_data = json.load(file)
# Append new data to the list
file_data["user"].append(new_data)
# Move the cursor to the beginning of the file
file.seek(0)
# Write the updated data back to the file
json.dump(file_data, file, indent=4)
def read_json(filename='database.json'):
with open(filename, 'r') as file:
file_data = json.load(file)
json.dumps(file_data, indent=4)
pass
# New data to append
user = {
"username": "Nikhil",
"password": "pass",
}
def main():
# Call the function to append data
# write_json(user)
username = input(f"Username:")
password = input(f"Password:")
new_user = Register(username, password)
new_user.check_username_exists()
new_user.hash_salt_password()
new_user.create_user()
pass
if __name__ == "__main__":
main()