-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator_digest.go
43 lines (37 loc) · 926 Bytes
/
validator_digest.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
package httpsign
import (
"bytes"
"io"
"net/http"
"github.com/thinkgos/http-signature-go/digest"
)
// DigestValidator checking digest in header match body
type DigestValidator struct {
digest digest.Digest
}
// NewDigestValidator return pointer of new DigestValidator
func NewDigestValidator(digest digest.Digest) *DigestValidator {
return &DigestValidator{digest: digest}
}
// Validate return error when checking digest match body
func (v *DigestValidator) Validate(r *http.Request, _ *Parameter) error {
if r.ContentLength == 0 {
return nil
}
headerDigest := r.Header.Get(Digest)
// FIXME: using buffer to prevent using too much memory
body, err := io.ReadAll(r.Body)
if err != nil {
return err
}
r.Body.Close()
r.Body = io.NopCloser(bytes.NewBuffer(body))
digest, err := v.digest.Sign(body)
if err != nil {
return err
}
if digest != headerDigest {
return ErrDigestMismatch
}
return nil
}