Skip to content

Commit

Permalink
handles a JSON body that isn't a SWML callback
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsantosbh committed Nov 14, 2023
1 parent 1a0883c commit ca6fd45
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
10 changes: 9 additions & 1 deletion signalwire/request_validator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
import hmac
import json
from hashlib import sha1
from urllib.parse import urlparse
from twilio.request_validator import compare, remove_port, add_port, RequestValidator as TwilioRequestValidator
Expand Down Expand Up @@ -42,6 +43,13 @@ def validate(self, uri, params, signature):
signature
)

return valid_signature_without_port or valid_signature_with_port
if valid_signature_without_port or valid_signature_with_port:
return True

try:
parsed_params = json.loads(params)
return self.validate_with_compatibility(uri, parsed_params, signature)
except json.JSONDecodeError as e:
return False

return self.validate_with_compatibility(uri, params, signature)
26 changes: 26 additions & 0 deletions signalwire/tests/test_request_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,32 @@ def test_should_validate_from_signalwire_https_request(self):
valid = validator.validate(url, body, signature)
self.assertTrue(valid)

def test_should_validate_from_raw_json(self):
from signalwire.request_validator import RequestValidator

url = 'https://675d-189-71-169-171.ngrok-free.app/voice'
token = 'PSK_V3bF8oyeRNpJWGoRWHNYQMUU'
signature = 'muUMpldcBHlzuXGZ5gbw1ETZCYA='
body = '''{
"CallSid": "a97d4e8a-6047-4e2b-be48-fb96b33b5642",
"AccountSid": "6bfbbe86-a901-4197-8759-2a0de1fa319d",
"ApiVersion": "2010-04-01",
"Direction": "outbound-api",
"From": "sip:[email protected]",
"To": "sip:[email protected]",
"Timestamp": "Thu, 09 Nov 2023 14:40:55 +0000",
"CallStatus": "no-answer",
"CallbackSource": "call-progress-events",
"HangupDirection": "outbound",
"HangupBy": "sip:[email protected]",
"SipResultCode": "487"
}'''


validator = RequestValidator(token)
valid = validator.validate(url, body, signature)
self.assertTrue(valid)




0 comments on commit ca6fd45

Please sign in to comment.