<commit_before>import json

from django.http import HttpResponse

from rest_framework_auth0.settings import auth0_api_settings


def json_response(response_dict, status=200):
    response = HttpResponse(json.dumps(response_dict), content_type="application/json", status=status)
    response['Access-Control-Allow-Origin'] = '*'
    response['Access-Control-Allow-Headers'] = 'Content-Type, Authorization'
    return response


"""
TODO: Verify if the token is valid and not expired(need to decode before verify)
"""

from functools import wraps
from rest_framework_auth0.authentication import jwt_decode_handler
from rest_framework_auth0.utils import get_jwt_value, get_roles_from_payload

class token_required(object):

    def __init__(self, view_func):
        self.view_func = view_func
        wraps(view_func)(self)

    def __call__(self, request, *args, **kwargs):
        # maybe do something before the view_func call
        # print(request.method)
        # print ("----hello")

        if request.method == 'OPTIONS':
            return func(request, *args, **kwargs)

        auth_header = request.META.get('HTTP_AUTHORIZATION', None)

        if auth_header is not None:
            tokens = auth_header.split(' ')

            if len(tokens) == 2 and tokens[0] == auth0_api_settings.JWT_AUTH_HEADER_PREFIX :
                token = tokens[1]
                #get called view
                response = self.view_func(request, *args, **kwargs)
            else:
                response = json_response({"msg":"Not valid token"}, status=401)

        # maybe do something after the view_func call
        # print ("----bye")
        return response

class is_authenticated(object):

    def __init__(self, view_func):
        self.view_func = view_func
        wraps(view_func)(self)

    def __call__(self, request, *args, **kwargs):
        # maybe do something before the view_func call
        # print(request.method)
        # print ("----hello")

        if request.method == 'OPTIONS':
            return func(request, *args, **kwargs)

        if request.user.is_authenticated():
            #get called view
            response = self.view_func(request, *args, **kwargs)
        else:
            response = json_response({"msg":"Not authenticated"}, status=401)
<commit_msg>Return response when token is missing<commit_after>import json

from django.http import HttpResponse

from rest_framework_auth0.settings import auth0_api_settings


def json_response(response_dict, status=200):
    response = HttpResponse(json.dumps(response_dict), content_type="application/json", status=status)
    response['Access-Control-Allow-Origin'] = '*'
    response['Access-Control-Allow-Headers'] = 'Content-Type, Authorization'
    return response


"""
TODO: Verify if the token is valid and not expired(need to decode before verify)
"""

from functools import wraps
from rest_framework_auth0.authentication import jwt_decode_handler
from rest_framework_auth0.utils import get_jwt_value, get_roles_from_payload

class token_required(object):

    def __init__(self, view_func):
        self.view_func = view_func
        wraps(view_func)(self)

    def __call__(self, request, *args, **kwargs):
        # maybe do something before the view_func call
        # print(request.method)
        # print ("----hello")

        if request.method == 'OPTIONS':
            return func(request, *args, **kwargs)

        auth_header = request.META.get('HTTP_AUTHORIZATION', None)

        if auth_header is not None:
            tokens = auth_header.split(' ')

            if len(tokens) == 2 and tokens[0] == auth0_api_settings.JWT_AUTH_HEADER_PREFIX :
                token = tokens[1]
                #get called view
                response = self.view_func(request, *args, **kwargs)
            else:
                response = json_response({"msg":"Not valid token"}, status=401)
        else:
            response = json_response({"msg": "Missing token"}, status=401)

        # maybe do something after the view_func call
        # print ("----bye")
        return response

class is_authenticated(object):

    def __init__(self, view_func):
        self.view_func = view_func
        wraps(view_func)(self)

    def __call__(self, request, *args, **kwargs):
        # maybe do something before the view_func call
        # print(request.method)
        # print ("----hello")

        if request.method == 'OPTIONS':
            return func(request, *args, **kwargs)

        if request.user.is_authenticated():
            #get called view
            response = self.view_func(request, *args, **kwargs)
        else:
            response = json_response({"msg":"Not authenticated"}, status=401)

{'fraction_non_alphanumeric': 0.10672607680882665, 'fraction_numerical': 0.007850625928283471, 'mean_word_length': 2.835638730675346, 'pattern_counts': {'":': 5, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4992303', 'n_tokens_mistral': 1361, 'n_tokens_neox': 1304, 'n_words': 343}