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
Showing only changes of commit 54201439cb - Show all commits

16
auth.py
View file

@ -2,7 +2,7 @@ import json
import sys import sys
from getpass import getpass 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: ')
@ -16,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: