-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
51 lines (36 loc) · 909 Bytes
/
app.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
# Main app
from flask import Flask
app = Flask(__name__)
# CORS
from flask_cors import CORS
CORS(app, resources={r'/*': {'origins': '*'}})
# API
import json
from functools import wraps
import jwt
with open('keys.json', 'r') as file:
SECRET_KEY = json.loads(file.read())['jwt']
def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
try:
header = request.headers.get('Authorization')
token = header.split(' ')[1]
if not token:
return jsonify({'message': 'Token is missing!'}), 403
try:
data = jwt.decode(token, SECRET_KEY)
kwargs['data'] = data
except:
return jsonify({'message': 'Token is invalid!'}), 403
return f(*args, **kwargs)
except Exception as e:
print('ERR', e)
return f(*args, **kwargs)
return decorated
@app.route('/', methods=['POST'])
@token_required
def index(data={}):
x = request.json
print(x, data)
return jsonify(x)