complete implementation of basic API
This commit is contained in:
parent
387430c2f6
commit
fa633f7d1a
42
auth.py
Normal file
42
auth.py
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def get_credentials():
|
||||||
|
username = input('Enter your username: ')
|
||||||
|
password = input('Enter your password: ')
|
||||||
|
return (username, password)
|
||||||
|
|
||||||
|
def authenticate(username, password, pwdb):
|
||||||
|
return password == pwdb[username]
|
||||||
|
|
||||||
|
def add_user(username, pwdb):
|
||||||
|
pwdb[username] = input(f'Enter password for {username}: ')
|
||||||
|
return pwdb
|
||||||
|
|
||||||
|
def read_pwdb(pwdb_path):
|
||||||
|
try:
|
||||||
|
pwdb_file = open(pwdb_path, 'rt')
|
||||||
|
pwdb = json.load(pwdb_file)
|
||||||
|
except Exception:
|
||||||
|
pwdb = {}
|
||||||
|
return pwdb
|
||||||
|
|
||||||
|
def write_pwdb(pwdb, pwdb_path):
|
||||||
|
pwdb_file = open(pwdb_path, 'wt')
|
||||||
|
json.dump(pwdb, pwdb_file)
|
||||||
|
|
||||||
|
pwdb_path = 'pwdb.json'
|
||||||
|
pwdb = read_pwdb(pwdb_path)
|
||||||
|
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
pwdb = add_user(sys.argv[1], pwdb)
|
||||||
|
write_pwdb(pwdb, pwdb_path)
|
||||||
|
else:
|
||||||
|
username, password = get_credentials()
|
||||||
|
if username not in pwdb:
|
||||||
|
print('Wrong username!')
|
||||||
|
else:
|
||||||
|
if authenticate(username, password, pwdb):
|
||||||
|
print('Successfully authenticated!')
|
||||||
|
else:
|
||||||
|
print('Wrong password!')
|
Loading…
Reference in a new issue