generated from nakatanakatana/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
52 lines (39 loc) · 1021 Bytes
/
middleware.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
package slackworkflowbot
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"github.com/slack-go/slack"
)
func (v *SecretsVerifierMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
r.Body.Close()
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
sv, err := slack.NewSecretsVerifier(r.Header, string(v.signingSecret))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if _, err := sv.Write(body); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if err := sv.Ensure(); err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
}
func NewSecretsVerifierMiddleware(secret SigningSecret) Middleware {
secretsVerifier := &SecretsVerifierMiddleware{secret}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
secretsVerifier.ServeHTTP(w, r)
next.ServeHTTP(w, r)
})
}
}