simplified minimal solution

This commit is contained in:
Tiziano Zito 2024-08-22 12:01:07 +02:00
parent fa7c99d620
commit 18c7e6b0ca

View file

@ -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
if username in pwdb:
raise Exception(f'Username already exists [{username}]!')
else:
pwdb[username] = input(f'Enter password for {username}: ') 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)
def main(pwdb_path):
# load the password database from file
pwdb = read_pwdb(pwdb_path) pwdb = read_pwdb(pwdb_path)
# if we are passed an argument, we want to add a new user
if len(sys.argv) > 1: if len(sys.argv) > 1:
pwdb = add_user(sys.argv[1], pwdb) pwdb = add_user(sys.argv[1], pwdb)
write_pwdb(pwdb, pwdb_path) write_pwdb(pwdb, pwdb_path)
return else:
# 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 else:
# try to authenticate
if authenticate(username, password, pwdb): if authenticate(username, password, pwdb):
print('Successfully authenticated!') print('Successfully authenticated!')
else: else:
# report wrong password
print('Wrong password!') print('Wrong password!')
return
if __name__ == '__main__':
main(PWDB_FLNAME)