| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io/ioutil" |
| 6 | "net/http" |
| 7 | |
| 8 | "github.com/slack-go/slack" |
| 9 | ) |
| 10 | |
| 11 | func (v *SecretsVerifierMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 12 | body, err := ioutil.ReadAll(r.Body) |
| 13 | if err != nil { |
| 14 | w.WriteHeader(http.StatusBadRequest) |
| 15 | return |
| 16 | } |
| 17 | r.Body.Close() |
| 18 | r.Body = ioutil.NopCloser(bytes.NewBuffer(body)) |
| 19 | |
| 20 | sv, err := slack.NewSecretsVerifier(r.Header, appCtx.config.signingSecret) |
| 21 | if err != nil { |
| 22 | w.WriteHeader(http.StatusBadRequest) |
| 23 | return |
| 24 | } |
| 25 | |
| 26 | if _, err := sv.Write(body); err != nil { |
| 27 | w.WriteHeader(http.StatusInternalServerError) |
| 28 | return |
| 29 | } |
| 30 | |
| 31 | if err := sv.Ensure(); err != nil { |
| 32 | w.WriteHeader(http.StatusUnauthorized) |
| 33 | return |
| 34 | } |
| 35 | |
| 36 | v.handler.ServeHTTP(w, r) |
| 37 | } |
| 38 | |
| 39 | func NewSecretsVerifierMiddleware(h http.Handler) *SecretsVerifierMiddleware { |
| 40 | return &SecretsVerifierMiddleware{h} |
| 41 | } |