from functools import wraps
from flask import request, jsonify, current_app

from .lever_jwt import jwt_decode_auth_token


def authenticate(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        response_object = {
            'status': 'fail',
            'message': 'Provide a valid auth token.'
        }
        auth_header = request.headers.get('Authorization')
        if not auth_header:
            return jsonify(response_object), 403
        auth_token = auth_header.split(" ")[1]
        resp = jwt_decode_auth_token(
            auth_token,
            current_app.config.get('PUBLIC_KEY')
        )
        if isinstance(resp, str):
            response_object['message'] = resp
            return jsonify(response_object), 401
        kwargs.update(dict(resp=resp))
        return f(*args, **kwargs)

    return decorated_function
