docs: fix generic webhook example

Signed-off-by: Raffael Sahli <raffael.sahli@doodle.com>
This commit is contained in:
Raffael Sahli 2023-06-20 13:37:07 +00:00
parent 4343908a70
commit 605b42dcae
No known key found for this signature in database
GPG Key ID: 5E0BF46A67AD81C4
1 changed files with 6 additions and 6 deletions

View File

@ -235,7 +235,7 @@ func verifySignature(signature string, payload, key []byte) error {
case "sha512": case "sha512":
newF = sha512.New newF = sha512.New
default: default:
return fmt.Errorf("unsupported signature algorithm %q", sigHdr[0]) return fmt.Errorf("unsupported signature algorithm %q", sig[0])
} }
mac := hmac.New(newF, key) mac := hmac.New(newF, key)
@ -244,22 +244,22 @@ func verifySignature(signature string, payload, key []byte) error {
} }
sum := fmt.Sprintf("%x", mac.Sum(nil)) sum := fmt.Sprintf("%x", mac.Sum(nil))
if sum != sig[0] { if sum != sig[1] {
return fmt.Errorf("HMACs do not match: %#v != %#v", sum, sigHdr[0]) return fmt.Errorf("HMACs do not match: %#v != %#v", sum, sig[1])
} }
return nil return nil
} }
func handleRequest(w http.ResponseWriter, r *http.Request) { func handleRequest(w http.ResponseWriter, r *http.Request) {
// Require a X-Signature header // Require a X-Signature header
if len(r.Header["X-Signature"])) == 0 { if len(r.Header["X-Signature"]) == 0 {
http.Error(w, "missing X-Signature header", http.StatusBadRequest) http.Error(w, "missing X-Signature header", http.StatusBadRequest)
return return
} }
// Read the request body with a limit of 1MB // Read the request body with a limit of 1MB
lr := io.LimitReader(r.Body, 1<<20) lr := io.LimitReader(r.Body, 1<<20)
body, err := ioutil.ReadAll(lr) body, err := io.ReadAll(lr)
if err != nil { if err != nil {
http.Error(w, "failed to read request body", http.StatusBadRequest) http.Error(w, "failed to read request body", http.StatusBadRequest)
return return
@ -269,7 +269,7 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
// Provider // Provider
key := "<token>" key := "<token>"
if err := verifySignature(r.Header.Get("X-Signature"), body, key); err != nil { if err := verifySignature(r.Header.Get("X-Signature"), body, key); err != nil {
http.Error(w, fmt.Sprintf("failed to verify HMAC signature: %s", err.String()), http.StatusBadRequest) http.Error(w, fmt.Sprintf("failed to verify HMAC signature: %s", err.Error()), http.StatusBadRequest)
return return
} }