Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a file and authenticate if user exists #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import getpass
import os

PATH = "./pwdb.json"

Expand All @@ -8,31 +9,41 @@ def get_credentials():
password = getpass.getpass("Password: ")
return username, password

def read_pwdb():
with open(PATH, "rt") as f:
pwdb = json.load(f)
def read_pwdb(username, password):
if os.path.exists(PATH):
pwdb={}
with open(PATH, "rt") as f:
pwdb = json.load(f)
authenticate(username, password, pwdb)
else:
pwdb=add_user(username, password, pwdb={})
write_pwdb(pwdb)
print("First user added!")
return pwdb


def write_pwdb(pwdb):
with open(PATH, "w") as f:
json.dump(pwdb, f)
f.write(json.dumps(pwdb))
f.write("\n")
f.close()


def authenticate(username, password, pwdb):
if username in pwdb:
if password == pwdb[username]:
print("Successfully authenticated!")
else:
print("Wrong Password")
print("Wrong password")
else:
pwdb = add_user(username, password, pwdb)
write_pwdb(pwdb)
print("New user added")

def add_user(username, password, pwdb):
pwdb[username] = password
return pwdb

username, password = get_credentials()
pwdb = read_pwdb()
authenticate(username, password, pwdb)
pwdb = read_pwdb(username, password)