-
Notifications
You must be signed in to change notification settings - Fork 4
/
decorators.py
43 lines (37 loc) · 1.01 KB
/
decorators.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
import functools
import flask
import utils
import os
JWT_TOKEN_SECRET = os.getenv("JWT_TOKEN_SECRET")
'''
Auth decorator
@param {Function} decorated_function
'''
def auth_protected(decorated_function):
@functools.wraps(decorated_function)
def decorator(*args, **kwargs):
auth_header = flask.request.headers.get('Authorization', None)
if auth_header is None:
response = flask.json.jsonify({
"status": 400,
"message": "JWT Token required for this route"
})
response.status_code = 400
return response
try:
token = utils.auth.auth_token_decode(
JWT_TOKEN_SECRET,
utils.auth.authorization_header_token(
auth_header
)
)
setattr(flask.g, "jwt_token", token)
except (TypeError, ValueError) as e:
response = flask.json.jsonify({
"status": 400,
"message": str(e)
})
response.status_code = 400
return response
return decorated_function(*args, **kwargs)
return decorator