-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
237 lines (168 loc) · 6.92 KB
/
app.py
File metadata and controls
237 lines (168 loc) · 6.92 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
import os
from flask import Flask, flash, redirect, render_template, request, session, url_for
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from db.connection import *
from helpers.directory import *
from helpers.fileUploadRestrictions import *
from helpers.passwordPolicies import *
app = Flask(__name__)
# strong secret key => prevent brute-forcing & cookies attacks
app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY")
# rate limiter =>
# to prevent brute-forcing attack on specific routes that
# attacker can attack on it
limiter = Limiter(
get_remote_address, app=app, default_limits=["200 per day", "50 per hour"]
)
@app.route("/")
def home():
if "username" in session:
return render_template("index.html", username=session["username"])
flash("You're not logged in!", "danger")
return redirect(url_for("login"))
@app.route("/login", methods=["POST", "GET"])
@limiter.limit("5 per minute")
def login():
if request.method == "GET":
return render_template("login.html")
else:
username = request.form["username"]
password = request.form["password"]
user = get_user_by_username(username)
if user:
if is_password_matched(password, user[3]):
session["username"] = user[1]
session["user_id"] = user[0]
return redirect(url_for("home"))
flash("Invalid Credentials!", "danger")
return render_template("login.html")
@app.route("/sign-up", methods=["GET", "POST"])
def register():
if request.method == "GET":
return render_template("register.html")
else:
username = request.form["username"]
password = request.form["password1"]
email = request.form["email"]
confirmPassword = request.form["password2"]
user = get_user_by_username(username)
if user:
flash("Username already exists!", "danger")
return render_template("register.html")
if password != confirmPassword:
flash("Password doesn't match!!", "warning")
return render_template("register.html")
if check_password_policies(password):
add_user(username, password, email)
flash("Username is registered Successfully!", "success")
else:
flash("Your password is weak try another strong one!", "warning")
return render_template("register.html")
return redirect(url_for("login"))
@app.route("/upload-note", methods=["GET", "POST"])
def UploadNote():
if request.method == "POST":
category = request.form["category"]
image = request.files["image"]
content = request.form["content"]
user_id = session["user_id"]
if image:
if not allowed_file_size(image) or not allowed_file_extension(
image.filename
):
flash("Invalid Image uploaded!!", "danger")
return render_template("UploadNote.html")
image_url = f"uploads/{image.filename}"
image.save(f"static/{image_url}")
add_note(user_id, category, content, image_url)
flash("Your Note Uploaded Successfully ", "success")
return redirect(url_for("MyNotes"))
add_note(user_id, category, content)
flash("Your Note Uploaded Successfully ", "success")
return redirect(url_for("MyNotes"))
else:
if not "user_id" in session:
flash("Please Login to do this action!", "danger")
return redirect(url_for("login"))
if not get_purchases_by_user_id(session["user_id"]) and session["user_id"] != 1:
flash("Please purchase a plan to add your notes!", "danger")
return redirect(url_for("PlansPage"))
return render_template("UploadNote.html")
@app.route("/my-notes")
def MyNotes():
if "user_id" in session:
return render_template("notes.html", notes=get_all_notes(session["user_id"]))
flash("You're not logged in!", "danger")
return redirect(url_for("login"))
@app.route("/search", methods=["GET", "POST"])
def search():
if request.method == "POST":
title = request.form["job_name"]
user_id = session["user_id"]
if title:
# solve IDOR to access specific user_id with his notes
return render_template(
"notes.html", notes=get_note_by_title(user_id, title)
)
flash("Search value is empty!!", "danger")
return redirect(url_for("MyNotes"))
else:
if not "user_id" in session:
return redirect(url_for("login"))
return redirect(url_for("home"))
@app.route("/plans")
def PlansPage():
if "username" in session:
return render_template("plans.html", plans=get_all_plans())
flash("You're not logged in!")
return redirect(url_for("login"))
# user & admin hierarchy
@app.route("/admin/add-new-plan", methods=["POST", "GET"])
def AddNewPlan():
# Idor Solution
if session["username"] == "admin" and session["user_id"] == 1:
if request.method == "POST":
title = request.form["title"]
price = request.form["price"]
description = request.form["description"]
add_plan(title, price, description)
flash("New plan Added Successfully!", "success")
return render_template("add_plan.html")
return render_template("add_plan.html")
flash("You're not allowed to take this action!")
return redirect(url_for("home"))
@app.route("/buy-plan/<plan_id>", methods=["POST", "GET"])
def buy_plan(plan_id):
user_id = session["user_id"]
if "user_id" not in session:
flash("You're not logged in!", "danger")
return redirect(url_for("login"))
if request.method == "GET":
if not get_plan_by_id(plan_id):
flash("There is no plan with this id", "danger")
return redirect(url_for("PlansPage"))
users_purchases=get_purchases(plan_id,user_id)
flash(len(users_purchases))
if len(users_purchases):
flash("You've already purchased this plan!", "danger")
return redirect(url_for("PlansPage"))
else:
return render_template("buy_plan.html", plan_id=plan_id)
else:
users_purchases = get_purchases(plan_id,user_id)
flash(len(users_purchases))
if len(users_purchases):
flash(f"You've already purchased this plan!", "danger")
return redirect(url_for("PlansPage"))
else:
add_purchase(user_id, plan_id)
plan_data = get_plan_by_id(plan_id)
flash(f"Successfully purchased by {plan_data[2]}$!", "success")
return redirect(url_for("PlansPage"))
if __name__ == "__main__":
if not is_directory_exist("static/uploads"):
create_directory("static/uploads")
init_db()
create_admin()
app.run(debug=True)