-
Notifications
You must be signed in to change notification settings - Fork 2
/
webhook.go
182 lines (163 loc) · 5.3 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package webhooks
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"time"
"go.uber.org/zap"
"go.autokitteh.dev/autokitteh/internal/kittehs"
eventsv1 "go.autokitteh.dev/autokitteh/proto/gen/go/autokitteh/events/v1"
"go.autokitteh.dev/autokitteh/sdk/sdkservices"
"go.autokitteh.dev/autokitteh/sdk/sdktypes"
"go.autokitteh.dev/autokitteh/integrations/internal/extrazap"
"go.autokitteh.dev/autokitteh/integrations/slack/api"
)
const (
// signingSecretEnvVar is the name of an environment variable that contains
// a Slack app SECRET which is required to verify inbound request signatures.
signingSecretEnvVar = "SLACK_SIGNING_SECRET"
// The maximum shift/delay that we allow between an inbound request's
// timestamp, and our current timestamp.
maxDifference = 5 * time.Minute
// Slack API implementation detail.
slackSigVersion = "v0"
)
// handler is a collection of autokitteh webhooks which implement [http.Handler]
// to receive, dispatch, and acknowledge asynchronous event notifications.
type handler struct {
logger *zap.Logger
secrets sdkservices.Secrets
dispatcher sdkservices.Dispatcher
integrationID sdktypes.IntegrationID
scope string
}
func NewHandler(l *zap.Logger, sec sdkservices.Secrets, d sdkservices.Dispatcher, scope string, id sdktypes.IntegrationID) handler {
return handler{
logger: l,
secrets: sec,
dispatcher: d,
scope: scope,
integrationID: id,
}
}
// checkRequest checks that the given HTTP request has a valid content type and
// a valid Slack signature, and if so it returns the request's body. Otherwise
// it returns nil, and sends an HTTP error to the Slack platform's client.
func checkRequest(w http.ResponseWriter, r *http.Request, l *zap.Logger, wantContentType string) []byte {
// "Content-Type" header.
gotContentType := r.Header.Get(api.HeaderContentType)
if gotContentType == "" || gotContentType != wantContentType {
l.Error("Unexpected header value",
zap.String("header", api.HeaderContentType),
zap.String("got", gotContentType),
zap.String("want", wantContentType),
)
http.Error(w, "Bad Request", http.StatusBadRequest)
return nil
}
// "X-Slack-Request-Timestamp" header.
ts := r.Header.Get(api.HeaderSlackTimestamp)
if ts == "" {
l.Warn("Missing header",
zap.String("header", api.HeaderSlackTimestamp),
)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return nil
}
secs, err := strconv.ParseInt(ts, 10, 64)
if err != nil {
l.Warn("Invalid header value",
zap.String("header", api.HeaderSlackTimestamp),
zap.String("value", ts),
)
http.Error(w, "Forbidden", http.StatusForbidden)
return nil
}
d := time.Since(time.Unix(secs, 0))
if d.Abs() > maxDifference {
l.Warn("Unacceptable header value",
zap.String("header", api.HeaderSlackTimestamp),
zap.String("difference", fmt.Sprint(d)),
)
http.Error(w, "Forbidden", http.StatusForbidden)
return nil
}
// "X-Slack-Signature" header.
sig := r.Header.Get(api.HeaderSlackSignature)
if sig == "" {
l.Warn("Missing header",
zap.String("header", api.HeaderSlackSignature),
)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return nil
}
// Request body.
b, err := io.ReadAll(r.Body)
if err != nil {
l.Error("Failed to read inbound HTTP request body",
zap.Error(err),
)
http.Error(w, "Bad Request", http.StatusBadRequest)
return nil
}
signingSecret := os.Getenv(signingSecretEnvVar)
if !verifySignature(signingSecret, ts, sig, b) {
l.Error("Slack signature verification failed")
http.Error(w, "Forbidden", http.StatusForbidden)
return nil
}
return b
}
// verifySignature implements https://api.slack.com/authentication/verifying-requests-from-slack.
func verifySignature(signingSecret, ts, want string, body []byte) bool {
mac := hmac.New(sha256.New, []byte(signingSecret))
n, err := mac.Write([]byte(fmt.Sprintf("%s:%s:", slackSigVersion, ts)))
if err != nil || n != len(ts)+4 {
return false
}
if n, err := mac.Write(body); err != nil || n != len(body) {
return false
}
got := fmt.Sprintf("%s=%s", slackSigVersion, hex.EncodeToString(mac.Sum(nil)))
return hmac.Equal([]byte(got), []byte(want))
}
// listTokens calls the List method in SecretsService.
func (h handler) listTokens(appID, enterpriseID, teamID string) ([]string, error) {
k := appSecretName(appID, enterpriseID, teamID)
tokens, err := h.secrets.List(context.Background(), h.scope, k)
if err != nil {
return nil, err
}
return tokens, nil
}
func appSecretName(appID, enterpriseID, teamID string) string {
s := fmt.Sprintf("apps/%s/%s/%s", appID, enterpriseID, teamID)
// Slack enterprise ID is allowed to be empty.
return strings.ReplaceAll(s, "//", "/")
}
func (h handler) dispatchAsyncEventsToConnections(l *zap.Logger, tokens []string, event *eventsv1.Event) {
ctx := extrazap.AttachLoggerToContext(l, context.Background())
for _, connToken := range tokens {
event.IntegrationToken = connToken
event := kittehs.Must1(sdktypes.EventFromProto(event))
eventID, err := h.dispatcher.Dispatch(ctx, event, nil)
if err != nil {
l.Error("Dispatch failed",
zap.String("connectionToken", connToken),
zap.Error(err),
)
return
}
l.Debug("Dispatched",
zap.String("connectionToken", connToken),
zap.String("eventID", eventID.String()),
)
}
}