webhookのリクエストには、下記のHeaderが付与されます。

KEY 内容
X-HMAC Hash Based Message Authentication Codeです。通信が正規のものかどうかを検証する際に利用できます。独自のAPIと連携をする場合は、この文字列を利用して通信の検証を行ってください。

この値を利用することで、リクエストが正規のものかどうか検証が可能です。

独自のAPIを実装し、miiboと連携を行う場合は検証を行うことを推奨します。

検証に必要なもの

検証のサンプルコード

上記の必要なものを用意し、下記のようなコードで検証を行ってください。

(変数の定義)

code: Headerの「X-HMAC」に紐づく値 (string)

payload: リクエストに含まれるボディのJSON全体

secret: Webhookに紐づくシークレット

Go

import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/hex"
)

func VerifyCode(secret string, payload []byte, code string) bool {
	h := hmac.New(sha256.New, []byte(secret))
	h.Write(payload)
	expectedMAC := h.Sum(nil)

	receivedMAC, err := hex.DecodeString(code)
	if err != nil {
		// 適切にエラーハンドリングを行う
		return false
	}
	return hmac.Equal(expectedMAC, receivedMAC)
}

Python

import hmac
import hashlib

def verify_code(secret, payload, code):
    h = hmac.new(secret.encode(), payload, hashlib.sha256)
    calculated_hmac = h.hexdigest()
    return hmac.compare_digest(calculated_hmac, code)