Created
February 17, 2022 17:23
-
-
Save emckee4/deef6cc2bf67ce27fa87b4d5b5d7a859 to your computer and use it in GitHub Desktop.
Twilio webhook validation for POST events using an AWS lambda proxy integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Twilio webhook validation for POST events using an AWS lambda proxy integration. | |
This will return the parsed body or throw an exception if validation fails. It assumes | |
you are using an aws Lambda functions with an API Gateway proxy integration. | |
Example: | |
$ eventParser = TwilioEventParser(twilio_auth_token) | |
$ try: | |
$ parsedBody = eventParser.parse(event, validate=True) | |
$ except: | |
$ pass | |
""" | |
from urllib.parse import parse_qs | |
from collections import OrderedDict | |
from twilio.request_validator import RequestValidator | |
def parseTwilioEventParams(event:dict) -> OrderedDict: | |
if 'body' in event: #if post | |
try: | |
params = OrderedDict() | |
for (k,v) in parse_qs(event['body']).items(): | |
if len(v) == 1: | |
params[k] = v[0] | |
else: | |
params[k] = v | |
return params | |
except: | |
raise Exception('failed to parse twilio parameters') # Bring your own custom exceptions | |
else: | |
raise Exception('Request missing body') | |
class TwilioEventParser(): | |
def __init__(self, auth_token:str): | |
self.validator = RequestValidator(auth_token) | |
def validateEvent(self, event:dict, parsedParams:OrderedDict) -> bool: | |
twilioSig = event.get('headers').get('X-Twilio-Signature') | |
host = event.get('headers').get('Host') | |
port = event.get('headers').get('X-Forwarded-Port') | |
protocol = event.get('headers').get('X-Forwarded-Proto') | |
path = event['path'] | |
url = f"{protocol}://{host}:{port}{path}" | |
isValid = self.validator.validate(url, parsedParams, twilioSig) | |
print(f"Validation: {('success' if isValid else 'failure')}") | |
return isValid | |
def parse(self, event:dict, validate:bool=True) -> OrderedDict: | |
messageParams = parseTwilioEventParams(event) | |
if not validate: | |
return messageParams | |
if not self.validateEvent(event, messageParams): | |
raise Exception('twilio signature validation failed') | |
return messageParams |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment