mirror of
https://github.com/adnanh/webhook.git
synced 2025-05-11 08:04:44 +00:00
Apply gofmt
This commit is contained in:
parent
48061f1508
commit
8d260c6a7e
3 changed files with 89 additions and 89 deletions
84
hook/hook.go
84
hook/hook.go
|
@ -11,8 +11,8 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math"
|
|
||||||
"log"
|
"log"
|
||||||
|
"math"
|
||||||
"net"
|
"net"
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
"os"
|
"os"
|
||||||
|
@ -129,44 +129,44 @@ func CheckPayloadSignature256(payload []byte, secret string, signature string) (
|
||||||
}
|
}
|
||||||
return expectedMAC, err
|
return expectedMAC, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckScalrSignature(headers map[string]interface{}, body []byte, signingKey string, checkDate bool) (bool, error) {
|
func CheckScalrSignature(headers map[string]interface{}, body []byte, signingKey string, checkDate bool) (bool, error) {
|
||||||
// Check for the signature and date headers
|
// Check for the signature and date headers
|
||||||
if _, ok := headers["X-Signature"]; !ok {
|
if _, ok := headers["X-Signature"]; !ok {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
if _, ok := headers["Date"]; !ok {
|
if _, ok := headers["Date"]; !ok {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
providedSignature := headers["X-Signature"].(string)
|
providedSignature := headers["X-Signature"].(string)
|
||||||
dateHeader := headers["Date"].(string)
|
dateHeader := headers["Date"].(string)
|
||||||
mac := hmac.New(sha1.New, []byte(signingKey))
|
mac := hmac.New(sha1.New, []byte(signingKey))
|
||||||
mac.Write(body)
|
mac.Write(body)
|
||||||
mac.Write([]byte(dateHeader))
|
mac.Write([]byte(dateHeader))
|
||||||
expectedSignature := hex.EncodeToString(mac.Sum(nil))
|
expectedSignature := hex.EncodeToString(mac.Sum(nil))
|
||||||
|
|
||||||
if !hmac.Equal([]byte(providedSignature), []byte(expectedSignature)) {
|
if !hmac.Equal([]byte(providedSignature), []byte(expectedSignature)) {
|
||||||
return false, &SignatureError{providedSignature}
|
return false, &SignatureError{providedSignature}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !checkDate {
|
if !checkDate {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
// Example format: Fri 08 Sep 2017 11:24:32 UTC
|
// Example format: Fri 08 Sep 2017 11:24:32 UTC
|
||||||
date, err := time.Parse("Mon 02 Jan 2006 15:04:05 MST", dateHeader)
|
date, err := time.Parse("Mon 02 Jan 2006 15:04:05 MST", dateHeader)
|
||||||
//date, err := time.Parse(time.RFC1123, dateHeader)
|
//date, err := time.Parse(time.RFC1123, dateHeader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
delta := math.Abs(now.Sub(date).Seconds())
|
delta := math.Abs(now.Sub(date).Seconds())
|
||||||
|
|
||||||
if delta > 300 {
|
if delta > 300 {
|
||||||
return false, &SignatureError{"outdated"}
|
return false, &SignatureError{"outdated"}
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckIPWhitelist makes sure the provided remote address (of the form IP:port) falls within the provided IP range
|
// CheckIPWhitelist makes sure the provided remote address (of the form IP:port) falls within the provided IP range
|
||||||
// (in CIDR form or a single IP address).
|
// (in CIDR form or a single IP address).
|
||||||
func CheckIPWhitelist(remoteAddr string, ipRange string) (bool, error) {
|
func CheckIPWhitelist(remoteAddr string, ipRange string) (bool, error) {
|
||||||
|
@ -751,10 +751,10 @@ func (r MatchRule) Evaluate(headers, query, payload *map[string]interface{}, bod
|
||||||
if r.Type == IPWhitelist {
|
if r.Type == IPWhitelist {
|
||||||
return CheckIPWhitelist(remoteAddr, r.IPRange)
|
return CheckIPWhitelist(remoteAddr, r.IPRange)
|
||||||
}
|
}
|
||||||
if r.Type == ScalrSignature {
|
if r.Type == ScalrSignature {
|
||||||
return CheckScalrSignature(*headers, *body, r.Secret, true)
|
return CheckScalrSignature(*headers, *body, r.Secret, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
if arg, ok := r.Parameter.Get(headers, query, payload); ok {
|
if arg, ok := r.Parameter.Get(headers, query, payload); ok {
|
||||||
switch r.Type {
|
switch r.Type {
|
||||||
case MatchValue:
|
case MatchValue:
|
||||||
|
|
|
@ -60,52 +60,52 @@ func TestCheckPayloadSignature256(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var checkScalrSignatureTests = []struct {
|
var checkScalrSignatureTests = []struct {
|
||||||
description string
|
description string
|
||||||
headers map[string]interface{}
|
headers map[string]interface{}
|
||||||
payload []byte
|
payload []byte
|
||||||
secret string
|
secret string
|
||||||
expectedSignature string
|
expectedSignature string
|
||||||
ok bool
|
ok bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"Valid signature",
|
"Valid signature",
|
||||||
map[string]interface{}{"Date": "Thu 07 Sep 2017 06:30:04 UTC", "X-Signature": "48e395e38ac48988929167df531eb2da00063a7d"},
|
map[string]interface{}{"Date": "Thu 07 Sep 2017 06:30:04 UTC", "X-Signature": "48e395e38ac48988929167df531eb2da00063a7d"},
|
||||||
[]byte(`{"a": "b"}`), "bilFGi4ZVZUdG+C6r0NIM9tuRq6PaG33R3eBUVhLwMAErGBaazvXe4Gq2DcJs5q+",
|
[]byte(`{"a": "b"}`), "bilFGi4ZVZUdG+C6r0NIM9tuRq6PaG33R3eBUVhLwMAErGBaazvXe4Gq2DcJs5q+",
|
||||||
"48e395e38ac48988929167df531eb2da00063a7d", true,
|
"48e395e38ac48988929167df531eb2da00063a7d", true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"Wrong signature",
|
"Wrong signature",
|
||||||
map[string]interface{}{"Date": "Thu 07 Sep 2017 06:30:04 UTC", "X-Signature": "999395e38ac48988929167df531eb2da00063a7d"},
|
map[string]interface{}{"Date": "Thu 07 Sep 2017 06:30:04 UTC", "X-Signature": "999395e38ac48988929167df531eb2da00063a7d"},
|
||||||
[]byte(`{"a": "b"}`), "bilFGi4ZVZUdG+C6r0NIM9tuRq6PaG33R3eBUVhLwMAErGBaazvXe4Gq2DcJs5q+",
|
[]byte(`{"a": "b"}`), "bilFGi4ZVZUdG+C6r0NIM9tuRq6PaG33R3eBUVhLwMAErGBaazvXe4Gq2DcJs5q+",
|
||||||
"48e395e38ac48988929167df531eb2da00063a7d", false,
|
"48e395e38ac48988929167df531eb2da00063a7d", false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"Missing Date header",
|
"Missing Date header",
|
||||||
map[string]interface{}{"X-Signature": "999395e38ac48988929167df531eb2da00063a7d"},
|
map[string]interface{}{"X-Signature": "999395e38ac48988929167df531eb2da00063a7d"},
|
||||||
[]byte(`{"a": "b"}`), "bilFGi4ZVZUdG+C6r0NIM9tuRq6PaG33R3eBUVhLwMAErGBaazvXe4Gq2DcJs5q+",
|
[]byte(`{"a": "b"}`), "bilFGi4ZVZUdG+C6r0NIM9tuRq6PaG33R3eBUVhLwMAErGBaazvXe4Gq2DcJs5q+",
|
||||||
"48e395e38ac48988929167df531eb2da00063a7d", false,
|
"48e395e38ac48988929167df531eb2da00063a7d", false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"Missing X-Signature header",
|
"Missing X-Signature header",
|
||||||
map[string]interface{}{"Date": "Thu 07 Sep 2017 06:30:04 UTC"},
|
map[string]interface{}{"Date": "Thu 07 Sep 2017 06:30:04 UTC"},
|
||||||
[]byte(`{"a": "b"}`), "bilFGi4ZVZUdG+C6r0NIM9tuRq6PaG33R3eBUVhLwMAErGBaazvXe4Gq2DcJs5q+",
|
[]byte(`{"a": "b"}`), "bilFGi4ZVZUdG+C6r0NIM9tuRq6PaG33R3eBUVhLwMAErGBaazvXe4Gq2DcJs5q+",
|
||||||
"48e395e38ac48988929167df531eb2da00063a7d", false,
|
"48e395e38ac48988929167df531eb2da00063a7d", false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCheckScalrSignature(t *testing.T) {
|
func TestCheckScalrSignature(t *testing.T) {
|
||||||
for _, testCase := range checkScalrSignatureTests {
|
for _, testCase := range checkScalrSignatureTests {
|
||||||
valid, err := CheckScalrSignature(testCase.headers, testCase.payload, testCase.secret, false)
|
valid, err := CheckScalrSignature(testCase.headers, testCase.payload, testCase.secret, false)
|
||||||
if valid != testCase.ok {
|
if valid != testCase.ok {
|
||||||
t.Errorf("failed to check scalr signature fot test case: %s\nexpected ok:%#v, got ok:%#v}",
|
t.Errorf("failed to check scalr signature fot test case: %s\nexpected ok:%#v, got ok:%#v}",
|
||||||
testCase.description, testCase.ok, valid)
|
testCase.description, testCase.ok, valid)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil && strings.Contains(err.Error(), testCase.expectedSignature) {
|
if err != nil && strings.Contains(err.Error(), testCase.expectedSignature) {
|
||||||
t.Errorf("error message should not disclose expected mac: %s on test case %s", err, testCase.description)
|
t.Errorf("error message should not disclose expected mac: %s on test case %s", err, testCase.description)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var extractParameterTests = []struct {
|
var extractParameterTests = []struct {
|
||||||
|
|
|
@ -5,8 +5,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue