-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
39 lines (34 loc) · 860 Bytes
/
Copy pathexample_test.go
File metadata and controls
39 lines (34 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package webhooks_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"github.com/aatuh/api-toolkit/v4/webhooks"
)
func ExampleNewHMACSHA256Verifier() {
body := []byte(`{"id":"evt_123"}`)
signer, err := webhooks.NewHMACSHA256Signer(webhooks.HMACSignerConfig{
Secret: []byte("secret"),
})
if err != nil {
panic(err)
}
signature, err := signer.SignWebhook(context.Background(), body)
if err != nil {
panic(err)
}
verifier, err := webhooks.NewHMACSHA256Verifier(webhooks.HMACConfig{
Secret: []byte("secret"),
HeaderName: "X-Signature",
})
if err != nil {
panic(err)
}
req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/webhooks", nil)
req.Header.Set("X-Signature", signature)
err = verifier.VerifyWebhook(context.Background(), req, body)
fmt.Println(err == nil)
// Output:
// true
}