Merge pull request 'Hide password and capitalize global variables' (#2) from aitor/2024-heraklion-git:safe-psw into main

Reviewed-on: #2
This commit is contained in:
Tiziano Zito 2024-08-26 12:04:22 +02:00
commit b42ef06d91

19
auth.py
View file

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