-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
184 lines (151 loc) · 5.22 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import os
import json
import requests
import urllib
import hashlib
import base64
from flask import Flask, render_template, session, request, abort
from flask import redirect, escape, Response, flash
from flask import jsonify
from flask_login import LoginManager, UserMixin, login_required, login_user
from flask_login import logout_user, current_user
from modules import *
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
APP_STATIC = os.path.join(APP_ROOT, 'static')
app = Flask(__name__,
template_folder='template',
static_url_path="/static",
static_folder="static")
app.secret_key = '[PUT_HERE_LONG_AND_RANDOM_STRING___HOW_LONG_?___REALLY]'
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "/user"
def base64encode(s):
return (base64.b64encode(str.encode(s))).decode("utf-8").replace("=", "")
def base64decode(s):
while len(s) % 3 != 2:
s += "="
return base64.b64decode(str.encode(s)).decode("utf-8")
def encPass(p):
hash = hashlib.sha512()
hash.update(('%s' % p).encode('utf-8'))
return hash.hexdigest()
class User(UserMixin):
pass
@login_manager.user_loader
def user_loader(email):
if email is None or email == "":
return
# cursor.execute("insert into people values (?, ?)", (who, age))
c = db.query_db("select * from zx_users where email =(?)", ([email]))
if not len(c) == 1:
return
user = User()
user.id = c[0][1] # email
user.role = c[0][3] # role
return user
#
#
# @login_manager.request_loader
# def request_loader(request):
# email = request.form.get('email')
# if email is None or email == "":
# return
# quary = "select * from zx_users where email ='" + email + "'"
# print(quary)
# c = db.query_db(quary)
# print("request_loader")
# for row in c:
# print(row)
# if email not in users:
# return
#
# user = User()
# user.id = email
#
# # DO NOT ever store passwords in plaintext and always compare password
# # hashes using constant-time comparison!
# user.is_authenticated = request.form['password'] == users[email]['password']
#
# return user
@app.route("/")
@login_required
def main():
return render_template('index.html')
@app.route("/domain", methods=["GET", "POST"])
@login_required
def domain():
email = current_user.id
c = db.query_db("select * from zx_users where email =(?)", ([email]))
d = db.query_db("select * from domains where userid =(?)", ([c[0][0]]))
if request.method == 'POST':
domain = request.form['domain']
notify_email = request.form['notify_email']
q = "select 1 from domains where userid=(?) and domain=(?)"
e = db.query_db(q, (c[0][0], domain))
if len(e) >= 1:
return jsonify({"error": "Duplicated domain"})
db.query_db("insert into domains (userid,domain,notify_email) "
"values (?, ?, ?)",
(c[0][0], domain, notify_email))
return jsonify({"error": ""})
if len(d) == 0:
return jsonify({})
data = {}
data['domains'] = []
for row in d:
data['domains'].append(row[1])
return jsonify(data)
@app.route("/user", methods=['GET', 'POST'])
def user():
if request.method == 'POST':
email = request.form['email']
c = db.query_db("select * from zx_users where email =(?)", ([email]))
if len(c) == 1:
h = encPass(request.form['password'])
if h == c[0][2]:
user = User()
user.id = email
login_user(user)
return redirect('/')
return render_template('user.html', notLoggedin='true',
error='Wrong email or password')
elif request.method == 'GET':
return render_template('user.html', notLoggedin='true')
else:
abort(400)
@app.route("/user/logout", methods=['GET', 'POST'])
def logout():
logout_user()
return redirect('/user')
@app.route("/user/management", methods=['GET', 'POST'])
@login_required
def management():
if current_user.role != 'admin':
return redirect('/')
if request.method == 'POST':
email = base64encode(request.form['email'])
return render_template('userManagement.html',
invite=(email))
return render_template('userManagement.html')
@app.route("/user/invite/<id>", methods=['GET', 'POST'])
def invite(id):
email = base64decode(id)
c = db.query_db("select * from zx_users where email =(?)", ([email]))
if len(c) == 1:
return render_template('user.html', error='Error.\n' +
'If you have an acount try to login\n' +
'If not ask for another invitaion link')
if request.method == 'POST':
password = request.form['password']
db.query_db("insert into zx_users (EMAIL,PASSWORD,ROLE) "
"values (?, ?, ?)",
(email, encPass(password), 'user'))
user = User()
user.id = email
login_user(user)
return redirect('/')
return render_template('newUser.html', email=email)
@app.teardown_appcontext
def close_connection(exception):
db.close_connection