<commit_before>
# Standard library imports
from functools import wraps
import json

# Django imports
from django.http import Http404, HttpResponse
from django.conf import settings

# Tastypie imports
from tastypie import fields
from tastypie.authentication import Authentication
from tastypie.authorization import ReadOnlyAuthorization, Authorization
from tastypie.exceptions import BadRequest
from tastypie.throttle import CacheThrottle

# External imports
from casexml.apps.case.models import CommCareCase
from corehq.apps.users.decorators import require_permission
from couchforms.models import XFormInstance

# CCHQ imports
from corehq.apps.domain.decorators import login_or_digest, domain_admin_required
from corehq.apps.groups.models import Group
from corehq.apps.users.models import CommCareUser, WebUser, Permissions

# API imports
from corehq.apps.api.serializers import CustomXMLSerializer, XFormInstanceSerializer
from corehq.apps.api.util import get_object_or_not_exist
from corehq.apps.api.resources import JsonResource, DomainSpecificResourceMixin


def api_auth(view_func):
    @wraps(view_func)
    def _inner(req, domain, *args, **kwargs):
        try:
            return view_func(req, domain, *args, **kwargs)
        except Http404:
            return HttpResponse(json.dumps({"error": "not authorized"}),
                                content_type="application/json",
                                status=401)
    return _inner


class LoginAndDomainAuthentication(Authentication):
    def is_authenticated(self, request, **kwargs):
        PASSED_AUTH = 'is_authenticated'

        @api_auth
        @login_or_digest
        def dummy(request, domain, **kwargs):
            return PASSED_AUTH

        if not kwargs.has_key('domain'):
            kwargs['domain'] = request.domain

        response = dummy(request, **kwargs)

        if response == PASSED_AUTH:
            return True
        else:
            return response


    def get_identifier(self, request):
        return request.couch_user.username


class RequirePermissionAuthentication(LoginAndDomainAuthentication):
    def __init__(self, permission, *args, **kwargs):
        super(RequirePermissionAuthentication, self).__init__(*args, **kwargs)
        self.permission = permission

    def is_authenticated(self, request, **kwargs):
        PASSED_AUTH = 'is_authenticated'

        @api_auth
        @require_permission(self.permission)
        @login_or_digest
        def dummy(request, domain, **kwargs):
            return PASSED_AUTH

        if not kwargs.has_key('domain'):
            kwargs['domain'] = request.domain

        response = dummy(request, **kwargs)
        if response == PASSED_AUTH:
            return True
        else:
            return response
<commit_msg>fix api authorization with digest auth<commit_after>
# Standard library imports
from functools import wraps
import json

# Django imports
from django.http import Http404, HttpResponse
from django.conf import settings

# Tastypie imports
from tastypie import fields
from tastypie.authentication import Authentication
from tastypie.authorization import ReadOnlyAuthorization, Authorization
from tastypie.exceptions import BadRequest
from tastypie.throttle import CacheThrottle

# External imports
from casexml.apps.case.models import CommCareCase
from corehq.apps.users.decorators import require_permission
from couchforms.models import XFormInstance

# CCHQ imports
from corehq.apps.domain.decorators import login_or_digest, domain_admin_required
from corehq.apps.groups.models import Group
from corehq.apps.users.models import CommCareUser, WebUser, Permissions

# API imports
from corehq.apps.api.serializers import CustomXMLSerializer, XFormInstanceSerializer
from corehq.apps.api.util import get_object_or_not_exist
from corehq.apps.api.resources import JsonResource, DomainSpecificResourceMixin


def api_auth(view_func):
    @wraps(view_func)
    def _inner(req, domain, *args, **kwargs):
        try:
            return view_func(req, domain, *args, **kwargs)
        except Http404:
            return HttpResponse(json.dumps({"error": "not authorized"}),
                                content_type="application/json",
                                status=401)
    return _inner


class LoginAndDomainAuthentication(Authentication):
    def is_authenticated(self, request, **kwargs):
        PASSED_AUTH = 'is_authenticated'

        @api_auth
        @login_or_digest
        def dummy(request, domain, **kwargs):
            return PASSED_AUTH

        if not kwargs.has_key('domain'):
            kwargs['domain'] = request.domain

        response = dummy(request, **kwargs)

        if response == PASSED_AUTH:
            return True
        else:
            return response


    def get_identifier(self, request):
        return request.couch_user.username


class RequirePermissionAuthentication(LoginAndDomainAuthentication):
    def __init__(self, permission, *args, **kwargs):
        super(RequirePermissionAuthentication, self).__init__(*args, **kwargs)
        self.permission = permission

    def is_authenticated(self, request, **kwargs):
        PASSED_AUTH = 'is_authenticated'

        @api_auth
        @require_permission(self.permission, login_decorator=login_or_digest)
        def dummy(request, domain, **kwargs):
            return PASSED_AUTH

        if not kwargs.has_key('domain'):
            kwargs['domain'] = request.domain

        response = dummy(request, **kwargs)
        if response == PASSED_AUTH:
            return True
        else:
            return response

{'fraction_non_alphanumeric': 0.07511150758251561, 'fraction_numerical': 0.0032114183764495987, 'mean_word_length': 3.246969696969697, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4533009', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1474, 'n_words': 395}