Added byte/str compatibility for python 3

This commit is contained in:
Jens Alm
2013-05-30 11:21:05 +02:00
parent bbfaf2c8d8
commit 4e81e5d5e8
3 changed files with 14 additions and 5 deletions

10
djrill/compat.py Normal file
View File

@@ -0,0 +1,10 @@
# For python 3 compatibility, see http://python3porting.com/problems.html#nicer-solutions
import sys
if sys.version < '3':
def b(x):
return x
else:
import codecs
def b(x):
return codecs.latin_1_encode(x)[0]

View File

@@ -7,6 +7,7 @@ from django.test import TestCase
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.conf import settings from django.conf import settings
from ..compat import b
from ..signals import webhook_event from ..signals import webhook_event
@@ -64,7 +65,7 @@ class DjrillWebhookSignatureMixinTests(TestCase):
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
def test_signature(self): def test_signature(self):
signature = hmac.new(key=settings.DJRILL_WEBHOOK_SIGNATURE_KEY, msg = settings.DJRILL_WEBHOOK_URL+"mandrill_events[]", digestmod=hashlib.sha1) signature = hmac.new(key=b(settings.DJRILL_WEBHOOK_SIGNATURE_KEY), msg = b(settings.DJRILL_WEBHOOK_URL+"mandrill_events[]"), digestmod=hashlib.sha1)
hash_string = b64encode(signature.digest()) hash_string = b64encode(signature.digest())
response = self.client.post('/webhook/?secret=abc123', data={"mandrill_events":"[]"}, **{"X-Mandrill-Signature" : hash_string}) response = self.client.post('/webhook/?secret=abc123', data={"mandrill_events":"[]"}, **{"X-Mandrill-Signature" : hash_string})
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)

View File

@@ -2,12 +2,10 @@ from base64 import b64encode
import hashlib import hashlib
import hmac import hmac
import json import json
from django import forms from django import forms
from django.conf import settings from django.conf import settings
from django.contrib import messages from django.contrib import messages
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
from django.views.generic import TemplateView, View from django.views.generic import TemplateView, View
from django.http import HttpResponse from django.http import HttpResponse
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
@@ -16,7 +14,7 @@ from django.views.decorators.csrf import csrf_exempt
import requests import requests
from djrill import MANDRILL_API_URL, signals from djrill import MANDRILL_API_URL, signals
from .compat import b
class DjrillAdminMedia(object): class DjrillAdminMedia(object):
def _media(self): def _media(self):
@@ -131,7 +129,7 @@ class DjrillWebhookSignatureMixin(object):
for item in value_list[1]: for item in value_list[1]:
post_string += "%s%s" % (value_list[0],item) post_string += "%s%s" % (value_list[0],item)
hash_string = b64encode(hmac.new(key=signature_key, msg=post_string, digestmod=hashlib.sha1).digest()) hash_string = b64encode(hmac.new(key=b(signature_key), msg=b(post_string), digestmod=hashlib.sha1).digest())
if signature != hash_string: if signature != hash_string:
return HttpResponse(status=403, content="Signature doesn't match") return HttpResponse(status=403, content="Signature doesn't match")