This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
81 lines (69 loc) · 1.94 KB
/
auth.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package MandrillWebhookAuth
import (
"net/http"
"sort"
"errors"
"strings"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
)
var (
// ErrBadSig is returned when verification with an incorrect
// is used.
ErrBadSig = errors.New("Bad Signature")
)
// VerifyRequest verifies a mandrill request authentication
// signature.
func VerifyRequest(req *http.Request, authKey string) error {
signature := req.Header.Get("X-Mandrill-Signature")
url := req.URL.String()
params := postParams(req)
signedData := sign(authKey, url, params)
if hmac.Equal([]byte(signedData), []byte(signature)) {
return nil
} else {
return ErrBadSig
}
}
// SignRequest returns a signature string given a HTTP request
// and a key.
func SignRequest(authKey string, req *http.Request) string {
url := req.URL.String()
params := postParams(req)
return sign(authKey, url, params)
}
func sign(authKey, url string, params []KeyValue) string {
signedData := url
for _, kv := range params {
signedData += kv.Key + kv.Val
}
mac := hmac.New(sha1.New, []byte(authKey))
mac.Write([]byte(signedData))
hashedData := mac.Sum(nil)
sig := base64.StdEncoding.EncodeToString(hashedData)
return sig
}
func postParams(req *http.Request) []KeyValue {
req.ParseForm()
arr := make([]KeyValue, 0, len(req.PostForm))
for k, v := range req.PostForm {
arr = append(arr, KeyValue{Key: k, Val: v[0]})
}
sort.Sort(ByKey(arr))
return arr
}
// KeyValue holds a key and corresponding value, to be used for sorting
// collections of key-value pairs.
type KeyValue struct {
Key string
Val string
}
// ByKey implements the sortable interface for a KeyValue slice
// such that it is sorted by key, in non-decreasing order.
type ByKey []KeyValue
func (a ByKey) Len() int { return len(a) }
func (a ByKey) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByKey) Less(i, j int) bool {
return strings.Compare(a[i].Key, a[j].Key) == -1
}