-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
112 lines (88 loc) · 3.28 KB
/
app.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from logging import error, log
from flask import Flask, render_template, session, request, redirect, url_for, flash
import os
from functools import wraps
app = Flask(__name__)
import boto3
import botocore
client = boto3.client('cognito-idp')
ClientId = ''
UserPoolId = ''
app.secret_key = os.urandom(24)
def is_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('Error: Unauthorized, Please login')
return redirect(url_for('index'))
return wrap
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
try:
session['logged_in'] = True
session['username'] = username
response = client.initiate_auth(
ClientId = ClientId,
AuthFlow = "USER_PASSWORD_AUTH",
AuthParameters = {"USERNAME": username, "PASSWORD": password},
)
flash('Login Successful')
return redirect(url_for('home'))
except botocore.exceptions.ClientError as error:
flash('Error: Incorrect username or password')
return render_template('login.html')
@app.route('/signup', methods=['GET', 'POST'])
def singnup():
if request.method == 'POST':
username = request.form['username']
email = request.form['email']
password = request.form['password']
try:
response = client.sign_up(
ClientId = ClientId,
Username = username,
Password = password,
UserAttributes = [{"Name": "name", "Value": username}, {"Name": "email", "Value": email}]
)
if response['UserConfirmed'] == False:
return redirect(url_for('confirm'))
except botocore.exceptions.ClientError as error:
flash('Error: User already exist')
except botocore.exceptions.ParamValidationError as error:
flash('Error: Invalid length for parameter Password')
return render_template('signup.html')
@app.route('/confirm', methods=['GET', 'POST'])
def confirm():
flash('Check Email for validation code')
if request.method == 'POST':
username = request.form['username']
code = request.form['code']
try:
response = client.confirm_sign_up(
ClientId = ClientId,
Username = username,
ConfirmationCode = code,
ForceAliasCreation = False,
)
flash("Confirmation successfull. Please login")
return redirect(url_for('index'))
except botocore.exceptions.ClientError as error:
flash('Error: Please request a code again')
return render_template('confirm.html')
@app.route('/home')
@is_logged_in
def home():
return render_template('home.html')
@app.route('/logout')
@is_logged_in
def logout():
session.clear()
flash('Error: You are now logged out')
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000, debug=True)