@@ -12,7 +12,6 @@ import (
12
12
"encoding/json"
13
13
"errors"
14
14
"fmt"
15
- "hash"
16
15
"io/ioutil"
17
16
"log"
18
17
"math"
@@ -49,19 +48,13 @@ const (
49
48
50
49
// SignatureError describes an invalid payload signature passed to Hook.
51
50
type SignatureError struct {
52
- Signature string
53
- Signatures []string
51
+ Signature string
54
52
}
55
53
56
54
func (e * SignatureError ) Error () string {
57
55
if e == nil {
58
56
return "<nil>"
59
57
}
60
-
61
- if e .Signatures != nil {
62
- return fmt .Sprintf ("invalid payload signatures %s" , e .Signatures )
63
- }
64
-
65
58
return fmt .Sprintf ("invalid payload signature %s" , e .Signature )
66
59
}
67
60
@@ -101,67 +94,25 @@ func (e *ParseError) Error() string {
101
94
return e .Err .Error ()
102
95
}
103
96
104
- // ExtractCommaSeparatedValues will extract the values matching the key.
105
- func ExtractCommaSeparatedValues (source , prefix string ) []string {
106
- parts := strings .Split (source , "," )
107
- values := make ([]string , 0 )
108
- for _ , part := range parts {
109
- if strings .HasPrefix (part , prefix ) {
110
- values = append (values , strings .TrimPrefix (part , prefix ))
111
- }
112
- }
113
-
114
- return values
115
- }
116
-
117
- // ExtractSignatures will extract all the signatures from the source.
118
- func ExtractSignatures (source , prefix string ) []string {
119
- // If there are multiple possible matches, let the comma seperated extractor
120
- // do it's work.
121
- if strings .Contains (source , "," ) {
122
- return ExtractCommaSeparatedValues (source , prefix )
97
+ // CheckPayloadSignature calculates and verifies SHA1 signature of the given payload
98
+ func CheckPayloadSignature (payload []byte , secret string , signature string ) (string , error ) {
99
+ if secret == "" {
100
+ return "" , errors .New ("signature validation secret can not be empty" )
123
101
}
124
102
125
- // There were no commas, so just trim the prefix (if it even exists) and
126
- // pass it back.
127
- return []string {
128
- strings .TrimPrefix (source , prefix ),
129
- }
130
- }
103
+ signature = strings .TrimPrefix (signature , "sha1=" )
131
104
132
- // ValidateMAC will verify that the expected mac for the given hash will match
133
- // the one provided.
134
- func ValidateMAC (payload []byte , mac hash.Hash , signatures []string ) (string , error ) {
135
- // Write the payload to the provided hash.
105
+ mac := hmac .New (sha1 .New , []byte (secret ))
136
106
_ , err := mac .Write (payload )
137
107
if err != nil {
138
108
return "" , err
139
109
}
140
-
141
110
expectedMAC := hex .EncodeToString (mac .Sum (nil ))
142
111
143
- for _ , signature := range signatures {
144
- if hmac .Equal ([]byte (signature ), []byte (expectedMAC )) {
145
- return expectedMAC , err
146
- }
147
- }
148
-
149
- return expectedMAC , & SignatureError {
150
- Signatures : signatures ,
112
+ if ! hmac .Equal ([]byte (signature ), []byte (expectedMAC )) {
113
+ return expectedMAC , & SignatureError {signature }
151
114
}
152
- }
153
-
154
- // CheckPayloadSignature calculates and verifies SHA1 signature of the given payload
155
- func CheckPayloadSignature (payload []byte , secret string , signature string ) (string , error ) {
156
- if secret == "" {
157
- return "" , errors .New ("signature validation secret can not be empty" )
158
- }
159
-
160
- // Extract the signatures.
161
- signatures := ExtractSignatures (signature , "sha1=" )
162
-
163
- // Validate the MAC.
164
- return ValidateMAC (payload , hmac .New (sha1 .New , []byte (secret )), signatures )
115
+ return expectedMAC , err
165
116
}
166
117
167
118
// CheckPayloadSignature256 calculates and verifies SHA256 signature of the given payload
@@ -170,11 +121,19 @@ func CheckPayloadSignature256(payload []byte, secret string, signature string) (
170
121
return "" , errors .New ("signature validation secret can not be empty" )
171
122
}
172
123
173
- // Extract the signatures.
174
- signatures := ExtractSignatures (signature , "sha256=" )
124
+ signature = strings .TrimPrefix (signature , "sha256=" )
125
+
126
+ mac := hmac .New (sha256 .New , []byte (secret ))
127
+ _ , err := mac .Write (payload )
128
+ if err != nil {
129
+ return "" , err
130
+ }
131
+ expectedMAC := hex .EncodeToString (mac .Sum (nil ))
175
132
176
- // Validate the MAC.
177
- return ValidateMAC (payload , hmac .New (sha256 .New , []byte (secret )), signatures )
133
+ if ! hmac .Equal ([]byte (signature ), []byte (expectedMAC )) {
134
+ return expectedMAC , & SignatureError {signature }
135
+ }
136
+ return expectedMAC , err
178
137
}
179
138
180
139
// CheckPayloadSignature512 calculates and verifies SHA512 signature of the given payload
@@ -183,11 +142,19 @@ func CheckPayloadSignature512(payload []byte, secret string, signature string) (
183
142
return "" , errors .New ("signature validation secret can not be empty" )
184
143
}
185
144
186
- // Extract the signatures.
187
- signatures := ExtractSignatures (signature , "sha512=" )
145
+ signature = strings .TrimPrefix (signature , "sha512=" )
146
+
147
+ mac := hmac .New (sha512 .New , []byte (secret ))
148
+ _ , err := mac .Write (payload )
149
+ if err != nil {
150
+ return "" , err
151
+ }
152
+ expectedMAC := hex .EncodeToString (mac .Sum (nil ))
188
153
189
- // Validate the MAC.
190
- return ValidateMAC (payload , hmac .New (sha512 .New , []byte (secret )), signatures )
154
+ if ! hmac .Equal ([]byte (signature ), []byte (expectedMAC )) {
155
+ return expectedMAC , & SignatureError {signature }
156
+ }
157
+ return expectedMAC , err
191
158
}
192
159
193
160
func CheckScalrSignature (headers map [string ]interface {}, body []byte , signingKey string , checkDate bool ) (bool , error ) {
@@ -210,7 +177,7 @@ func CheckScalrSignature(headers map[string]interface{}, body []byte, signingKey
210
177
expectedSignature := hex .EncodeToString (mac .Sum (nil ))
211
178
212
179
if ! hmac .Equal ([]byte (providedSignature ), []byte (expectedSignature )) {
213
- return false , & SignatureError {Signature : providedSignature }
180
+ return false , & SignatureError {providedSignature }
214
181
}
215
182
216
183
if ! checkDate {
@@ -225,7 +192,7 @@ func CheckScalrSignature(headers map[string]interface{}, body []byte, signingKey
225
192
delta := math .Abs (now .Sub (date ).Seconds ())
226
193
227
194
if delta > 300 {
228
- return false , & SignatureError {Signature : "outdated" }
195
+ return false , & SignatureError {"outdated" }
229
196
}
230
197
return true , nil
231
198
}
0 commit comments