from functools import wraps 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') DEBUG.info("get token {{}}".format(token)) accToken = accessController.validateAccessToken(token, groups) kwargs['accToken'] = accToken if accToken: DEBUG.info("token {{}} is valid".format(token)) return func(*args, **kwargs) else: DEBUG.warning("token {{}} is not valid".format(token)) return jsonify({"error": "error", "message": "permission denied"}), 401 return wrapper return real_decorator