flaschengeist/geruecht/decorator.py

26 lines
1.1 KiB
Python
Raw Normal View History

from functools import wraps
2020-03-09 18:54:51 +00:00
from .logger import getInfoLogger, getDebugLogger
DEBUG = getDebugLogger("login-decorator", True)
def login_required(**kwargs):
import geruecht.controller.accesTokenController as ac
from geruecht.model import BAR, USER, MONEY, GASTRO
from flask import request, jsonify
accessController = ac.AccesTokenController()
groups = [USER, BAR, GASTRO, MONEY]
if "groups" in kwargs:
groups = kwargs["groups"]
def real_decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
token = request.headers.get('Token')
2020-03-09 18:54:51 +00:00
DEBUG.info("get token {{}}".format(token))
accToken = accessController.validateAccessToken(token, groups)
kwargs['accToken'] = accToken
if accToken:
2020-03-09 18:54:51 +00:00
DEBUG.info("token {{}} is valid".format(token))
return func(*args, **kwargs)
else:
2020-03-09 18:54:51 +00:00
DEBUG.warning("token {{}} is not valid".format(token))
return jsonify({"error": "error", "message": "permission denied"}), 401
return wrapper
return real_decorator