-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
313 lines (237 loc) · 9.45 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import os, requests, json
from datetime import timedelta
from dotenv import load_dotenv
from forms import RegistrationForm, ArticleForm
from models import Article, User, db, migrate
from flask import Flask, abort, render_template, url_for, flash, redirect, request, session
from flask_login import LoginManager, current_user, login_required, login_user, logout_user
from flask_wtf.csrf import CSRFProtect
from passlib.hash import sha256_crypt
from oauthlib.oauth2 import WebApplicationClient
load_dotenv()
app = Flask(__name__)
csrf = CSRFProtect(app=app)
app.config['SECRET_KEY'] = os.getenv("SECRET_KEY")
app.config['WTF_CSRF_ENABLED'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv("DATABASE_URL")
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID", None)
GOOGLE_CLIENT_SECRET = os.getenv("CLIENT_SECRET", None)
GOOGLE_DISCOVERY_URL = (
"https://accounts.google.com/.well-known/openid-configuration"
)
login_manager = LoginManager()
login_manager.login_view = "login"
login_manager.login_message = (u"Unauthorized, please login")
login_manager.login_message_category = "danger"
login_manager.needs_refresh_message = (u"Session timedout, please re-login")
login_manager.needs_refresh_message_category = "info"
db.init_app(app)
migrate.init_app(app, db)
login_manager.init_app(app)
with app.app_context():
db.create_all()
client = WebApplicationClient(GOOGLE_CLIENT_ID)
c_username = None
@app.before_request
def before_request():
session.permanent = True
app.permanent_session_lifetime = timedelta(hours=24)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(user_id)
def get_google_provider_cfg():
return requests.get(GOOGLE_DISCOVERY_URL).json()
@app.route('/')
def home():
return render_template("home.html")
@app.route('/about')
def about():
return render_template("about.html")
@app.route('/register', methods=['GET', 'POST'])
def register():
if current_user.is_authenticated:
return redirect(url_for('dashboard'))
input_form = RegistrationForm(request.form)
if request.method == 'POST' and input_form.validate_on_submit():
uname = input_form.username.data
mail = input_form.email.data
pwd = sha256_crypt.encrypt(str(input_form.password.data))
user_exists = False
if User.query.filter_by(email=mail).first() or User.query.filter_by(username=uname).first():
user_exists = True
if not user_exists:
user = User(username=uname, email=mail, password=pwd)
db.session.add(user)
db.session.commit()
login_user(user)
flash("Registered successfully", 'success')
return redirect(url_for('dashboard'))
else:
flash("Email or username is taken!", "danger")
return redirect(url_for('register'))
return render_template("register.html", form=input_form)
@app.route("/login", methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('dashboard'))
if request.method == 'POST':
email = request.form['email']
password_candidate = request.form['password']
user_data = User.query.filter_by(email=email).first()
if user_data:
password = user_data.password
if user_data.password_is_correct(password_candidate):
app.logger.info("Password matched")
login_user(user=user_data)
session["username"] = current_user.username
global c_username
c_username = current_user.username
flash("You are now logged in", 'success')
return redirect(url_for('dashboard'))
else:
error = "Incorrect password"
return render_template("login.html", error=error)
else:
error = "User not found"
return render_template("login.html", error=error)
return render_template("login.html")
@app.route("/auth", methods=['POST'])
def auth():
google_provider_cfg = get_google_provider_cfg()
authorization_endpoint = google_provider_cfg['authorization_endpoint']
request_uri = client.prepare_request_uri(
authorization_endpoint,
redirect_uri=request.base_url + "/callback",
scope=["openid", "email", "profile"]
)
return redirect(request_uri)
@app.route("/auth/callback")
def callback():
code = request.args.get('code')
google_provider_cfg = get_google_provider_cfg()
token_endpoint = google_provider_cfg['token_endpoint']
token_url, headers, body = client.prepare_token_request(
token_endpoint,
authorization_response=request.url,
redirect_url=request.base_url,
code = code
)
token_response = requests.post(
url=token_url,
headers=headers,
data=body,
auth=(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET)
)
client.parse_request_body_response(json.dumps(token_response.json()))
user_info_endpoint = google_provider_cfg['userinfo_endpoint']
uri, headers, body = client.add_token(user_info_endpoint)
user_info_response = requests.get(url=uri, headers=headers, data=body)
response = user_info_response.json()
if response.get("email_verified"):
u_name = response['given_name']
u_mail = response['email']
u_gid = response['sub']
else:
flash("Email not verified, please try again...", "danger")
return redirect(url_for('login'))
user = User.query.filter_by(email=u_mail).first()
if user is None:
user = User(username=u_name, email=u_mail)
user.set_password(u_gid)
db.session.add(user)
db.session.commit()
login_user(user)
session["username"] = current_user.username
return redirect(url_for('dashboard'))
@app.route("/dashboard")
@login_required
def dashboard():
articles = current_user.articles
if len(articles) > 0:
return render_template("dashboard.html", user_articles=articles)
else:
msg = f"Oops. User: {current_user.username} does not have any article"
return render_template("dashboard.html", msg=msg, is_empty = True)
@app.route('/articles')
@login_required
def articles():
articles = current_user.articles
if len(articles) > 0:
return render_template("article.html", allArticles=articles)
else:
msg = f"Oops. User: {current_user.username} does not have any article"
return redirect(url_for("dashboard", msg=msg, is_empty = True))
@app.route('/articles/<int:id>')
@login_required
def article_page(id, username=c_username):
requested_article = Article.query.filter_by(id=id).first()
if requested_article.author_id != current_user.id:
flash("Don't be an intruder O_O", category="dark")
return redirect(url_for('dashboard'))
return render_template("article_page.html", article=requested_article)
@app.route("/<string:username>/add_article", methods=['GET', 'POST'])
@login_required
def add_article(username=c_username):
new_form = ArticleForm(request.form)
if request.method == 'POST' and new_form.validate_on_submit():
title = new_form.title.data
post_body = new_form.body.data
new_article = Article(title=title, body=post_body, author=current_user)
db.session.add(new_article)
db.session.commit()
flash("Article created", 'success')
return redirect(url_for("dashboard"))
return render_template("new_article.html", form=new_form)
@app.route("/edit_article/<string:article_id>", methods=['GET', 'POST'])
@login_required
def edit_article(article_id):
try:
article = Article.query.get_or_404(article_id)
except:
msg = "Sorry, that article does not exist."
flash(msg, category="info")
return redirect(url_for("dashboard"))
if article.author == current_user:
edit_form = ArticleForm(request.form)
edit_form.title.data = article.title
edit_form.body.data = article.body
if request.method == 'POST' and edit_form.validate_on_submit():
title = request.form['title']
post_body = request.form['body']
article.title = title
article.body = post_body
article.author = current_user
db.session.add(article)
db.session.commit()
flash("Article edited", 'success')
return redirect(url_for("dashboard"))
return render_template("edit_article.html", form=edit_form)
else:
flash("You are not permitted to do that...", 'danger')
return redirect(url_for("dashboard"))
@app.route("/delete_article/<int:id>", methods=['POST'])
@login_required
def delete_article(article_id):
try:
article = Article.query.get_or_404(article_id)
except:
msg = "Sorry, that article does not exist."
flash(msg, category="info")
return redirect(url_for("dashboard"))
if article.author == current_user:
db.session.delete(article)
db.session.commit()
flash("Article deleted", "success")
return redirect(url_for('dashboard'))
else:
flash("Unauthorized access", 'danger')
return redirect(url_for('dashboard'))
@app.route("/logout")
@login_required
def logout():
logout_user()
flash("Successfully signed out", 'success')
return redirect(url_for("login"))
if __name__ == "__main__":
app.run()