- ڈویلپر ہوم
- ایڈیٹرز ایمبیڈ کریں
- سیو کال بیک
سیو کال بیک
سیو کال بیک
جب کوئی محفوظ کرنا نیا ورژن لکھتا ہے، تو MR Office آپ کے کال بیک URL پر POST کرتا ہے۔ ناکام محفوظ کرنا document.save_failed کو غلطی کے ساتھ بھیجتا ہے۔
POST https://your-app.example/mroffice/callback
Content-Type: application/json
MRO-Signature: t=1795100212,v1=5f1c…e9a2
MRO-Event-Id: 6f0a1c1e-…
Idempotency-Key: 6f0a1c1e-…
{
"id": "6f0a1c1e-…",
"type": "document.saved",
"created": "2026-11-19T15:03:11+00:00",
"app_id": "7f1d2c3b-…",
"document": {
"id": "…", "key": "contract-42", "name": "contract.docx",
"version": 3, "previous_version": 2, "partner_version": "etag-17",
"sha256": "…", "size": 48211,
"content_type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"source": "url"
},
"save": { "id": "…", "reason": "manual", "editors": ["user-7", "user-9"], "saved_at": "…" },
"download_url": "https://mroffice.ai/api/v1/embed/download/…?expires=…&signature=…",
"download_expires_at": "2026-11-19T16:03:11+00:00"
}- سب سے پہلے MRO-Signature کی تصدیق کریں: t ایک Unix ٹائم اسٹیمپ ہے، ہر v1 ہیکس میں HMAC-SHA256(webhook key, "<t>.<raw body>") ہے (دو v1 اقدار جب تک کوئی گھمائی گئی کلید اب بھی قبول ہو)۔ پانچ منٹ سے پرانے ٹائم اسٹیمپ مسترد کریں۔
- 20 سیکنڈ کے اندر کسی بھی 2xx کا جواب دیں۔ اختیاری طور پر { "version": "<your new version>" } کا جواب دیں: اگلے ٹوکنز کا file.version اس سے موازنہ کیا جاتا ہے۔
- باقی سب کچھ 10 s، 30 s، 2 min، 10 min، 30 min، 1 h، 3 h اور 6 h کے بعد دوبارہ کوشش کیا جاتا ہے (410 Gone رک جاتا ہے)۔ id اور Idempotency-Key ہر دوبارہ کوشش میں یکساں رہتے ہیں: ہر ایونٹ کو ایک بار اسٹور کریں۔
- download_url ایک گھنٹے کے لیے کام کرتا ہے (کال بیک کی ہر دوبارہ کوشش کے ساتھ تازہ URL آتا ہے)۔ جب تک کوئی ورژن رکھا جائے آپ اسے اپنی کلید سے لا سکتے ہیں: GET /api/v1/embed/documents/{key}/content?version=N؛ حذف شدہ ورژن 410 embed_version_purged کا جواب دیتا ہے۔
// کال بیک، پیکیج کے سرور ہیلپر کے ساتھ (کوئی بھی Web Crypto رن ٹائم)۔
import { verifyCallback } from "mroffice-embed/server";
app.post("/mroffice/callback", express.raw({ type: "application/json" }), async (req, res) => {
let event;
try {
event = await verifyCallback(req.body, req.get("MRO-Signature"), process.env.MROFFICE_WEBHOOK_SECRET);
} catch {
return res.status(401).end(); // آپ کی webhook کلید سے دستخط شدہ نہیں
}
if (await seen(event.id)) return res.json({ ok: true }); // ایک کال بیک دو بار آ سکتا ہے
// … event.download_url کی فائل محفوظ کریں، جیسا کہ نیچے ہے
res.json({ ok: true });
});import { createHmac, timingSafeEqual } from "node:crypto";
// خام باڈی بائٹس استعمال کریں، بالکل ویسے ہی جیسے موصول ہوئیں (دوبارہ سیریلائز شدہ JSON نہیں)۔
export function verifyMROfficeSignature(rawBody, header, webhookSecret, toleranceSeconds = 300) {
const parts = Object.groupBy(header.split(","), (item) => item.split("=")[0].trim());
const t = Number(parts.t?.[0]?.split("=")[1]);
if (!Number.isFinite(t) || Math.abs(Date.now() / 1000 - t) > toleranceSeconds) return false;
const expected = createHmac("sha256", webhookSecret).update(`${t}.`).update(rawBody).digest();
return (parts.v1 ?? []).some((item) => {
const given = Buffer.from(item.split("=")[1] ?? "", "hex");
return given.length === expected.length && timingSafeEqual(given, expected);
});
}
// app.post("/mroffice/callback", express.raw({ type: "application/json" }), async (req, res) => {
// if (!verifyMROfficeSignature(req.body, req.get("MRO-Signature"), process.env.MROFFICE_WEBHOOK_SECRET))
// return res.status(401).end();
// const event = JSON.parse(req.body);
// if (await seen(event.id)) return res.json({ ok: true }); // idempotent
// if (event.type === "document.saved") {
// const file = await fetch(event.download_url).then((r) => r.arrayBuffer());
// const etag = await contracts.storeVersion(event.document.key, file, event.document.version);
// return res.json({ version: etag }); // your new version: compared with the next token's file.version
// }
// res.json({ ok: true });
// });import hashlib, hmac, time
def verify_mroffice_signature(raw_body: bytes, header: str, webhook_secret: str, tolerance: int = 300) -> bool:
pairs = [item.split("=", 1) for item in header.split(",") if "=" in item]
timestamps = [value for key, value in pairs if key.strip() == "t"]
signatures = [value for key, value in pairs if key.strip() == "v1"]
if not timestamps or not timestamps[0].isdigit() or abs(time.time() - int(timestamps[0])) > tolerance:
return False
expected = hmac.new(webhook_secret.encode(), f"{timestamps[0]}.".encode() + raw_body, hashlib.sha256).hexdigest()
return any(hmac.compare_digest(expected, candidate.strip()) for candidate in signatures)