Hide password and capitalize global variables #2

Merged
otizonaizit merged 2 commits from aitor/2024-heraklion-git:safe-psw into main 2024-08-26 12:04:23 +02:00

19
auth.py
View file

@ -1,11 +1,12 @@
import json import json
import sys import sys
from getpass import getpass
pwdb_path = 'pwdb.json' PWDB_PATH = 'pwdb.json'
def get_credentials(): def get_credentials():
username = input('Enter your username: ') username = input('Enter your username: ')
password = input('Enter your password: ') password = getpass('Enter your password: ')
return (username, password) return (username, password)
def authenticate(username, password, pwdb): def authenticate(username, password, pwdb):
@ -15,26 +16,26 @@ def add_user(username, pwdb):
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: try:
pwdb_file = open(pwdb_path, 'rt') pwdb_file = open(PWDB_PATH, 'rt')
pwdb = json.load(pwdb_file) pwdb = json.load(pwdb_file)
except Exception: except Exception:
pwdb = {} pwdb = {}
return pwdb return pwdb
def write_pwdb(pwdb, pwdb_path): def write_pwdb(pwdb, PWDB_PATH):
pwdb_file = open(pwdb_path, 'wt') pwdb_file = open(PWDB_PATH, 'wt')
json.dump(pwdb, pwdb_file) json.dump(pwdb, pwdb_file)
if __name__ == "__main__": if __name__ == "__main__":
pwdb_path = 'pwdb.json' PWDB_PATH = 'pwdb.json'
pwdb = read_pwdb(pwdb_path) pwdb = read_pwdb(PWDB_PATH)
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)
else: else:
username, password = get_credentials() username, password = get_credentials()
if username not in pwdb: if username not in pwdb: