-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
34 lines (28 loc) · 1.11 KB
/
helpers.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
from functools import wraps
import secrets
from flask import request, jsonify, json
import decimal
from models import User
def token_required(our_flask_function):
@wraps(our_flask_function)
def decorated(*args, **kwargs):
token = None
if 'x-access-token' in request.headers:
token = request.headers['x-access-token'].split(' ')[1]
if not token:
return jsonify({'message' : 'Token is missing'}), 401
try:
current_user_token = User.query.filter_by(token=token).first()
print(token)
print(current_user_token)
except:
owner = User.query.filter_by(token=token).first()
if token != owner.token and secrets.compare_digest(token, owner.token):
return jsonify({'message' : 'Token is invalid'}), 401
return our_flask_function(current_user_token, *args, **kwargs)
return decorated
class JSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, decimal.Decimal):
return str(obj)
return super(JSONEncoder, self).default(obj)