Podpisywanie webhooków
Stwórz klucz aby podpisać i weryfikować swoje webhooki
Wygeneruj swój indywidualny klucz, a my automatycznie podpiszemy nim wszystkie Twoje webhooki. Nie wpływa to w żaden sposób na dostarczanie tych webhooków, ani na ich treść, ale daje możliwość, aby po stronie odbiorcy, weryfikować sygnaturę tym samym kluczem.
Generowanie klucza
Aby wygenerować klucz do podpisywania webhooków:
- Przejdź do sekcji Sklep ➔ Ustawienia sklepu
- Otwórz zakładkę API i Webhooki
- Kliknij +Wygeneruj klucz do podpisu webhooka
- Skopiuj swój klucz klikając Skopiuj klucz
Ważne: Upewnij się, że skopiujesz i zapiszesz klucz podpisywania, ponieważ nie będziesz w stanie go zobaczyć po jego utworzeniu.
Po powrocie do panelu API i Webhooki zobaczysz informację o swoim kluczu podpisywania. Możesz także usunąć go klikając Usuń lub wygenerować nowy, klikając Zmień klucz.
Wykorzystywanie klucza do weryfikacji webhooków
Do każdego zapytania wysyłanego na webhook jest od tej pory dodawany nagłówek X-Webhook-Signature, którą należy wyliczyć po stronie odbierającej i porównać czy wartości są takie same.
Aby to zrobić, należy wyliczyć SHA256 z treści otrzymanego zapytania. Robimy to za pomocą wygenerowanego wcześniej klucza i porównujemy wartości.
Sprawdź poniższe przykłady w różnych językach programowania, aby zobaczyć, jak to działa.
JavaScript (Node.js/Express)
const crypto = require('crypto');
const express = require('express');
const app = express();
app.use(express.json());
app.post('/webhook', (req, res) => {
// Get the signature from the headers
const receivedSignature = req.headers['x-webhook-signature'];
// Your webhook signing key (should be stored securely)
const signingKey = 'your_secret_signing_key';
// Get the raw body data
const data = req.body;
const jsonData = JSON.stringify(data);
// Calculate signature using HMAC-SHA256
const calculatedSignature = crypto
.createHmac('sha256', signingKey)
.update(jsonData)
.digest('hex');
// Verify the signature
if (receivedSignature === calculatedSignature) {
console.log('Signature verified successfully!');
res.status(200).send('Webhook received and verified');
} else {
console.log('Signature verification failed!');
res.status(401).send('Invalid signature');
}
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
PHP
// Get the raw webhook payload without json_decode
$payload = file_get_contents('php://input');
// Get the signature from headers
$receivedSignature = $_SERVER['HTTP_X_WEBHOOK_SIGNATURE'] ?? '';
// Your webhook signing key (should be stored securely)
$signingKey = 'your_secret_signing_key';
// Calculate signature using HMAC-SHA256 directly on the raw payload
$calculatedSignature = hash_hmac('sha256', $payload, $signingKey);
// Verify the signature
if (hash_equals($receivedSignature, $calculatedSignature)) {
http_response_code(200);
echo "Webhook received and verified";
} else {
http_response_code(401);
echo "Invalid signature";
}
Python (Flask)
from flask import Flask, request, jsonify
import hmac
import hashlib
import json
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def webhook():
# Get the signature from headers
received_signature = request.headers.get('X-Webhook-Signature')
# Your webhook signing key (should be stored securely)
signing_key = 'your_secret_signing_key'
# Get the payload data
payload = request.get_data()
# Calculate signature using HMAC-SHA256
calculated_signature = hmac.new(
signing_key.encode('utf-8'),
payload,
hashlib.sha256
).hexdigest()
# Verify the signature
if hmac.compare_digest(received_signature, calculated_signature):
return jsonify({"message": "Webhook received and verified"}), 200
else:
return jsonify({"message": "Invalid signature"}), 401
if __name__ == '__main__':
app.run(debug=True, port=5000)
C# (.NET)
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseHttpsRedirection();
app.MapPost("/webhook", async context =>
{
// Read the request body
using var reader = new StreamReader(context.Request.Body);
var payload = await reader.ReadToEndAsync();
// Get the signature from headers
context.Request.Headers.TryGetValue("X-Webhook-Signature", out var receivedSignature);
// Your webhook signing key (should be stored securely)
string signingKey = "your_secret_signing_key";
// Calculate signature using HMAC-SHA256
using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(signingKey));
var hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(payload));
var calculatedSignature = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
// Verify the signature
if (receivedSignature.Equals(calculatedSignature, StringComparison.OrdinalIgnoreCase))
{
context.Response.StatusCode = 200;
await context.Response.WriteAsync("Webhook received and verified");
}
else
{
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Invalid signature");
}
});
app.Run();