browser-go-api/api/decorators.py

21 lines
682 B
Python
Raw Permalink Normal View History

2019-10-09 22:42:38 +00:00
from flask import Blueprint, request, jsonify, session, abort
2019-10-09 05:53:37 +00:00
import os
import jwt
def jwt_required():
def decorator(func):
def authorized(*args, **kwargs):
auth_header = request.headers.get('Authorization') or None
if auth_header:
2019-10-09 05:53:37 +00:00
auth_token = auth_header.split(" ")[1]
2019-10-24 23:11:27 +00:00
try:
if jwt.decode(auth_token, os.environ.get('SECRET_KEY')):
return func(*args, **kwargs)
else:
abort(401)
except:
2019-10-09 05:53:37 +00:00
abort(401)
else:
abort(401)
return authorized
return decorator