8000 Merge pull request #181 from hwayne/fix-python3-validator · madCode/twilio-python@1d56e0f · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d56e0f

Browse files
committed
Merge pull request twilio#181 from hwayne/fix-python3-validator
signature now returns native type for both python2 and python3
2 parents 49894a4 + 5911a5e commit 1d56e0f

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

tests/test_validation.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
import unittest
33

44
from nose.tools import assert_equal, assert_true
5-
from six import b
5+
from six import b, u
66

77
from twilio.util import RequestValidator
88

99

1010
class ValidationTest(unittest.TestCase):
1111

12-
def test_validation(self):
12+
def setUp(self):
1313
token = "1c892n40nd03kdnc0112slzkl3091j20"
14-
validator = RequestValidator(token)
14+
self.validator = RequestValidator(token)
1515

16-
uri = "http://www.postbin.org/1ed898x"
17-
params = {
16+
self.uri = "http://www.postbin.org/1ed898x"
17+
self.params = {
1818
"AccountSid": "AC9a9f9392lad99kla0sklakjs90j092j3",
1919
"ApiVersion": "2010-04-01",
2020
"CallSid": "CAd800bb12c0426a7ea4230e492fef2a4f",
@@ -43,7 +43,20 @@ def test_validation(self):
4343
"ToZip": "94612",
4444
}
4545

46+
def test_compute_signature_bytecode(self):
4647
expected = b("fF+xx6dTinOaCdZ0aIeNkHr/ZAA=")
48+
signature = self.validator.compute_signature(self.uri,
49+
self.params,
50+
utf=False)
51+
assert_equal(signature, expected)
52+
53+
def test_compute_signature_unicode(self):
54+
expected = u("fF+xx6dTinOaCdZ0aIeNkHr/ZAA=")
55+
signature = self.validator.compute_signature(self.uri,
56+
self.params,
57+
utf=True)
58+
assert_equal(signature, expected)
4759

48-
assert_equal(validator.compute_signature(uri, params), expected)
49-
assert_true(validator.validate(uri, params, expected))
60+
def test_validation(self):
61+
expected = "fF+xx6dTinOaCdZ0aIeNkHr/ZAA="
62+
assert_true(self.validator.validate(self.uri, self.params, expected))

twilio/util.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@
55

66
from . import jwt
77
from .compat import izip, urlencode
8-
from six import iteritems
8+
from six import iteritems, PY3
99

1010

1111
class RequestValidator(object):
1212

1313
def __init__(self, token):
1414
self.token = token.encode("utf-8")
1515

16-
def compute_signature(self, uri, params):
16+
def compute_signature(self, uri, params, utf=PY3):
1717
"""Compute the signature for a given request
1818
1919
:param uri: full URI that Twilio requested on your server
2020
:param params: post vars that Twilio sent with the request
2121
:param auth: tuple with (account_sid, token)
22+
:param utf: whether return should be bytestring or unicode (python3)
2223
2324
:returns: The computed signature
2425
"""
@@ -30,6 +31,8 @@ def compute_signature(self, uri, params):
3031
# compute signature and compare signatures
3132
mac = hmac.new(self.token, s.encode("utf-8"), sha1)
3233
computed = base64.b64encode(mac.digest())
34+
if utf:
35+
computed = computed.decode('utf-8')
3336

3437
return computed.strip()
3538

0 commit comments

Comments
 (0)
0