@@ -52,16 +52,6 @@ func stringToTime(s string) (time.Time, error) {
52
52
return time .Unix (sec , 0 ), nil
53
53
}
54
54
55
- // HMACSHA256 generates HMACS enconded hashes using the provided Key and SHA256
56
- // encoding for the message.
57
- func hMACSHA256 (message , key []byte ) ([]byte , error ) {
58
- mac := hmac .New (sha256 .New , []byte (key ))
59
- if _ , err := mac .Write (message ); err != nil {
60
- return nil , err
61
- }
62
- return mac .Sum (nil ), nil
63
- }
64
-
65
55
// Validator type represents a MessageBird signature validator.
66
56
type Validator struct {
67
57
SigningKey string // Signing Key provided by MessageBird.
@@ -94,7 +84,11 @@ func (v *Validator) calculateSignature(ts, qp string, b []byte) ([]byte, error)
94
84
var m bytes.Buffer
95
85
bh := sha256 .Sum256 (b )
96
86
fmt .Fprintf (& m , "%s\n %s\n %s" , ts , qp , bh [:])
97
- return hMACSHA256 (m .Bytes (), []byte (v .SigningKey ))
87
+ mac := hmac .New (sha256 .New , []byte (v .SigningKey ))
88
+ if _ , err := mac .Write (m .Bytes ()); err != nil {
89
+ return nil , err
90
+ }
91
+ return mac .Sum (nil ), nil
98
92
}
99
93
100
94
// validSignature takes the timestamp, query params and body from the request,
@@ -117,8 +111,6 @@ func (v *Validator) validSignature(ts, rqp string, b []byte, rs string) bool {
117
111
118
112
// ValidRequest is a method that takes care of the signature validation of
119
113
// incoming requests.
120
- // To use just pass the request:
121
- // signature.Validate(request)
122
114
func (v * Validator ) ValidRequest (r * http.Request ) error {
123
115
ts := r .Header .Get (tsHeader )
124
116
rs := r .Header .Get (sHeader )
@@ -136,8 +128,6 @@ func (v *Validator) ValidRequest(r *http.Request) error {
136
128
// Validate is a handler wrapper that takes care of the signature validation of
137
129
// incoming requests and rejects them if invalid or pass them on to your handler
138
130
// otherwise.
139
- // To use just wrap your handler with it:
140
- // http.Handle("/path", signature.Validate(handleThing))
141
131
func (v * Validator ) Validate (h http.Handler ) http.Handler {
142
132
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
143
133
if err := v .ValidRequest (r ); err != nil {
0 commit comments