-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
285 lines (237 loc) · 10.1 KB
/
auth.py
File metadata and controls
285 lines (237 loc) · 10.1 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from flask import Blueprint, jsonify, session, request, render_template, render_template_string, url_for, redirect
from models import User, Verification, db
from flask_mail import Mail, Message
import random
import string
import requests
import re
from datetime import datetime
mail = Mail()
def is_valid_email(email):
# Regular expression for validating an email address
email_regex = r'^[\w\.-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9.-]+$'
return re.match(email_regex, email) is not None
def delete_verification_record(email):
verification_record = Verification.query.filter_by(email=email).first()
if verification_record:
db.session.delete(verification_record)
db.session.commit()
return True
return False
def generate_random_string(length=6):
"""Generate a random alphanumeric lowercase string of given length."""
characters = string.ascii_lowercase + string.digits
return ''.join(random.choice(characters) for _ in range(length))
auth = Blueprint("auth", __name__, static_folder="static", template_folder="templates")
@auth.route('/create', methods=['GET', 'POST'])
def show_ar_add_users():
if request.method == 'GET':
if session['role'] == 1:
all_users = User.query.all()
users_data = [{'id': user.id, 'name': user.name, 'email': user.email, 'contact': user.contact, 'role': user.role_id} for user in all_users]
if request.headers.get('accept') == 'application/json':
return jsonify(users_data), 201
else:
return render_template('all_users.html', users=users_data)
else:
response_data = {
"success": "False",
"message": "Update Failed: Method Not Allowed"
}
return jsonify(response_data), 500
if request.method == 'POST':
# if 1 in session['permissions']:
data = request.json
email = data.get('email')
password = data.get('password')
password2 = data.get('password2')
# name = data.get('')
# contact = data.get('contact')
# password = "12345678"
new_user = User(email=email, password=password, role_id=0)
existing_user = User.query.filter_by(email=new_user.email).first()
if existing_user:
response_data = {
"success": "False",
"message": "There's already an account registered with this email"
}
return jsonify(response_data), 500
elif password != password2:
response_data = {
"success": "False",
"message": "Passwords didn't match"
}
return jsonify(response_data), 500
else:
endpoint2_url = url_for('auth.send_mail', _external=True)
response = requests.post(endpoint2_url, json={'email': email})
if response.status_code // 100 == 2:
data = response.json()
code = data.get('code')
otp = Verification.query.filter_by(email=email).first()
if otp:
otp.update_code(code, datetime.now())
db.session.commit()
else:
otp = Verification(email, code, datetime.now())
db.session.add(otp)
db.session.commit()
# otp = Verification(email, code, datetime.now())
# db.session.add(otp)
db.session.add(new_user)
db.session.commit()
response_data = {
"success": "True",
"message": "Registration Success: Please Check Email for verification code"
}
return jsonify(response_data), 201
else:
return jsonify({"success": False, "message": "Failed to send email"}), response.status_code
else:
response_data = {
"success": "False",
"message": "Registration Failed: Method Not Allowed"
}
return jsonify(response_data), 500
@auth.route('/login', methods=['POST'])
def login():
data = request.json
email = data.get('email')
password = data.get('password')
print(email)
print(password)
user = User.query.filter_by(email=email).first()
if user and user.check_password(password):
# if user:
if user.verified:
session['email'] = user.email
session['role'] = user.role_id
session['id'] = user.id
# role = Role.query.filter_by(id=user.role_id).first() # Changed role_id to id
print('logged IN')
return jsonify({"success": True, "message": "Successfully logged In"}), 201
else:
return jsonify({"success": True, "message": "Need to verify account"}), 202
else:
return jsonify({"success": False, "message": "Email and password didn't match"}), 401
@auth.route('/mail', methods=['POST'])
def send_mail():
data = request.json
email = data.get('email')
# print(session['email'])
msg_title = "Verification mail"
sender = "noreply@app.com"
msg = Message(msg_title, sender=sender, recipients=[email])
msg.body = ""
code = generate_random_string()
msg.html = render_template("verificationemail.html", code=code)
try:
mail.send(msg)
return jsonify({"success": True, "code": code}), 201
except Exception as e:
print(e)
return jsonify({"success": False, "message": "Email could not be sent"}), 400
@auth.route('/change-password', methods=['POST'])
def change_pass():
data = request.json
old_pass = data.get('old_pass')
new_pass1 = data.get('new_pass1')
new_pass2 = data.get('new_pass2')
print(old_pass)
print(new_pass2)
print(new_pass1)
if 'email' in session:
user = User.query.filter_by(email=session['email']).first()
if user and user.check_password(old_pass):
if len(new_pass1) < 8:
return jsonify({"success": False, "message": "Password needs to be of 8 Characters or more"}), 405
elif new_pass1 == new_pass2:
user.update_password(new_pass1)
db.session.commit()
return jsonify({"success": True, "message": "Password updated successfully"}), 201
else:
return jsonify({"success": False, "message": "Passwords didn't match"}), 401
else:
return jsonify({"success": False, "message": "Incorrect Password"}), 403
else:
error_message1 = "You Need to log In first"
return jsonify({"success": True, "message": error_message1}), 405
@auth.route('/reset-password/initiate', methods=['POST'])
def reset_pass_ini():
data = request.json
email = data.get('email')
user = User.query.filter_by(email=email).first()
if user:
# session['email'] = email
session['temp_mail'] = email
endpoint2_url = url_for('auth.send_mail', _external=True)
response = requests.post(endpoint2_url, json={'email': email})
# Check if the request was successful (status code 2xx)
if response.status_code // 100 == 2:
# Extract relevant data from the response
response_data = response.json()
code = response_data.get('code')
otp = Verification.query.filter_by(email=email).first()
if otp:
otp.update_code(code, datetime.now())
db.session.commit()
else:
otp = Verification(email, code, datetime.now())
db.session.add(otp)
db.session.commit()
return jsonify(response_data), response.status_code
else:
return jsonify({"success": False, "message": "Failed to send email"}), response.status_code
else:
return jsonify({"success": False, "message": "User not found"}), 404
@auth.route('/reset-password/confirm', methods=['POST'])
def reset_pass_confirm():
data = request.json
email = data.get('email')
code = data.get('code')
new_password = data.get('new_password')
print(email)
otp = Verification.query.filter_by(email=email).first()
user = User.query.filter_by(email=email).first()
if otp and user:
if otp.code == code:
user.update_password(new_password)
return jsonify({"success": True, "message": "Password Updated"}), 201
else:
return jsonify({"success": False, "message": "Verification code didn't match"}), 401
else:
return jsonify({"success": False, "message": "Invalid email"}), 402
@auth.route('/login-verification', methods=['POST'])
def login_verification():
data = request.json
email = data.get('email')
code = data.get('code')
otp = Verification.query.filter_by(email=email).first()
user = User.query.filter_by(email=email).first()
if otp and user:
if otp.code == code:
user.verified = True
db.session.commit()
session['email'] = user.email
session['role'] = user.role_id
session['id'] = user.id
# role = Role.query.filter_by(id=user.role_id).first()
# session['permissions'] = role.get_permissions()
return jsonify({"success": True, "message": "User Verified"}), 201
else:
return jsonify({"success": False, "message": "Verification code didn't match"}), 401
else:
return jsonify({"success": False, "message": "Invalid email"}), 402
@auth.route('/logout')
def logout():
if 'email' in session:
session.pop('email', None)
session.pop('role', None)
session.pop('id', None)
session.pop('permissions', None)
if request.headers.get('accept') == 'application/json':
return jsonify({"success": True, "message": "Successfully logged Out"}), 201
else:
return render_template('index.html')
else:
return jsonify({"success": False, "message": "Need to login first"}), 400