webhook-id: 7c1e2f0a-3b5d-4e8f-9a61-2d4c8b0e5f13
webhook-timestamp: 1790490600
webhook-signature: v1,s0RzLJ1d7Cxd9nwe8pqER9DWy/stEIB+uT9MlGVLlM0=
| Ceanntásc |
Cad é atá ann |
| webhook-id |
An seachadadh, an luach céanna le delivery_id sa chorp. Coinnigh i gcuimhne na cinn a láimhseáil tú, agus freagair athsheoladh le 200 gan an obair a dhéanamh arís. |
| webhook-timestamp |
An t-am ar shínigh muid é, i soicindí iomlána ó 1970 i leith. Diúltaigh d'aon rud atá níos mó ná cúig nóiméad ó do chlog féin, ionas nach féidir seanseachadadh a sheoladh chugat arís. |
| webhook-signature |
v1, ansin base64 HMAC-SHA256 den aitheantas, den stampa ama agus den chorp amh ceangailte le lánstadanna, déanta le d'eochair shínithe. Is féidir níos mó ná ceann amháin a bheith ann, scartha le spásanna. Glac leis an seachadadh má mheaitseálann aon cheann acu. |
An eochair shínithe
Tosaíonn sé le whsec_. Bain é sin de agus díchódaigh an chuid eile le base64: is iad na bearta sin an eochair HMAC, ní an téacs a chóipeáil tú. Taispeántar é uair amháin, nuair a nascann tú an webhook nó a bhrúnn tú "New signing key" ar an leathanach Foilsitheoireachta, agus ní sheolaimid riamh é.
Seiceáil an corp amh
Ríomh an síniú ó na bearta díreach mar a tháinig siad, sula ndéanann aon rud iad a pharsáil. Ní hiad na bearta céanna iad JSON a díchódaíodh agus a ionchódaíodh arís, agus ní mheaitseálfaidh a shíniú.
PHP
<?php
$raw = getenv('SEONIS_SIGNING_KEY'); // whsec_...
// No key, no deliveries. A missing or empty variable would otherwise become
// an empty HMAC key, and anybody can sign with that.
$secret = is_string($raw) && str_starts_with($raw, 'whsec_')
? base64_decode(substr($raw, strlen('whsec_')), true)
: false;
if ($secret === false || strlen($secret) < 24) {
http_response_code(500);
exit('Signing key not configured.');
}
$id = $_SERVER['HTTP_WEBHOOK_ID'] ?? '';
$timestamp = $_SERVER['HTTP_WEBHOOK_TIMESTAMP'] ?? '';
$header = $_SERVER['HTTP_WEBHOOK_SIGNATURE'] ?? '';
$body = file_get_contents('php://input');
if (! ctype_digit($timestamp) || abs(time() - (int) $timestamp) > 300) {
http_response_code(401);
exit('Too old.');
}
$expected = base64_encode(hash_hmac('sha256', "$id.$timestamp.$body", $secret, true));
$valid = false;
foreach (explode(' ', $header) as $signature) {
[$version, $value] = array_pad(explode(',', $signature, 2), 2, '');
$valid = $valid || ($version === 'v1' && hash_equals($expected, $value));
}
if (! $valid) {
http_response_code(401);
exit('Bad signature.');
}
$delivery = json_decode($body, true);
Node.js
import crypto from 'node:crypto';
import express from 'express';
const app = express();
// No key, no server. An empty variable would otherwise become an empty HMAC
// key, and anybody can sign with that.
const raw = process.env.SEONIS_SIGNING_KEY ?? '';
if (!raw.startsWith('whsec_')) throw new Error('SEONIS_SIGNING_KEY is not set.');
const key = Buffer.from(raw.slice('whsec_'.length), 'base64');
if (key.length < 24) throw new Error('SEONIS_SIGNING_KEY is not a valid key.');
// express.raw keeps the body as the bytes we signed.
app.post('/hooks/seonis', express.raw({ type: 'application/json' }), (req, res) => {
const id = req.get('webhook-id') ?? '';
const timestamp = req.get('webhook-timestamp') ?? '';
const header = req.get('webhook-signature') ?? '';
if (!/^\d+$/.test(timestamp) || Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) {
return res.status(401).send('Too old.');
}
const expected = crypto.createHmac('sha256', key)
.update(`${id}.${timestamp}.`)
.update(req.body)
.digest();
const valid = header.split(' ').some((signature) => {
const [version, value = ''] = signature.split(',');
const given = Buffer.from(value, 'base64');
return version === 'v1' && given.length === expected.length && crypto.timingSafeEqual(given, expected);
});
if (!valid) return res.status(401).send('Bad signature.');
const delivery = JSON.parse(req.body);
res.json({ ok: true });
});
Nó úsáid leabharlann
Déanann leabharlanna oifigiúla Standard Webhooks, do PHP, JavaScript, Python, Go, Ruby agus eile, é seo ar fad in aon ghlao amháin. Tabhair an eochair shínithe dóibh mar a taispeánadh duit í, whsec_ san áireamh.
github.com/standard-webhooks
Na ceanntásca níos sine
Iompraíonn seachadtaí an rún sínithe mar thócan iompróra freisin, agus X-AutoSEO-LT-Signature, HMAC den chorp amháin a dhéantar leis an rún sin. Seoltar iad fós ionas go leanfaidh críochphointí a tógadh orthu ag obair. Ba chóir do chríochphointe nua webhook-signature a sheiceáil ina ionad: ní chlúdaíonn an seanshíniú aon am ná aon aitheantas, agus téann a eochair i ngach iarratas.
Seolann na formáidí AutoSEO, Outrank agus BabyLoveGrowth go díreach an méid a sheol na seirbhísí sin, mar sin ní iompraíonn siad na ceanntásca seo.