Compare commits
6 commits
main
...
live_codin
Author | SHA1 | Date | |
---|---|---|---|
33469b8840 | |||
0c28955b6e | |||
7548a83412 | |||
0aadaff89d | |||
18579b0af2 | |||
becc5fd8aa |
1 changed files with 40 additions and 0 deletions
40
minimal_auth.py
Normal file
40
minimal_auth.py
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
import getpass # hides types characters, very useful
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def get_credentials():
|
||||||
|
username = input('Enter your username: ')
|
||||||
|
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] = getpass.getpass(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 or not authenticate(username, password, pwdb):
|
||||||
|
print('Wrong username or password!')
|
||||||
|
else:
|
||||||
|
print('Successfully authenticated!')
|
Loading…
Add table
Add a link
Reference in a new issue