{
  "name": "Sitecommand alerts",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "sitecommand-alerts",
        "responseMode": "responseNode",
        "options": { "rawBody": true }
      },
      "id": "6d1f5a50-0001-4c1a-9a01-5c0000000001",
      "name": "Sitecommand webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [0, 0],
      "webhookId": "6d1f5a50-0001-4c1a-9a01-5c00000000ff"
    },
    {
      "parameters": {
        "jsCode": "// Checks that the alert really comes from Sitecommand (docs: /guide#n8n).\n// 1. Paste the channel's signing secret (Alerts -> the webhook channel -> Sends to).\n//    Better: keep it in an n8n variable or credential and read it here.\n// 2. Self-hosted n8n: start it with NODE_FUNCTION_ALLOW_BUILTIN=crypto.\nconst SECRET = 'PASTE_THE_CHANNEL_SIGNING_SECRET';\nconst crypto = require('crypto');\n\nconst item = $input.first();\nconst headers = item.json.headers || {};\nif (!item.binary || !item.binary.data) {\n  throw new Error('Turn on Raw Body in the webhook node options: the signature covers the body exactly as sent.');\n}\nconst raw = await this.helpers.getBinaryDataBuffer(0, 'data');\n\n// The signature: HMAC-SHA256 of the raw body, compared in constant time.\nconst expected = 'sha256=' + crypto.createHmac('sha256', SECRET).update(raw).digest('hex');\nconst got = String(headers['x-sitecommand-signature'] || '');\nconst signed = got.length === expected.length && crypto.timingSafeEqual(Buffer.from(got), Buffer.from(expected));\nif (!signed) return [{ json: { valid: false, reason: 'bad_signature' } }];\n\nlet alert;\ntry {\n  alert = JSON.parse(raw.toString('utf8'));\n} catch (e) {\n  return [{ json: { valid: false, reason: 'not_json' } }];\n}\n\n// Fresh: sent_at is inside the signed body, so it cannot be changed. Five minutes either way.\nconst age = Date.now() - Date.parse(alert.sent_at);\nif (!(Math.abs(age) <= 5 * 60 * 1000)) return [{ json: { valid: false, reason: 'stale' } }];\n\n// Once: a replayed copy has the same signature. Remembered for a day (active workflows only).\nconst store = $getWorkflowStaticData('global');\nconst now = Date.now();\nstore.seen = (store.seen || []).filter((s) => now - s.t < 24 * 3600 * 1000);\nif (store.seen.some((s) => s.k === got)) return [{ json: { valid: false, reason: 'repeat' } }];\nstore.seen.push({ k: got, t: now });\n\nreturn [{ json: { valid: true, alert } }];"
      },
      "id": "6d1f5a50-0002-4c1a-9a01-5c0000000002",
      "name": "Check signature",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [240, 0]
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict" },
          "conditions": [
            {
              "id": "6d1f5a50-0003-4c1a-9a01-5c00000000c1",
              "leftValue": "={{ $json.valid }}",
              "rightValue": true,
              "operator": { "type": "boolean", "operation": "true", "singleValue": true }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "6d1f5a50-0003-4c1a-9a01-5c0000000003",
      "name": "Genuine?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [480, 0]
    },
    {
      "parameters": { "respondWith": "json", "responseBody": "={{ { ok: true } }}", "options": { "responseCode": 200 } },
      "id": "6d1f5a50-0004-4c1a-9a01-5c0000000004",
      "name": "Answer 200",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [720, -120]
    },
    {
      "parameters": { "respondWith": "json", "responseBody": "={{ { ok: false, reason: $json.reason } }}", "options": { "responseCode": 401 } },
      "id": "6d1f5a50-0005-4c1a-9a01-5c0000000005",
      "name": "Refuse 401",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [720, 120]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "6d1f5a50-0006-4c1a-9a01-5c00000000a1", "name": "event", "value": "={{ $('Check signature').item.json.alert.event }}", "type": "string" },
            { "id": "6d1f5a50-0006-4c1a-9a01-5c00000000a2", "name": "severity", "value": "={{ $('Check signature').item.json.alert.severity }}", "type": "string" },
            { "id": "6d1f5a50-0006-4c1a-9a01-5c00000000a3", "name": "title", "value": "={{ $('Check signature').item.json.alert.title }}", "type": "string" },
            { "id": "6d1f5a50-0006-4c1a-9a01-5c00000000a4", "name": "body", "value": "={{ $('Check signature').item.json.alert.body }}", "type": "string" },
            { "id": "6d1f5a50-0006-4c1a-9a01-5c00000000a5", "name": "site", "value": "={{ $('Check signature').item.json.alert.site ? $('Check signature').item.json.alert.site.url : '' }}", "type": "string" },
            { "id": "6d1f5a50-0006-4c1a-9a01-5c00000000a6", "name": "link", "value": "={{ $('Check signature').item.json.alert.url }}", "type": "string" }
          ]
        },
        "options": {}
      },
      "id": "6d1f5a50-0006-4c1a-9a01-5c0000000006",
      "name": "The alert",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [960, -120]
    },
    {
      "parameters": {},
      "id": "6d1f5a50-0007-4c1a-9a01-5c0000000007",
      "name": "Your next step (SMS, ClickUp, Google Sheets...)",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [1200, -120]
    }
  ],
  "connections": {
    "Sitecommand webhook": { "main": [[{ "node": "Check signature", "type": "main", "index": 0 }]] },
    "Check signature": { "main": [[{ "node": "Genuine?", "type": "main", "index": 0 }]] },
    "Genuine?": {
      "main": [
        [{ "node": "Answer 200", "type": "main", "index": 0 }],
        [{ "node": "Refuse 401", "type": "main", "index": 0 }]
      ]
    },
    "Answer 200": { "main": [[{ "node": "The alert", "type": "main", "index": 0 }]] },
    "The alert": { "main": [[{ "node": "Your next step (SMS, ClickUp, Google Sheets...)", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" },
  "active": false
}
