-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
271 lines (234 loc) · 9.75 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
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
from flask import Flask, request, render_template, redirect, url_for, session, flash, jsonify
from databases import Mongo
import re
from hashlib import sha256
import encryption
from flask_wtf import CSRFProtect
app = Flask(__name__)
# Config
app.config['SECRET_KEY'] = 'mysecretkey'
csrf = CSRFProtect(app)
# Database
MONGO_URI = "mongodb://localhost:27017"
client = Mongo(MONGO_URI)
# Password strength checker
def password_strength(password):
score = 0
# Check length
if len(password) >= 8:
score += 1
if len(password) >= 12:
score += 1
# Check for presence of uppercase, lowercase, digits, and special characters
if re.search(r"[A-Z]", password):
score += 1
if re.search(r"[a-z]", password):
score += 1
if re.search(r"\d", password):
score += 1
if re.search(r"[!@#$%^&*()-+=]", password):
score += 1
# Determine password difficulty
if score <= 2:
return "Weak"
elif score <= 4:
return "Medium"
else:
return "Strong"
# Routes
@app.route('/', methods=['GET', 'POST'])
def home():
if 'user_id' not in session:
return redirect(url_for('login'))
user = client.get_user(session['user_id'])
data = client.get_data(session['user_id'])
return render_template('home.html', user=user, data=data)
# Login
@app.route('/login', methods=['GET', 'POST'])
def login():
if 'user_id' in session:
return redirect('/')
if request.method == 'POST':
email = request.form['email']
password = sha256(request.form['password'].encode()).hexdigest() # Hashing the password
db_user = client.get_user(email)
if db_user and db_user['password'] == password:
session['user_id'] = email
return redirect('/')
else:
flash('Invalid username or password', 'danger')
return render_template('login.html')
# Signup
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if 'user_id' in session:
return redirect('/')
if request.method == 'POST':
email = request.form['email']
email = request.form['email']
password = request.form['password']
confirm_password = request.form['confirm-password']
if password != confirm_password:
flash('Passwords do not match', 'danger')
return redirect('/signup')
elif len(password) < 8 or len(password) > 26:
flash('Password must be at least 8 characters long and maximum 26 characters', 'danger')
return redirect('/signup')
elif any(char.isdigit() for char in password) is False:
flash('Password must contain at least one number', 'danger')
return redirect('/signup')
elif any(char.isupper() for char in password) is False:
flash('Password must contain at least one uppercase letter', 'danger')
return redirect('/signup')
elif any(char.islower() for char in password) is False:
flash('Password must contain at least one lowercase letter', 'danger')
return redirect('/signup')
elif any(char.isspace() for char in password) is True:
flash('Password must not contain any spaces', 'danger')
return redirect('/signup')
if client.get_user(email):
flash('Email already exists', 'danger')
return redirect('/signup')
else:
# Generating Privete Key & Public Key
public_key, private_key = encryption.encode_key(password)
# Hashing the password
password_hash = sha256(password.encode()).hexdigest()
client.add_user(password_hash, email, public_key, private_key)
session['user_id'] = email
return redirect('/')
return render_template('signup.html')
# Add
@app.route('/add', methods=['GET', 'POST'])
def add():
if 'user_id' not in session:
return redirect(url_for('login'))
if request.method == 'POST':
website = request.form['website']
email = request.form['email']
password = request.form['password']
owner_id = session['user_id']
user_data = client.get_user(session['user_id'])
# Validation
data = client.get_data(owner_id)
if data:
for d in data:
if d['website'] == website and d['email'] == email:
flash('Email already exists', 'danger')
return redirect(url_for('add'))
encrypted_password = encryption.encode_data(password, user_data['public_key'])
difficulty = password_strength(password)
client.add_data(website, email, encrypted_password, owner_id, difficulty)
flash('Password added successfully', 'success')
return redirect('/')
return render_template('add.html')
# Edit
@app.route('/edit', methods=['GET', 'POST'])
def tedit():
if 'user_id' not in session:
return redirect(url_for('login'))
if request.method == 'POST':
id = request.form['id']
password = request.form['password']
user_data = client.get_user(session['user_id'])
if sha256(password.encode()).hexdigest() != user_data['password']:
flash('Invalid password', 'danger')
return redirect(f'/decrypt/{id}')
data = client.get_by_id(id)
decoded_key = encryption.decode_key(user_data['private_key'], password)
data['password'] = encryption.decode_data(data['password'], decoded_key)
flash('Password updated successfully', 'success')
return render_template('edit.html', data=data)
@app.route('/edit/<string:doc_id>', methods=['POST'])
def update(doc_id):
if 'user_id' not in session:
return redirect(url_for('login'))
if request.method == 'POST':
id = doc_id
password = request.form['password']
email = request.form['email']
website = request.form['website']
user_data = client.get_user(session['user_id'])
data = client.get_by_id(id)
data['password'] = encryption.encode_data(password, user_data['public_key'])
data['email'] = email
data['website'] = website
client.update_by_id(id, data)
flash('Password updated successfully', 'success')
return redirect('/')
# Decrypt
@app.route('/decrypt/<string:doc_id>', methods=['GET', 'POST'])
def decrypt(doc_id):
if 'user_id' not in session:
return jsonify({'message': 'Unauthorized'})
if request.method == 'POST':
id = doc_id
password = request.form['password']
user_data = client.get_user(session['user_id'])
if sha256(password.encode()).hexdigest() != user_data['password']:
return jsonify({'message': 'Invalid password'})
return redirect(url_for('edit',id=id))
return render_template('decrypt.html', id=doc_id)
# Delete
@app.route('/delete', methods=['POST'])
def delete():
if 'user_id' not in session:
return redirect(url_for('login'))
if request.method == 'POST':
id = request.form['id']
client.delete_data(id)
flash('Password deleted successfully', 'success')
return redirect('/')
# Settings
@app.route('/settings', methods=['GET', 'POST'])
def settings():
if 'user_id' not in session:
return redirect('/login')
if request.method == 'POST':
password = request.form['password']
new_password = request.form['new_password']
confirm_password = request.form['confirm_new_password']
user_data = client.get_user(session['user_id'])
if sha256(password.encode()).hexdigest() != user_data['password']:
flash('Invalid password', 'danger')
return redirect('/settings')
if new_password != confirm_password:
flash('Passwords do not match', 'danger')
return redirect('/settings')
elif len(new_password) < 8 or len(new_password) > 26:
flash('Password must be at least 8 characters long and maximum 26 characters', 'danger')
return redirect('/settings')
elif any(char.isdigit() for char in new_password) is False:
flash('Password must contain at least one number', 'danger')
return redirect('/settings')
elif any(char.isupper() for char in new_password) is False:
flash('Password must contain at least one uppercase letter', 'danger')
return redirect('/settings')
elif any(char.islower() for char in new_password) is False:
flash('Password must contain at least one lowercase letter', 'danger')
return redirect('/settings')
elif any(char.isspace() for char in new_password) is True:
flash('Password must not contain any spaces', 'danger')
return redirect('/settings')
elif new_password == password:
flash('New password cannot be the same as the old password', 'danger')
return redirect('/settings')
else:
private_key = encryption.decode_key(user_data['private_key'], password)
user_data['private_key'] = encryption.encode_key(new_password,private_key=private_key)[1]
user_data['password'] = sha256(new_password.encode()).hexdigest() # Hashing the new_password
client.update_user(session['user_id'], user_data)
flash('Password updated successfully', 'success')
return redirect('/')
return render_template('settings.html')
# Logout
@app.route('/logout')
def logout():
session.pop('user_id', None)
return redirect('/')
# Error handlers
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
if __name__ == '__main__':
app.run(debug=True)