From becc5fd8aae56731fc7b7e8e6033ad2d1fe7cf13 Mon Sep 17 00:00:00 2001 From: Tiziano Zito Date: Mon, 22 Sep 2025 13:18:08 +0300 Subject: [PATCH 1/5] add minimal implementation --- minimal_auth.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 minimal_auth.py diff --git a/minimal_auth.py b/minimal_auth.py new file mode 100644 index 0000000..67064e5 --- /dev/null +++ b/minimal_auth.py @@ -0,0 +1,42 @@ +import json +import sys + +def get_credentials(): + username = input('Enter your username: ') + password = input('Enter your password: ') + return (username, password) + +def authenticate(username, password, pwdb): + return password == pwdb[username] + +def add_user(username, pwdb): + pwdb[username] = input(f'Enter password for {username}: ') + return pwdb + +def read_pwdb(pwdb_path): + try: + 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') + json.dump(pwdb, pwdb_file) + +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) +else: + username, password = get_credentials() + if username not in pwdb: + print('Wrong username!') + else: + if authenticate(username, password, pwdb): + print('Successfully authenticated!') + else: + print('Wrong password!') From 18579b0af2c69e948a8f212131498df67bf2f70d Mon Sep 17 00:00:00 2001 From: Tiziano Zito Date: Mon, 22 Sep 2025 13:25:55 +0300 Subject: [PATCH 2/5] do not leak valid usernames --- minimal_auth.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/minimal_auth.py b/minimal_auth.py index 67064e5..8bdff7d 100644 --- a/minimal_auth.py +++ b/minimal_auth.py @@ -34,9 +34,9 @@ if len(sys.argv) > 1: else: username, password = get_credentials() if username not in pwdb: - print('Wrong username!') + print('Wrong username or password!') else: if authenticate(username, password, pwdb): print('Successfully authenticated!') else: - print('Wrong password!') + print('Wrong username or password!') From 0aadaff89df3a2e80537dde1d857b3b166068515 Mon Sep 17 00:00:00 2001 From: victoris93 Date: Mon, 22 Sep 2025 13:28:45 +0300 Subject: [PATCH 3/5] fixed that security problem --- minimal_auth.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/minimal_auth.py b/minimal_auth.py index 8bdff7d..1950121 100644 --- a/minimal_auth.py +++ b/minimal_auth.py @@ -33,10 +33,7 @@ if len(sys.argv) > 1: write_pwdb(pwdb, pwdb_path) else: username, password = get_credentials() - if username not in pwdb: + if username not in pwdb or not authenticate(username, password, pwdb): print('Wrong username or password!') else: - if authenticate(username, password, pwdb): - print('Successfully authenticated!') - else: - print('Wrong username or password!') + print('Successfully authenticated!') From 7548a834123579375d7a36ec381926594c6235bc Mon Sep 17 00:00:00 2001 From: victoris93 Date: Mon, 22 Sep 2025 15:15:42 +0300 Subject: [PATCH 4/5] fixed the password visibility problem --- minimal_auth.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/minimal_auth.py b/minimal_auth.py index 1950121..30fd41f 100644 --- a/minimal_auth.py +++ b/minimal_auth.py @@ -1,16 +1,17 @@ +import getpass import json import sys def get_credentials(): username = input('Enter your username: ') - password = input('Enter your password: ') + password = getpass.getpass('Enter your password: ') return (username, password) def authenticate(username, password, pwdb): return password == pwdb[username] def add_user(username, pwdb): - pwdb[username] = input(f'Enter password for {username}: ') + pwdb[username] = getpass.getpass(f'Enter password for {username}: ') return pwdb def read_pwdb(pwdb_path): From 0c28955b6ed9d1fe25b9a519e3ca7830b555cf39 Mon Sep 17 00:00:00 2001 From: victoris93 Date: Mon, 22 Sep 2025 15:28:03 +0300 Subject: [PATCH 5/5] added comment to explain what getpass does --- minimal_auth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minimal_auth.py b/minimal_auth.py index 30fd41f..9f535a7 100644 --- a/minimal_auth.py +++ b/minimal_auth.py @@ -1,4 +1,4 @@ -import getpass +import getpass # hides types characters, very useful import json import sys