Skip to content

Commit

Permalink
Fix TypeError while validating webhook signature(Fix razorpay#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
namantam1 committed Jul 19, 2021
1 parent 8482c5c commit 0146d4d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 6 additions & 3 deletions razorpay/utility/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ def verify_webhook_signature(self, body, signature, secret):
return self.verify_signature(body, signature, secret)

def verify_signature(self, body, signature, key):
if sys.version_info[0] == 3: # pragma: no cover
key = bytes(key, 'utf-8')
body = bytes(body, 'utf-8')
if sys.version_info[0] == 3:
# ensure if not already bytes object
if not isinstance(key, (bytes, bytearray)):
key = bytes(key, 'utf-8')
if not isinstance(body, (bytes, bytearray)):
body = bytes(body, 'utf-8')

dig = hmac.new(key=key,
msg=body,
Expand Down
10 changes: 10 additions & 0 deletions tests/test_client_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ def test_verify_webhook_signature(self):
self.client.utility.verify_webhook_signature(body, sig, secret),
True)

@responses.activate
def test_verify_webhook_signature_with_bytes_data(self):
secret = self.client.auth[1]
sig = 'd60e67fd884556c045e9be7dad57903e33efc7172c17c6e3ef77db42d2b366e9'
body = bytes(mock_file('fake_payment_authorized_webhook'), 'utf-8')

self.assertEqual(
self.client.utility.verify_webhook_signature(body, sig, secret),
True)

@responses.activate
def test_verify_webhook_signature_with_exception(self):
secret = self.client.auth[1]
Expand Down

0 comments on commit 0146d4d

Please sign in to comment.