-
Notifications
You must be signed in to change notification settings - Fork 21
/
webhook.go
100 lines (86 loc) · 2.36 KB
/
webhook.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package onfido
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"os"
)
// Constants
const (
WebhookSignatureHeader = "X-Signature"
WebhookTokenEnv = "ONFIDO_WEBHOOK_TOKEN"
)
// Webhook errors
var (
ErrInvalidWebhookSignature = errors.New("invalid request, payload hash doesn't match signature")
ErrMissingWebhookToken = errors.New("webhook token not found in environmental variable")
)
// Webhook represents a webhook handler
type Webhook struct {
Token string
SkipSignatureValidation bool
}
// WebhookRequest represents an incoming webhook request from Onfido
type WebhookRequest struct {
Payload struct {
ResourceType string `json:"resource_type"`
Action string `json:"action"`
Object struct {
ID string `json:"id"`
Status string `json:"status"`
CompletedAt string `json:"completed_at"`
Href string `json:"href"`
} `json:"object"`
} `json:"payload"`
}
// NewWebhookFromEnv creates a new webhook handler using
// configuration from environment variables.
func NewWebhookFromEnv() (*Webhook, error) {
token := os.Getenv(WebhookTokenEnv)
if token == "" {
return nil, ErrMissingWebhookToken
}
return NewWebhook(token), nil
}
// NewWebhook creates a new webhook handler
func NewWebhook(token string) *Webhook {
return &Webhook{
Token: token,
}
}
// ValidateSignature validates the request body against the signature header.
func (wh *Webhook) ValidateSignature(body []byte, signature string) error {
mac := hmac.New(sha1.New, []byte(wh.Token))
if _, err := mac.Write(body); err != nil {
return err
}
sig, err := hex.DecodeString(signature)
if err != nil || !hmac.Equal(sig, mac.Sum(nil)) {
return ErrInvalidWebhookSignature
}
return nil
}
// ParseFromRequest parses the webhook request body and returns
// it as WebhookRequest if the request signature is valid.
func (wh *Webhook) ParseFromRequest(req *http.Request) (*WebhookRequest, error) {
signature := req.Header.Get(WebhookSignatureHeader)
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, err
}
defer req.Body.Close()
if !wh.SkipSignatureValidation {
if err := wh.ValidateSignature(body, signature); err != nil {
return nil, err
}
}
var wr WebhookRequest
if err := json.Unmarshal(body, &wr); err != nil {
return nil, err
}
return &wr, nil
}