forked from ASPP/2023-heraklion-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
44 lines (32 loc) · 970 Bytes
/
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
import json
import getpass
import hashlib
PATH = "./pwdb.json"
def get_credentials():
username = input("Username: ")
password = getpass.getpass("Password: ")
return username, password
def read_pwdb():
with open(PATH, "rt") as f:
pwdb = json.load(f)
return pwdb
def write_pwdb(pwdb):
with open(PATH, "w") as f:
json.dump(pwdb, f)
def authenticate(username, password, pwdb):
if username in pwdb:
if pwhash(password) == pwdb[username]:
print("Successfully authenticated!")
else:
print("Wrong Password")
else:
pwdb = add_user(username, password, pwdb)
write_pwdb(pwdb)
def add_user(username, password, pwdb):
pwdb[username] = pwhash(password)
return pwdb
def pwhash(password: str) -> str:
return hashlib.sha256(password.encode("utf-8")).hexdigest()
username, password = get_credentials()
pwdb = read_pwdb()
authenticate(username, password, pwdb)