return 401 on invalid token

This commit is contained in:
Itay Mizeretz 2018-02-22 15:43:51 +02:00
parent c9d644f88a
commit 28ef2d8708
1 changed files with 15 additions and 11 deletions

View File

@ -1,7 +1,7 @@
from functools import wraps from functools import wraps
import flask_jwt from flask import current_app, abort
from flask_jwt import JWT from flask_jwt import JWT, _jwt_required, JWTError
from werkzeug.security import safe_str_cmp from werkzeug.security import safe_str_cmp
from cc.island_config import AUTH_ENABLED from cc.island_config import AUTH_ENABLED
@ -43,12 +43,16 @@ def init_jwt(app):
def jwt_required(realm=None): def jwt_required(realm=None):
if AUTH_ENABLED: def wrapper(fn):
return flask_jwt.jwt_required(realm) @wraps(fn)
else: def decorator(*args, **kwargs):
def wrapper(fn): if AUTH_ENABLED:
@wraps(fn) try:
def decorator(*args, **kwargs): _jwt_required(realm or current_app.config['JWT_DEFAULT_REALM'])
return fn(*args, **kwargs) except JWTError:
return decorator abort(401)
return wrapper return fn(*args, **kwargs)
return decorator
return wrapper