{
  "id": "FmSignatureWebh3",
  "name": "FreightMaster 3 : vérifier la signature d'un webhook",
  "active": false,
  "nodes": [
    {
      "parameters": {
        "content": "## Vérifier la signature d'un webhook FreightMaster\n\n1. Publiez ce modèle et déclarez son adresse de production (HTTPS) dans le portail, section API, Webhooks.\n2. Le secret `whsec_…` n'est montré qu'une fois : mettez-le dans un identifiant **Crypto** nommé « FreightMaster secret de webhook », champ *Hmac Secret*.\n3. Un corps modifié, une mauvaise signature ou un horodatage de plus de 5 minutes reçoivent 401.\n\nUne réponse autre que 2xx déclenche les reprises du serveur.",
        "width": 620,
        "height": 270,
        "color": 4
      },
      "id": "f0a1b2c3-0000-4000-8000-000000000501",
      "name": "Note 500",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        160,
        -80
      ]
    },
    {
      "parameters": {
        "content": "**Le calcul porte sur le corps BRUT.** Ne le reconstruisez pas à partir du JSON analysé : un espace changé casse la signature.\n\nLimite : la comparaison du nœud If n'est pas à temps constant. Si votre risque l'exige, comparez dans votre propre service.",
        "width": 520,
        "height": 150,
        "color": 7
      },
      "id": "f0a1b2c3-0000-4000-8000-000000000502",
      "name": "Note 501",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1160,
        -80
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "freightmaster-evenements",
        "responseMode": "responseNode",
        "options": {
          "rawBody": true
        }
      },
      "id": "f0a1b2c3-0000-4000-8000-000000000503",
      "name": "Webhook FreightMaster",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        200,
        300
      ],
      "webhookId": "freightmaster-evenements"
    },
    {
      "parameters": {
        "operation": "text",
        "binaryPropertyName": "data",
        "destinationKey": "corps_brut",
        "options": {
          "keepSource": "json"
        }
      },
      "id": "f0a1b2c3-0000-4000-8000-000000000504",
      "name": "Corps brut",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        440,
        300
      ]
    },
    {
      "parameters": {
        "language": "javaScript",
        "jsCode": "// En-tête : FreightMaster-Signature: t=<horodatage>,v1=<signature>. La signature porte sur « t.corps », corps BRUT tel que reçu.\nconst entrees = $('Webhook FreightMaster').first().json.headers || {};\nconst brut = String(entrees['freightmaster-signature'] || '');\nconst parties = Object.fromEntries(brut.split(',').map((p) => p.trim().split('=')).filter((p) => p.length === 2));\nconst t = Number(parties.t);\nconst corps_brut = $input.first().json.corps_brut || '';\nreturn [{ json: {\n  corps_brut,\n  t: Number.isFinite(t) ? t : null,\n  v1: parties.v1 || '',\n  age_s: Number.isFinite(t) ? Math.abs(Math.floor(Date.now() / 1000) - t) : null,\n  a_signer: (Number.isFinite(t) ? t : 'x') + '.' + corps_brut,\n} }];"
      },
      "id": "f0a1b2c3-0000-4000-8000-000000000505",
      "name": "Lire la signature",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "action": "hmac",
        "type": "SHA256",
        "value": "={{ $json.a_signer }}",
        "dataPropertyName": "attendue",
        "encoding": "hex"
      },
      "id": "f0a1b2c3-0000-4000-8000-000000000506",
      "name": "Calculer la signature",
      "type": "n8n-nodes-base.crypto",
      "typeVersion": 2,
      "position": [
        920,
        300
      ],
      "credentials": {
        "crypto": {
          "id": "fmSecretWebhook1",
          "name": "FreightMaster secret de webhook"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "cond-1",
              "leftValue": "={{ $json.t !== null && $json.v1.length > 0 && $json.attendue === $json.v1 && $json.age_s <= 300 }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "f0a1b2c3-0000-4000-8000-000000000507",
      "name": "Signature valable ?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1160,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { recu: true } }}",
        "options": {
          "responseCode": 200
        }
      },
      "id": "f0a1b2c3-0000-4000-8000-000000000508",
      "name": "Répondre 200",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1400,
        210
      ]
    },
    {
      "parameters": {
        "language": "javaScript",
        "jsCode": "// Le corps est sûr : à vous de le transmettre à votre logiciel. Ignorez un « id » déjà reçu : une reprise peut le renvoyer.\nconst e = JSON.parse($('Calculer la signature').first().json.corps_brut);\nreturn [{ json: { id: e.id, type: e.type, sequence: e.sequence, livemode: e.livemode, expedition: e.data && e.data.shipment && e.data.shipment.id } }];"
      },
      "id": "f0a1b2c3-0000-4000-8000-000000000509",
      "name": "Événement vérifié",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1640,
        210
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { erreur: \"signature_invalide\" } }}",
        "options": {
          "responseCode": 401
        }
      },
      "id": "f0a1b2c3-0000-4000-8000-000000000510",
      "name": "Répondre 401",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1400,
        410
      ]
    }
  ],
  "connections": {
    "Webhook FreightMaster": {
      "main": [
        [
          {
            "node": "Corps brut",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Corps brut": {
      "main": [
        [
          {
            "node": "Lire la signature",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Lire la signature": {
      "main": [
        [
          {
            "node": "Calculer la signature",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculer la signature": {
      "main": [
        [
          {
            "node": "Signature valable ?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Signature valable ?": {
      "main": [
        [
          {
            "node": "Répondre 200",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Répondre 401",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Répondre 200": {
      "main": [
        [
          {
            "node": "Événement vérifié",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveManualExecutions": true,
    "saveDataSuccessExecution": "all",
    "saveDataErrorExecution": "all",
    "timezone": "Europe/Paris"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false,
    "freightmaster": "Modèle FreightMaster, voir README"
  },
  "tags": []
}
