forked from IMS-workshop/git-lecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.py
53 lines (42 loc) · 1.36 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
46
47
48
49
50
51
52
import getpass
import pickle
import sys
def get_credentials():
username = input('Enter your username: ')
password = getpass.getpass('Enter your password: ')
return username, password
def authenticate(username, password, pwdb):
if username in pwdb:
if password == pwdb[username]:
return True
return False
def read_pwdb(pwdb_file):
pwdb_file.seek(0)
pwdb = pickle.load(pwdb_file)
return pwdb
def write_pwdb(pwdb, pwdb_file):
pwdb_file.seek(0)
pickle.dump(pwdb, pwdb_file)
def add_user(username, password, pwdb):
pwdb[username] = password
return pwdb
if __name__ == '__main__':
DEFAULT_PWDB = 'pwdb.pkl'
try:
pwdb_file = open(DEFAULT_PWDB, 'rb+')
except FileNotFoundError:
pwdb_file = open(DEFAULT_PWDB, 'wb')
pickle.dump({}, pwdb_file)
pwdb_file.close()
print('Created empty pw database!')
sys.exit(0)
username, password = get_credentials()
pwdb = read_pwdb(pwdb_file)
if authenticate(username, password, pwdb):
print('Successfull authentication', username, password)
else:
ans = input('User not known or password is wrong. Do you want to add the '
'user to the password database? [y/n]')
if ans == 'y':
add_user(username, password, pwdb)
write_pwdb(pwdb, pwdb_file)