Last active
April 29, 2022 12:12
-
-
Save byteofprash/ca411411b5c9f4f5adf934eb2b358f61 to your computer and use it in GitHub Desktop.
This file contains 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
import os | |
from flask import Flask, request, abort | |
from twilio.request_validator import RequestValidator | |
from urllib import parse | |
from collections import OrderedDict | |
from hashlib import sha1, sha256 | |
import hmac | |
import base64 | |
app = Flask(__name__) | |
@app.route('/test', methods=['GET']) | |
def test(): | |
validator = RequestValidator(os.environ.get('TWILIO_AUTH_TOKEN')) | |
full_url = 'https://' + request.url.lstrip('http://') | |
result = validator.validate(full_url, None, request.headers.get('X-Twilio-Signature')) | |
print(result) | |
if result: | |
print("THIS IS Validated") | |
else: | |
print("Not Validated") | |
print("Request is: ", request.__dict__) | |
return {"result": result} | |
@app.route('/testsigs', methods=['GET']) | |
def test_sigs(): | |
full_url = 'https://' + request.url.lstrip('http://') | |
mac = hmac.new(bytes(os.environ.get("TWILIO_AUTH_TOKEN"), 'UTF-8'), full_url.encode("utf-8"), sha1) | |
print(mac.digest()) | |
computed = base64.b64encode(mac.digest()) | |
print(f"computed value (base64 encoded): {computed}") | |
computed = computed.decode('utf-8') | |
print(f"computed value (utf-8 decoded): {computed}") | |
diy_signature = computed.strip() | |
print(f"diy_signature value: {diy_signature}") | |
print(f"original signature: {request.headers.get('X-Twilio-Signature')}") | |
return "done" | |
if __name__ == "__main__": | |
app.run(debug=True, port=5002) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment