|
35 | 35 | from PyMatcha.utils.tables import create_tables |
36 | 36 | from pymysql.cursors import DictCursor |
37 | 37 | from redis import StrictRedis |
38 | | -from sentry_sdk import configure_scope |
39 | 38 | from sentry_sdk.integrations.flask import FlaskIntegration |
40 | 39 |
|
41 | 40 |
|
|
127 | 126 |
|
128 | 127 | jwt = JWTManager(application) |
129 | 128 |
|
130 | | -logging.debug("Configuring JWT expired token handler callback") |
131 | | - |
132 | | - |
133 | | -@jwt.expired_token_loader |
134 | | -def expired_token_callback(expired_token): |
135 | | - logging.debug("Token {} expired".format(expired_token)) |
136 | | - resp = { |
137 | | - "code": 401, |
138 | | - "error": { |
139 | | - "message": f"The {expired_token['type']} token has expired", |
140 | | - "name": "Unauthorized Error", |
141 | | - "solution": "Try again when you have renewed your token", |
142 | | - "type": "UnauthorizedError", |
143 | | - }, |
144 | | - "success": False, |
145 | | - } |
146 | | - return jsonify(resp), 401 |
147 | | - |
148 | 129 |
|
149 | 130 | logging.debug("Configuring CORS") |
150 | 131 | CORS(application, expose_headers="Authorization", supports_credentials=True) |
@@ -183,39 +164,6 @@ def expired_token_callback(expired_token): |
183 | 164 |
|
184 | 165 | redis.flushdb() |
185 | 166 |
|
186 | | -from PyMatcha.models.user import get_user |
187 | | - |
188 | | - |
189 | | -logging.debug("Configuring JWT user callback loader") |
190 | | - |
191 | | - |
192 | | -from PyMatcha.utils.errors import NotFoundError |
193 | | - |
194 | | - |
195 | | -@jwt.user_loader_callback_loader |
196 | | -def jwt_user_callback(identity): |
197 | | - try: |
198 | | - user = get_user(identity["id"]) |
199 | | - except NotFoundError: |
200 | | - # The user who the server issues the token for was deleted in the db. |
201 | | - return None |
202 | | - |
203 | | - with configure_scope() as scope: |
204 | | - scope.user = {"email": user.email, "id": user.id, "username": user.username} |
205 | | - user.is_online = True |
206 | | - user.date_lastseen = datetime.datetime.utcnow() |
207 | | - user.save() |
208 | | - return user |
209 | | - |
210 | | - |
211 | | -@jwt.token_in_blacklist_loader |
212 | | -def check_if_token_is_revoked(decrypted_token): |
213 | | - jti = decrypted_token["jti"] |
214 | | - entry = redis.get("is_revoked_jti:" + jti) |
215 | | - if entry is None: |
216 | | - return True |
217 | | - return entry == "true" |
218 | | - |
219 | 167 |
|
220 | 168 | from PyMatcha.routes.api.user import user_bp |
221 | 169 | from PyMatcha.routes.api.auth.email import auth_email_bp |
@@ -257,66 +205,10 @@ def check_if_token_is_revoked(decrypted_token): |
257 | 205 | application.register_blueprint(debug_bp) |
258 | 206 |
|
259 | 207 |
|
260 | | -@jwt.revoked_token_loader |
261 | | -def jwt_revoked_token_callback(): |
262 | | - return ( |
263 | | - jsonify( |
264 | | - { |
265 | | - "code": 401, |
266 | | - "error": { |
267 | | - "message": "Token has been revoked.", |
268 | | - "name": "Unauthorized Error", |
269 | | - "solution": "Please login again", |
270 | | - "type": "UnauthorizedError", |
271 | | - }, |
272 | | - "success": False, |
273 | | - } |
274 | | - ), |
275 | | - 401, |
276 | | - ) |
277 | | - |
278 | | - |
279 | | -@jwt.unauthorized_loader |
280 | | -def no_jwt_callback(error_message): |
281 | | - return ( |
282 | | - jsonify( |
283 | | - { |
284 | | - "code": 401, |
285 | | - "error": { |
286 | | - "message": error_message, |
287 | | - "name": "Unauthorized Error", |
288 | | - "solution": "Try again", |
289 | | - "type": "UnauthorizedError", |
290 | | - }, |
291 | | - "success": False, |
292 | | - } |
293 | | - ), |
294 | | - 401, |
295 | | - ) |
296 | | - |
297 | | - |
298 | | -@jwt.invalid_token_loader |
299 | | -def jwt_invalid_token_callback(error_message): |
300 | | - return ( |
301 | | - jsonify( |
302 | | - { |
303 | | - "code": 400, |
304 | | - "error": { |
305 | | - "message": error_message, |
306 | | - "name": "Bad Request Error", |
307 | | - "solution": "Try again (The token is invalid)", |
308 | | - "type": "BadRequestError", |
309 | | - }, |
310 | | - "success": False, |
311 | | - } |
312 | | - ), |
313 | | - 400, |
314 | | - ) |
315 | | - |
316 | | - |
317 | 208 | # import tasks here to be registered by celery |
318 | 209 |
|
319 | 210 | import PyMatcha.utils.tasks # noqa |
| 211 | +import PyMatcha.utils.jwt_callbacks # noqa |
320 | 212 |
|
321 | 213 |
|
322 | 214 | @application.route("/") |
|
0 commit comments