simplified minimal solution
This commit is contained in:
parent
fa7c99d620
commit
18c7e6b0ca
|
@ -1,78 +1,42 @@
|
||||||
import json
|
import json
|
||||||
import pathlib
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
# name of the file where we store the pw database
|
|
||||||
PWDB_FLNAME = pathlib.Path('pwdb.json')
|
|
||||||
|
|
||||||
def get_credentials():
|
def get_credentials():
|
||||||
# get input from terminal
|
|
||||||
username = input('Enter your username: ')
|
username = input('Enter your username: ')
|
||||||
# get password using the appropriate module, so that typed characters are not
|
|
||||||
# echoed to the terminal
|
|
||||||
password = input('Enter your password: ')
|
password = input('Enter your password: ')
|
||||||
return (username, password)
|
return (username, password)
|
||||||
|
|
||||||
def authenticate(username, password, pwdb):
|
def authenticate(username, password, pwdb):
|
||||||
# calculate hash and compare with stored hash
|
|
||||||
return password == pwdb[username]
|
return password == pwdb[username]
|
||||||
|
|
||||||
def add_user(username, pwdb):
|
def add_user(username, pwdb):
|
||||||
# do not try to add a username twice
|
pwdb[username] = input(f'Enter password for {username}: ')
|
||||||
if username in pwdb:
|
|
||||||
raise Exception(f'Username already exists [{username}]!')
|
|
||||||
else:
|
|
||||||
pwdb[username] = input(f'Enter password for {username}: ')
|
|
||||||
return pwdb
|
return pwdb
|
||||||
|
|
||||||
def read_pwdb(pwdb_path):
|
def read_pwdb(pwdb_path):
|
||||||
# try to read from the database
|
|
||||||
# if anything happens, report the error!
|
|
||||||
if not pwdb_path.exists():
|
|
||||||
initialize_pwdb(pwdb_path)
|
|
||||||
try:
|
try:
|
||||||
with open(pwdb_path, 'rt') as pwdb_file:
|
pwdb_file = open(pwdb_path, 'rt')
|
||||||
pwdb = json.load(pwdb_file)
|
pwdb = json.load(pwdb_file)
|
||||||
except json.decoder.JSONDecodeError as exc:
|
except Exception:
|
||||||
# this happens when the json data is invalid
|
pwdb = {}
|
||||||
raise Exception(f'Invalid database {pwdb_path}: {exc}')
|
|
||||||
except Exception as exc:
|
|
||||||
# this is a catch-all condition
|
|
||||||
raise Exception(f'Unkown error reading {pwdb_path}: {exc}')
|
|
||||||
return pwdb
|
return pwdb
|
||||||
|
|
||||||
def write_pwdb(pwdb, pwdb_path):
|
def write_pwdb(pwdb, pwdb_path):
|
||||||
with open(pwdb_path, 'wt') as pwdb_file:
|
pwdb_file = open(pwdb_path, 'wt')
|
||||||
json.dump(pwdb, pwdb_file)
|
json.dump(pwdb, pwdb_file)
|
||||||
|
|
||||||
def initialize_pwdb(pwdb_path):
|
pwdb_path = 'pwdb.json'
|
||||||
write_pwdb({}, pwdb_path)
|
pwdb = read_pwdb(pwdb_path)
|
||||||
|
|
||||||
def main(pwdb_path):
|
if len(sys.argv) > 1:
|
||||||
# load the password database from file
|
pwdb = add_user(sys.argv[1], pwdb)
|
||||||
pwdb = read_pwdb(pwdb_path)
|
write_pwdb(pwdb, pwdb_path)
|
||||||
|
else:
|
||||||
# if we are passed an argument, we want to add a new user
|
|
||||||
if len(sys.argv) > 1:
|
|
||||||
pwdb = add_user(sys.argv[1], pwdb)
|
|
||||||
write_pwdb(pwdb, pwdb_path)
|
|
||||||
return
|
|
||||||
|
|
||||||
# ask for credentials
|
|
||||||
username, password = get_credentials()
|
username, password = get_credentials()
|
||||||
|
|
||||||
if username not in pwdb:
|
if username not in pwdb:
|
||||||
print('Wrong username!')
|
print('Wrong username!')
|
||||||
return
|
|
||||||
|
|
||||||
# try to authenticate
|
|
||||||
if authenticate(username, password, pwdb):
|
|
||||||
print('Successfully authenticated!')
|
|
||||||
else:
|
else:
|
||||||
# report wrong password
|
if authenticate(username, password, pwdb):
|
||||||
print('Wrong password!')
|
print('Successfully authenticated!')
|
||||||
return
|
else:
|
||||||
|
print('Wrong password!')
|
||||||
if __name__ == '__main__':
|
|
||||||
main(PWDB_FLNAME)
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue