forked from blindside-io/octohooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret.go
66 lines (52 loc) · 1.55 KB
/
secret.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
package octohooks
import (
"crypto/hmac"
"crypto/sha1"
"crypto/subtle"
"encoding/hex"
"net/http"
"github.com/pkg/errors"
)
// SecretResolver defines a simple interface for returning a Secret based
// on a request that has come in from Github
type SecretResolver interface {
Resolve(req *http.Request) (Secret, error)
}
// StaticResolver implements SecretResolver but only validates all requests
// against a static secret you initialize it with
type StaticResolver struct {
Secret string
}
// force a compilation error if static resolver doesnt implement the correct
// interface
var _ SecretResolver = &StaticResolver{}
// Resolve returns a Secret from the defined variable
func (sr *StaticResolver) Resolve(req *http.Request) (Secret, error) {
return Secret(sr.Secret), nil
}
type signatureInvalid string
func (s signatureInvalid) Error() string {
return string(s)
}
// Secret contains behavior for verifying github webhooks against the secret
type Secret string
// Validate validates a signature and body against the secret
func (s Secret) Validate(sig string, body []byte) error {
if len(s) == 0 {
return nil
}
if len(sig) == 0 {
return signatureInvalid("signature not found")
}
mac := hmac.New(sha1.New, []byte(s))
_, err := mac.Write(body)
if err != nil {
return errors.Wrap(err, "could not generate hmac signature")
}
expectedMAC := mac.Sum(nil)
expectedSig := []byte("sha1=" + hex.EncodeToString(expectedMAC))
if subtle.ConstantTimeCompare(expectedSig, []byte(sig)) != 1 {
return signatureInvalid("signature invalid")
}
return nil
}