simplified solution
This commit is contained in:
parent
7e2039734e
commit
efcb6ae933
29
auth.py
29
auth.py
|
@ -39,19 +39,11 @@ def add_user(username, pwdb):
|
||||||
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():
|
if not pwdb_path.exists():
|
||||||
initialize_pwdb(pwdb_path)
|
pwdb = {}
|
||||||
try:
|
else:
|
||||||
with open(pwdb_path, 'rt') as pwdb_file:
|
with open(pwdb_path, 'rt') as pwdb_file:
|
||||||
pwdb = json.load(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}')
|
|
||||||
return pwdb
|
return pwdb
|
||||||
|
|
||||||
def write_pwdb(pwdb, pwdb_path):
|
def write_pwdb(pwdb, pwdb_path):
|
||||||
|
@ -72,9 +64,6 @@ def get_salt():
|
||||||
salt_chars = random.choices(CHARS, k=SALT_LENGTH)
|
salt_chars = random.choices(CHARS, k=SALT_LENGTH)
|
||||||
return ''.join(salt_chars)
|
return ''.join(salt_chars)
|
||||||
|
|
||||||
def initialize_pwdb(pwdb_path):
|
|
||||||
write_pwdb({}, pwdb_path)
|
|
||||||
|
|
||||||
def main(pwdb_path):
|
def main(pwdb_path):
|
||||||
# load the password database from file
|
# load the password database from file
|
||||||
pwdb = read_pwdb(pwdb_path)
|
pwdb = read_pwdb(pwdb_path)
|
||||||
|
@ -87,17 +76,11 @@ def main(pwdb_path):
|
||||||
|
|
||||||
# ask for credentials
|
# ask for credentials
|
||||||
username, password = get_credentials()
|
username, password = get_credentials()
|
||||||
|
if username not in pwdb or not authenticate(username, password, pwdb):
|
||||||
if username not in pwdb:
|
print('Wrong username or password!')
|
||||||
print('Wrong username!')
|
|
||||||
return
|
|
||||||
|
|
||||||
# try to authenticate
|
|
||||||
if authenticate(username, password, pwdb):
|
|
||||||
print('Successfully authenticated!')
|
|
||||||
else:
|
else:
|
||||||
# report wrong password
|
print('Successfully authenticated!')
|
||||||
print('Wrong password!')
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue