From 18c7e6b0ca02b55ec836ccfd8161d3fa88270ac5 Mon Sep 17 00:00:00 2001 From: Tiziano Zito Date: Thu, 22 Aug 2024 12:01:07 +0200 Subject: [PATCH] simplified minimal solution --- minimal_auth.py | 70 ++++++++++++------------------------------------- 1 file changed, 17 insertions(+), 53 deletions(-) diff --git a/minimal_auth.py b/minimal_auth.py index b68dd09..67064e5 100644 --- a/minimal_auth.py +++ b/minimal_auth.py @@ -1,78 +1,42 @@ import json -import pathlib import sys -# name of the file where we store the pw database -PWDB_FLNAME = pathlib.Path('pwdb.json') - def get_credentials(): - # get input from terminal 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: ') return (username, password) def authenticate(username, password, pwdb): - # calculate hash and compare with stored hash return password == pwdb[username] 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 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: - with open(pwdb_path, 'rt') as pwdb_file: - pwdb = json.load(pwdb_file) - except json.decoder.JSONDecodeError as exc: - # this happens when the json data is invalid - 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}') + pwdb_file = open(pwdb_path, 'rt') + pwdb = json.load(pwdb_file) + except Exception: + pwdb = {} return pwdb def write_pwdb(pwdb, pwdb_path): - with open(pwdb_path, 'wt') as pwdb_file: - json.dump(pwdb, pwdb_file) + pwdb_file = open(pwdb_path, 'wt') + json.dump(pwdb, pwdb_file) -def initialize_pwdb(pwdb_path): - write_pwdb({}, pwdb_path) +pwdb_path = 'pwdb.json' +pwdb = read_pwdb(pwdb_path) -def main(pwdb_path): - # load the password database from file - pwdb = read_pwdb(pwdb_path) - - # 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 +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!') - return - - # try to authenticate - if authenticate(username, password, pwdb): - print('Successfully authenticated!') else: - # report wrong password - print('Wrong password!') - return - -if __name__ == '__main__': - main(PWDB_FLNAME) - + if authenticate(username, password, pwdb): + print('Successfully authenticated!') + else: + print('Wrong password!')