Controllare le firme dei webhook

Panoramica #

Verificare gli eventi che Wooshpay invia agli endpoint webhook.

Intestazione della firma Wooshpay #

Il Wooshpay-Firma L'intestazione inclusa in ogni evento firmato contiene un timestamp e una firma.

Il timestamp è preceduto da t=e ogni firma è preceduta da v1.

Ad esempio:

Wooshpay-Signature:t=1492774577,v1=6fdfb9c357542b8ee07277f5fca2c6f728bae2dce9be2f91412f4de922c1bae4

Chiave segreta del webhook #

Wooshpay genera un unico chiave segreta per ogni webhook. whsec_

Prima di verificare le firme, è necessario recuperare il segreto del webhook da webhook.secret nell'oggetto webhook.

webhook-secret-key

Verifica delle firme #

Confrontando i valori di firma nell'intestazione al firma previstaÈ possibile verificare che i messaggi siano stati inviati da Wooshpay e non da terzi.

Per verificare la firma passo per passo, potete seguire la guida qui sotto:

Passo 1: Estrarre il timestamp e le firme dall'header #

Dividere l'intestazione, utilizzando l'opzione , come separatore, per ottenere un elenco di elementi. Quindi dividere ogni elemento, utilizzando l'opzione = come separatore, per ottenere una coppia di prefisso e valore.

Il valore del prefisso t corrisponde al timestamp e v1 corrisponde alla firma. Tutti gli altri elementi possono essere scartati.

Ad esempio:

t=1687845304,v1=6fdfb9c357542b8ee07277f5fca2c6f728bae2dce9be2f91412f4de922c1bae4

Fase 2: Preparare il carico_firmato stringa #

Il carico_firmato viene creata concatenando le stringhe:

  • Il timestamp (come stringa)
  • Il personaggio .
  • Il payload JSON effettivo (il corpo della richiesta)

Ad esempio:

1687845304+.+Carico utile JSON

1687845304.{
  "id": "evt_1NNUrjL6kclEVx6Mb1x5dKJ3",
  "oggetto": "event",
  "api_version": "2022-11-15",
  "creato": 1687845303,
  "dati": {
    "oggetto": {
      "id": "prod_O9oUVgsSaordCT",
      "oggetto": "product",
      "active": true,
      "livemode": true,
      "nome": "test",
      "tipo": "service",
  "livemode": true,
  "pending_webhooks": 1,
  "tipo": "product.created"
}

Fase 3: Determinazione della firma prevista #

Calcolo di un HMAC con la funzione di hash SHA256.

  • Usare il segreto del webhook dell'endpoint come parametro chiave, che si può ottenere da webhook.secret nell'oggetto webhook
  • Utilizzare il carico_firmato come stringa messaggio che avete già preparato al punto 2.

Attraverso il chiave + messaggio, è possibile calcolare una stringa HMAC, che è firma prevista

Ad esempio:

 public static void main(String[] args) {
        String webhookSecret = "whsec_261V2mfsXt1BsOjJbHaQOxnTzhWZKrUE";
        String timestamp = "1687845304";
        String requestBody = "{\"id\":\"evt_1NNUrjL6kclEVx6Mb1x5dKJ3\",\"object\":\"event\",\"api_version\":\"2022-11-15\",\"created\":1687845303,\"data\":{\"object\":{\"id\":\"prod_O9oUVgsSaordCT\",\"object\":\"product\",\"active\":true,\"livemode\":true,\"name\":\"test\",\"type\":\"service\",\"livemode\":true,\"pending_webhooks\":1,\"type\":\"product.created\"}";
        String signedPayload = timestamp+". "+requestBody;
        String signature = hmacSha256(webhookSecret,signedPayload);
        String WooshpaySignature = "t="+timestamp+",v1="+firma";
    }

    /**
     * HMAC-SHA256
     */
    public static String hmacSha256(String secret, String message) {
        String res;
        try {
            Mac mac = Mac.getInstance("HmacSHA256");
            SecretKey secretKey = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
            mac.init(secretKey);
            byte[] hash = mac.doFinal(message.getBytes());
            res = Hex.encodeHexString(hash);
        } catch (Exception e) {
            return null;
        }
        return res;
    }

Fase 4: Confronto delle firme #

Confrontare il firma nell'intestazione al firma prevista.

Per una corrispondenza di uguaglianza, calcolare la differenza tra i valori di data e ora attuali e il timestamp ricevutoPoi decidete se la differenza rientra nella vostra tolleranza.

Per proteggersi dagli attacchi temporali, è possibile utilizzare un confronto di stringhe a tempo costante per confrontare la firma prevista con ciascuna delle firme ricevute.

Quali sono i vostri sentimenti
Aggiornato il 5 agosto 2023