-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler.go
51 lines (42 loc) · 1.11 KB
/
handler.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
package hmac
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (m HMAC) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
if r.Body == nil {
// nothing to do
return next.ServeHTTP(w, r)
}
body, err := copyRequestBody(r)
if err != nil {
return err
}
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
secret := repl.ReplaceAll(m.Secret, "")
signature := generateSignature(m.hasher, secret, body)
if err != nil {
return err
}
repl.Set(m.replacerKey(), signature)
return next.ServeHTTP(w, r)
}
// copyRequestBody copies the request body while making it reusable.
// It returns the copied []byte.
func copyRequestBody(r *http.Request) ([]byte, error) {
bodyCopy := bytes.Buffer{}
tee := io.TeeReader(r.Body, &bodyCopy)
body, err := ioutil.ReadAll(tee)
if err != nil {
return nil, err
}
// replace the body
r.Body = ioutil.NopCloser(bytes.NewReader(body))
// return the copy
return bodyCopy.Bytes(), nil
}