-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathauth.py
45 lines (35 loc) · 1.1 KB
/
auth.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
import getpass
import json
def get_credentials():
username = input('Enter your username: ')
password = getpass.getpass('Enter your password: ')
return username, password
def authenticate(username, password, pwdb):
status = False
if username in pwdb:
if password == pwdb[username]:
status = True
else:
ans = input(f"Would you like to add a new user with this name {username}? (y/n)")
if ans == "y":
pwdb = add_user(pwdb, username, password, "pwdb.json")
return status
def read_pwdb(path):
with open(path, 'rt') as pwdb_file:
pwdb = json.load(pwdb_file)
return pwdb
def write_pwdb(pwdb, path):
with open(path, 'wt') as pwdb_file:
json.dump(pwdb, pwdb_file)
def add_user(pwdb, username, password, path):
pwdb[username] = password
write_pwdb(pwdb, 'pwdb.json')
return pwdb
if __name__ == "__main__":
username, password = get_credentials()
pwdb = read_pwdb('pwdb.json')
status = authenticate(username, password, pwdb)
if status:
print("success")
else:
print("failed")