-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
85 lines (66 loc) · 1.81 KB
/
http.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
package webhook
import (
"net/http"
"strings"
"github.com/userhubdev/go-sdk/internal"
"github.com/userhubdev/go-sdk/types"
)
// Request is a wrapper for webhook requests.
type Request struct {
Headers http.Header
Body []byte
}
// GetAction returns the webhook action.
func (r *Request) GetAction() (string, error) {
var name string
if r != nil {
name = r.Headers.Get(internal.WebhookActionHeader)
}
if name == "" {
return "", types.NewError("%s header is missing", internal.WebhookActionHeader)
}
return name, nil
}
// GetTimestamp returns the webhook timestamp.
func (r *Request) GetTimestamp() (string, error) {
timestamp := r.Headers.Get(internal.WebhookTimestampHeader)
if timestamp == "" {
return "", types.NewError("%s header is missing", internal.WebhookTimestampHeader)
}
return timestamp, nil
}
// GetSignatures returns the webhook signatures.
func (r *Request) GetSignatures() ([]string, error) {
var signatures []string
headers := r.Headers.Values(internal.WebhookSignatureHeader)
if len(headers) == 0 {
return nil, types.NewError("%s header is missing", internal.WebhookSignatureHeader)
}
for _, header := range headers {
header = strings.TrimSpace(header)
if header == "" {
continue
}
if !strings.Contains(header, ",") {
signatures = append(signatures, header)
continue
}
headerParts := strings.Split(header, ",")
for _, signature := range headerParts {
signature = strings.TrimSpace(signature)
if signature != "" {
signatures = append(signatures, signature)
}
}
}
if len(signatures) == 0 {
return nil, types.NewError("%s header normalized to nothing", internal.WebhookSignatureHeader)
}
return signatures, nil
}
// Response is a wrapper for webhook responses.
type Response struct {
StatusCode int
Headers http.Header
Body []byte
}