2020-01-19 08:07:45 +00:00
|
|
|
from functools import wraps
|
2020-03-09 18:54:51 +00:00
|
|
|
from .logger import getInfoLogger, getDebugLogger
|
|
|
|
DEBUG = getDebugLogger("login-decorator", True)
|
2020-01-19 08:07:45 +00:00
|
|
|
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))
|
2020-01-19 08:07:45 +00:00
|
|
|
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))
|
2020-01-19 08:07:45 +00:00
|
|
|
return func(*args, **kwargs)
|
|
|
|
else:
|
2020-03-09 18:54:51 +00:00
|
|
|
DEBUG.warning("token {{}} is not valid".format(token))
|
2020-01-19 08:07:45 +00:00
|
|
|
return jsonify({"error": "error", "message": "permission denied"}), 401
|
|
|
|
return wrapper
|
|
|
|
return real_decorator
|