-
Notifications
You must be signed in to change notification settings - Fork 2
/
Server.py
executable file
·147 lines (123 loc) · 4.73 KB
/
Server.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
APP_PATH = os.path.realpath('.')
import sys
sys.path.append(os.path.join(APP_PATH,'bin'))
sys.path.append(os.path.join(APP_PATH,'db_libs'))
sys.path.append(os.path.join(APP_PATH,'db_libs/utils'))
from flask import Flask, render_template, request, redirect, make_response
from forms import LoginForm, RegisterForm, PreRegisterForm, AddExpenseForm
from utils import ExpenseManagerUtils
import hashlib
import connection
import os
from nocache import nocache
import em
app = Flask(__name__)
app.secret_key = "highlysecret"
app.config['TEMPLATES_AUTO_RELOAD'] = True
emutils = ExpenseManagerUtils.UtilsLib()
conn = connection.connection()
emapi = em.em()
@app.route('/')
def reroute():
return redirect('/login', code=302)
@app.route('/login')
def login():
email = request.cookies.get('loggedin')
if email and request.cookies.get('security_verify:'+email) == emutils.hash_of_hashpass(email):
return redirect('/home', code=302)
else:
form = LoginForm.LoginForm()
return render_template('login.html', form=form)
@app.route('/register', methods = ['POST'])
def register():
form = RegisterForm.RegisterForm()
email = request.form['email_of_user']
return render_template('register.html', form=form, email=email)
@app.route('/pre-register')
def pre_register():
form = PreRegisterForm.PreRegisterForm()
return render_template('pre-register.html', form=form)
@app.route('/validate-email',methods=['POST'])
def check_email_from_members():
email = request.form['email_of_user']
q = conn.q.check_email_exist.format(email=email)
res = conn.exec_query(q)
if len(res.fetchall()) > 0:
return redirect('/otp-generate', code=307)
else:
return redirect('/login', code=302)
pass
@app.route('/otp-generate', methods=['POST'])
def GenerateOTP():
email = request.form['email_of_user']
emutils.generate_otp_for_email(email)
return redirect('/register', code=307)
@app.route('/validate-reset', methods=['POST'])
def validate_and_set_password():
password = request.form['password']
confirm_pass = request.form['confirm']
email = request.form['email']
otp = request.form['otp']
if password==confirm_pass and emutils.validate_otp(email,otp):
hashed_pass = hashlib.sha256(password.encode('utf-8'))
hashed_password =hashed_pass.hexdigest()
q = conn.q.update_user_password.format(email=email,hashed_password=hashed_password)
emutils.clear_all_sessions(email)
res = conn.exec_script(q)
return redirect('/login', code=302)
else:
return redirect('/register', code=302)
@app.route('/validate-login', methods=['POST'])
def check_login():
email = request.form['email']
password = request.form['password']
hashed_password = hashlib.sha256(password.encode('utf-8')).hexdigest()
q = conn.q.validate_login.format(email=email,hashed_password=hashed_password)
res = conn.exec_query(q)
if len(res.fetchall()) > 0:
emutils.set_session(email, request.remote_addr)
resp_obj = make_response(redirect('/home', code=302))
resp_obj.set_cookie('loggedin',email)
resp_obj.set_cookie('security_verify:'+email,emutils.hash_of_hashpass(email))
return resp_obj
else:
return redirect('/login', code=302)
@app.route('/home')
def home():
user = request.cookies.get('loggedin')
if not security_feature():
return render_template('login.html', form=LoginForm.LoginForm())
form = AddExpenseForm.AddExpenseForm()
return render_template('home.html',user=user,form=form)
@app.route('/add-expense-controller',methods=['POST'])
def add_expense():
if not security_feature():
return render_template('login.html', form=LoginForm.LoginForm())
from_list = request.form.getlist('FromMultipleList')
to = request.form['ToSingleSelect']
exp_type = request.form['ExpenseType']
amount = request.form['AmountInput']
amt = float(amount)
comment = request.form['CommentInput']
if exp_type == 'SPLIT':
# Add as split
print(from_list)
emapi.split_add_internal_api(from_list, to,amt,comment)
elif exp_type == 'GROUP':
# Add as group
emapi.group_add_internal_api(from_list, to,amt,comment)
return redirect('/home',code=302)
# Utils Functions
def security_feature():
user = request.cookies.get('loggedin')
if not emutils.check_and_update_exist_session(user):
print("Fail check sessions")
return False
elif request.cookies.get('security_verify:'+user) != emutils.hash_of_hashpass(user):
print("Fail security_Verify : {}".format('security_verify:' + emutils.hash_of_hashpass(user)))
return False
else:
return True
if __name__ == "__main__":
app.run(debug=True)