/
webhook_notification_gateway.go
54 lines (46 loc) · 1.3 KB
/
webhook_notification_gateway.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package braintree
import (
"encoding/base64"
"encoding/xml"
"github.com/braintree-go/braintree-go/xmlnil"
"net/http"
)
type WebhookNotificationGateway struct {
*Braintree
apiKey apiKey
}
func (w *WebhookNotificationGateway) ParseRequest(r *http.Request) (*WebhookNotification, error) {
signature := r.PostFormValue("bt_signature")
payload := r.PostFormValue("bt_payload")
return w.Parse(signature, payload)
}
func (w *WebhookNotificationGateway) Parse(signature, payload string) (*WebhookNotification, error) {
hmacer := newHmacer(w.apiKey.publicKey, w.apiKey.privateKey)
if verified, err := hmacer.verifySignature(signature, payload); err != nil {
return nil, err
} else if !verified {
return nil, SignatureError{}
}
xmlNotification, err := base64.StdEncoding.DecodeString(payload)
if err != nil {
return nil, err
}
strippedBuf, err := xmlnil.StripNilElements(xmlNotification)
if err != nil {
return nil, err
}
var n WebhookNotification
err = xml.Unmarshal(strippedBuf, &n)
if err != nil {
return nil, err
}
return &n, nil
}
func (w *WebhookNotificationGateway) Verify(challenge string) (string, error) {
hmacer := newHmacer(w.apiKey.publicKey, w.apiKey.privateKey)
digest, err := hmacer.hmac(challenge)
if err != nil {
return ``, err
}
return hmacer.publicKey + `|` + digest, nil
}