import logging

from django.conf import settings
from django.http import HttpResponse, HttpResponseNotAllowed, HttpResponseBadRequest
from django.views.decorators.csrf import csrf_exempt

from djzendesk.signals import target_callback_received


@csrf_exempt
def callback(request, ticket_id):
    """Handle HTTP callback requests from Zendesk"""

    # Require POST. Anything less is uncivilized.
    if not request.method == 'POST':
        return HttpResponseNotAllowed(['GET'])

    # Extract the message
    if not 'message' in request.POST:
        return HttpResponseBadRequest()

    # TODO: check HTTP basic auth

    message = request.POST['message']

    logging.info("HTTP callback received from Zendesk for ticket %s: %s", ticket_id, message)

    # Fire the signal to notify listeners of received target callback
    target_callback_received.send(sender=__name__, ticket_id=ticket_id, message=message)

    return HttpResponse('OK')

@@ -1,12 +1,26 @@
+import base64%0A
 import loggi
@@ -140,16 +140,39 @@
 dRequest
+, HttpResponseForbidden
 %0Afrom dj
@@ -275,16 +275,624 @@
 eived%0A%0A%0A
+def is_authenticated(request, username, password):%0A    %22%22%22Authenticate the request using HTTP Basic authorization%22%22%22%0A    authenticated = False%0A    if 'HTTP_AUTHORIZATION' in request.META:%0A        print request.META%5B'HTTP_AUTHORIZATION'%5D%0A        auth = request.META%5B'HTTP_AUTHORIZATION'%5D.split()%0A        if len(auth) == 2:%0A            if auth%5B0%5D.lower() == %22basic%22:%0A                provided_username, provided_password = base64.b64decode(auth%5B1%5D).split(':')%0A                if username == provided_username and password == provided_password:%0A                    authenticated = True%0A    return authenticated%0A%0A
 @csrf_ex
@@ -1017,15 +1017,21 @@
 ing 
-less is
+else would be
  unc
@@ -1121,16 +1121,381 @@
 d(%5B'
-GE
+POS
 T'%5D)%0A%0A
+    username = getattr(settings, 'ZENDESK_CALLBACK_USERNAME', None)%0A    password = getattr(settings, 'ZENDESK_CALLBACK_PASSWORD', None)%0A%0A    # Authenticate the request if credentials have been configured%0A    if username is not None and password is not None:%0A        if not is_authenticated(request, username, password):%0A            return HttpResponseForbidden()%0A%0A
     
@@ -1599,43 +1599,8 @@
 ()%0A%0A
-    # TODO: check HTTP basic auth%0A%0A
     

Add HTTP Basic authentication