-
Notifications
You must be signed in to change notification settings - Fork 0
/
Authenticate.py
79 lines (58 loc) · 1.88 KB
/
Authenticate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Siddharth Gaikwad
# Software Development Fundamentals
# Authenticate Password 2
import json
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template("Options.html")
@app.route("/home")
def main():
option = request.values.get("opt")
if (option == "1" or option == "Login"):
return render_template("Login.html")
if (option == "2" or option == "Create New User Account"):
return render_template("New_User.html")
else:
return render_template("quit.html")
# checks the password for the given username
def database(username, password):
creditials = dict()
with open("database.json", "r") as f:
creditials = json.load(f)
if (creditials[str(username)] == str(password)):
return True
else:
return False
# Prompt user for the password
# Authenticates the password
@app.route("/login")
def login():
username = request.values.get("user")
password = request.values.get("pass")
if database(username, password) == True:
return render_template("secretbase.html", user=username)
else:
return render_template("wrongpass.html")
# Adds a new username and password to the dictionary
@app.route("/new_user")
def new_user():
username = request.values.get("user")
password = request.values.get("pass")
users = dict()
with open("database.json", "r") as f:
users = json.load(f)
n = 1
while n > 0:
if str(username in users) == 'True':
return render_template("try_again.html", user=username)
else:
n = 0
users[username] = password
with open("database.json", "w+") as f:
json.dump(users, f, indent=2)
f.close()
return render_template("created.html", user=username)
if __name__ == '__main__':
app.run()