-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.py
More file actions
257 lines (231 loc) · 8.73 KB
/
loader.py
File metadata and controls
257 lines (231 loc) · 8.73 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
from datetime import datetime, timezone, timedelta
from flask import jsonify, make_response, request, redirect, url_for, flash
from flask_jwt_extended import current_user, unset_jwt_cookies, unset_access_cookies, verify_jwt_in_request
from flask_jwt_extended.exceptions import NoAuthorizationError
from init import app, jwt, env, limiter
from models.user import RevokedTokens, User
class FlaskErrorLoaders:
@app.errorhandler(404)
def page_not_found(e):
if "api" in request.url:
return make_response(jsonify(
message="Resource not found."
), 404)
else:
flash(message="Resource not found.", category=404)
return redirect(url_for("main.error"))
@app.errorhandler(500)
def internal_server_error(e):
if "api" in request.url:
return make_response(jsonify(
message="Internal server error."
), 500)
else:
flash(message="Internal server error.", category=500)
return redirect(url_for("main.error"))
@app.errorhandler(405)
def method_not_allowed(e):
if "api" in request.url:
return make_response(jsonify(
message="Method not allowed."
), 405)
else:
flash(message="Method not allowed.", category=405)
return redirect(url_for("main.error"))
@app.errorhandler(400)
def bad_request(e):
if "api" in request.url:
return make_response(jsonify(
message="Bad request."
), 400)
else:
flash(message="Bad request.", category=400)
return redirect(url_for("main.error"))
@app.errorhandler(401)
def unauthorized(e):
if "api" in request.url:
return make_response(jsonify(
message=e
), 401)
else:
flash(message=e, category=401)
response = redirect(url_for("main.login"))
unset_jwt_cookies(response)
unset_access_cookies(response)
return response
@app.errorhandler(403)
def forbidden(e):
if "api" in request.url:
return make_response(jsonify(
message="Forbidden request."
), 403)
else:
flash(message="Forbidden request.", category=403)
return redirect(url_for("main.error"))
@app.errorhandler(422)
def unprocessable_entity(e):
if "api" in request.url:
return make_response(jsonify(
message="Unprocessable entity."
), 422)
else:
flash(message="Unprocessable entity.", category=422)
return redirect(url_for("main.error"))
@app.errorhandler(429)
def too_many_requests(e):
if "api" in request.url:
return make_response(jsonify(
message="Too many requests."
), 429)
else:
flash(message="Too many requests.", category=429)
return redirect(url_for("main.error"))
@app.errorhandler(503)
def service_unavailable(e):
if "api" in request.url:
return make_response(jsonify(
message="Service unavailable."
), 503)
else:
flash(message="Service unavailable.", category=503)
return redirect(url_for("main.error"))
@app.errorhandler(504)
def gateway_timeout(e):
if "api" in request.url:
return make_response(jsonify(
message="Gateway timeout."
), 504)
else:
flash(message="Gateway timeout.", category=504)
return redirect(url_for("main.error"))
@app.errorhandler(505)
def http_version_not_supported(e):
if "api" in request.url:
return make_response(jsonify(
message="HTTP version not supported."
), 505)
else:
flash(message="HTTP version not supported.", category=505)
return redirect(url_for("main.error"))
@app.errorhandler(415)
def unsupported_media_type(e):
if "api" in request.url:
return make_response(jsonify(
message="Unsupported media type."
), 415)
else:
flash(message="Unsupported media type.", category=415)
return redirect(url_for("main.error"))
@app.errorhandler(NoAuthorizationError)
def no_authorization_error(e):
if "api" in request.url:
return make_response(jsonify(
message="No authorization token found."
), 401)
else:
flash(message="No authorization token found.", category=401)
return redirect(url_for("main.error"))
class JWTErrorLoaders:
@jwt.expired_token_loader
def expired_token_callback(jwt_header, jwt_payload):
if "api" in request.url:
return jsonify({
"message": "Your credentials have expired",
}), 401
else:
flash(message="Your credentials have expired", category=401)
response = redirect(url_for("main.login"))
unset_jwt_cookies(response)
unset_access_cookies(response)
return response
@jwt.invalid_token_loader
def invalid_token_callback(error):
print(error)
if "api" in request.url:
return jsonify({
"message": "Invalid credentials",
}), 401
else:
flash(message="Invalid credentials", category=401)
response = redirect(url_for("main.login"))
unset_jwt_cookies(response)
unset_access_cookies(response)
return response
@jwt.unauthorized_loader
def unauthorized_loader(error):
if "api" in request.url:
return jsonify({
"message": "Session expired or unauthorized access"
}), 401
else:
flash(message="Session expired or unauthorized access", category=401)
response = redirect(url_for("main.login"))
unset_jwt_cookies(response)
unset_access_cookies(response)
return response
@jwt.needs_fresh_token_loader
def needs_fresh_token_loader():
if "api" in request.url:
return jsonify({
"message": "The credentials are not fresh"
}), 401
else:
flash(message="The credentials are not fresh", category=401)
response = redirect(url_for("main.login"))
unset_jwt_cookies(response)
unset_access_cookies(response)
return response
@jwt.revoked_token_loader
def revoked_token_loader():
if "api" in request.url:
return jsonify({
"message": "The credentials have been revoked"
}), 401
else:
flash(message="The credentials have been revoked", category=401)
response = redirect(url_for("main.login"))
unset_jwt_cookies(response)
unset_access_cookies(response)
return response
@jwt.user_lookup_error_loader
def user_loader_error_callback(jwt_header, jwt_payload):
if "api" in request.url:
return jsonify({
"message": "User not found"
}), 404
else:
flash(message="User not found", category=404)
response = redirect(url_for("main.login"))
unset_jwt_cookies(response)
unset_access_cookies(response)
return response
@jwt.token_in_blocklist_loader
def check_if_token_in_blocklist(jwt_header, jwt_payload):
return RevokedTokens.query.filter_by(jti=jwt_payload["jti"]).first()
@jwt.revoked_token_loader
def revoked_token_callback(jwt_header, jwt_payload):
if "api" in request.url:
return jsonify({
"message": "The token has been revoked"
}), 401
else:
flash(message="The token has been revoked", category=401)
return redirect(url_for("main.login"))
class JWTUserCallbacks:
@jwt.user_identity_loader
def user_identity_lookup(user):
return user.id
@jwt.user_lookup_loader
def user_lookup_callback(_jwt_header, jwt_data):
identity = jwt_data["sub"]
user = User.query.filter_by(id=identity).first()
if not user is None:
return user
return None
local_tz = timezone(timedelta(hours=int(env.get('TIMEZONE_OFFSET'))))
class TemplateFilters:
@app.template_filter("rfc822")
def rfc822(value):
if isinstance(value, datetime):
return value.astimezone(local_tz).strftime('%a, %d %b %Y %H:%M:%S %z')
return value