Created
December 16, 2023 03:18
-
-
Save digidigo/cc631c1cbf1602664dce48eec6a00a42 to your computer and use it in GitHub Desktop.
mockserver to validate and response to VIO webhooks
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
from flask import Flask, request, jsonify | |
import hashlib | |
import hmac | |
import json | |
app = Flask(__name__) | |
def verify_signature(payload_body, secret_token, signature_header): | |
hash_object = hmac.new(secret_token.encode('utf-8'), msg=payload_body, digestmod=hashlib.sha256) | |
expected_signature = "sha256=" + hash_object.hexdigest() | |
if hmac.compare_digest(expected_signature, signature_header): | |
print('Signature match') | |
else: | |
print('Signature: No match') | |
@app.route('/', defaults={'path': ''}, methods=['GET', 'POST', 'PUT', 'DELETE']) | |
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE']) | |
def log_request(path): | |
print(f"Path: {path}") # Log the request path | |
print(f"Data: {request.data}") # Log the request data | |
print(f"Headers: {request.headers}") # Log the request headers | |
print(f"Method: {request.method}") # Log the request method | |
print(f"Args: {request.args}") # Log the query parameters | |
# Get the request payload body and X-Vio-Signature header | |
payload_body = request.data | |
signature_header = request.headers.get('X-Vio-Signature', '') | |
# Your secret token | |
secret_token = 'XXXXXXXXXX' | |
# Verify the signature | |
verify_signature(payload_body, secret_token, signature_header) | |
request_data_string = request.data.decode('utf-8') | |
request_data = json.loads(request_data_string) | |
return jsonify(request_data), 200 | |
if __name__ == '__main__': | |
app.run(debug=True, port=9000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment