Skip to main content

Webhook

1. Introduction and Preparation

Overview and Importance

This section covers how QI Tech sends webhooks with signed headers, highlighting the importance of decrypting and validating these headers to ensure secure communication.

Request Format

Webhook requests will be sent to the URL configured for receiving webhooks. . They have a specific format for headers and body, which is detailed below.

ENDPOINT
URL configurada para recebimento dos webhooks
METHOD
POST
Request Headers
{
"AUTHORIZATION": "eyJhbGciOiJFUzUxMiIsInR5cCI6IkpXVCJ9.eyJwYXlsb2FkX21kNSI6IjRhNjAzZjBmMGU3ZGRkZTlkYTJhMGFkM2QzNDFmNzRiIiwidGltZXN0YW1wIjoiMjAyMy0wNi0zMFQxODo1MjoyNy44ODU3MzFaIiwibWV0aG9kIjoiUE9TVCIsInVyaSI6Ii90ZXN0In0.AcNiJqXDdVmlXSbPI6bH41n0KXz9JwVVMgo4Ivqsq5UZjM2WBOTWw3aAvIMAAhjK5OdrURD4cX3dbbnRgzxspUckANRt0hVHRKSkhROHBfZxuTXVfv8oYzwghwiO2MatPBsroC9Vxbh-DEVQJIBigtN9_D5bg8p2-mlVvoxou2I-EwZs",
"API-CLIENT-KEY": "20d6a816-9d21-4e29-bbe5-2ffb3baacfe9"
}
Request Body
{
"body_sample": "Exemplo de webhook"
}

2. Configuration and Decryption

Import libraries

Before starting the decryption and validation of webhooks, it is essential to import the necessary libraries in your preferred programming language. These libraries will facilitate working with JWTs, encryption, and other related aspects.

import json
from datetime import datetime, timedelta
from hashlib import md5
from jose import jwt

Define variables

Define the necessary variables to handle the headers and body of the webhook. This includes the public key provided by QI Tech, used to decrypt and validate the webhook.

headers = {
"AUTHORIZATION": "eyJhbGciOiJFUzUxMiIsInR5cCI6IkpXVCJ9.eyJwYXlsb2FkX21kNSI6IjRhNjAzZjBmMGU3ZGRkZTlkYTJhMGFkM2QzNDFmNzRiIiwidGltZXN0YW1wIjoiMjAyMy0wNi0zMFQxODo1MjoyNy44ODU3MzFaIiwibWV0aG9kIjoiUE9TVCIsInVyaSI6Ii90ZXN0In0.AcNiJqXDdVmlXSbPI6bH41n0KXz9JwVVMgo4Ivqsq5UZjM2WBOTWw3aAvIMAAhjK5OdrURD4cX3dbbnRgzxspUckANRt0hVHRKSkhROHBfZxuTXVfv8oYzwghwiO2MatPBsroC9Vxbh-DEVQJIBigtN9_D5bg8p2-mlVvoxou2I-EwZs",
"API-CLIENT-KEY": "20d6a816-9d21-4e29-bbe5-2ffb3baacfe9",
}
body = {"body_sample": "Exemplo de webhook"}
authorization = headers.get("AUTHORIZATION")

2. Insertion of Encryption Data and Performing Decryption

We insert the public key provided by QI Tech and perform the decryption of the webhook header. This key is crucial for decrypting the webhook headers.

qi_public_key = """-----BEGIN PUBLIC KEY-----
{QI_PUBLIC_KEY}
-----END PUBLIC KEY-----"""

Decrypt the header

The decryption process is essential to verify the authenticity and integrity of the received webhook.

try:
decoded_header = jwt.decode(token=authorization, key=qi_public_key)
except:
raise Exception("Decodification failed.")

3. Validation and Conclusion

Performing Validations

After decrypting the header, it is important to perform various validations to ensure that the webhook is valid and secure.

assert decoded_header.get("method") == "POST"
assert decoded_header.get("uri") == "/client_webhook_endpoint"
assert (
decoded_header.get("payload_md5")
== md5(json.dumps(body).encode()).hexdigest()
)
assert (
(datetime.now() - timedelta(minutes=5))
< datetime.strptime(decoded_header.get("timestamp"), "%Y-%m-%dT%H:%M:%S.%fZ")
< (datetime.now() + timedelta(minutes=5))
)