-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.go
86 lines (65 loc) · 1.98 KB
/
hook.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
82
83
84
85
86
package hook
import (
"crypto/sha256"
"encoding/hex"
"errors"
"strconv"
"time"
log "github.com/Sirupsen/logrus"
"github.com/TykTechnologies/tyk-protobuf/bindings/go"
)
type Sha256 struct {
sharedSecret string
allowedClockSkew int64
headerAuthKey string
headerSignatureKey string
}
func (s *Sha256) Init(sharedSecret string, allowedClockSkew int64, headerAuthKey string, headerSignatureKey string) {
s.sharedSecret = sharedSecret
s.allowedClockSkew = allowedClockSkew
s.headerAuthKey = headerAuthKey
s.headerSignatureKey = headerSignatureKey
}
func (s Sha256) ValidateSignature(obj *coprocess.Object) (*coprocess.Object, error) {
//log.Info("ValidateSignature called")
authHeader, ok := obj.Request.Headers[s.headerAuthKey]
if !ok {
log.Error("authorization header not present")
return obj, errors.New("auth header not present")
}
xSignature, ok := obj.Request.Headers[s.headerSignatureKey]
if !ok {
log.Error("authorization signature not present")
return obj, errors.New("authorization signature not present")
}
if err := s.validate(authHeader, xSignature); err != nil {
// signature is not valid
return obj, errors.New("signature is not valid")
}
return obj, nil
}
func (s Sha256) validate(tokenAttempt string, signatureAttempt string) error {
now := time.Now().Unix()
attempts := 0
for i := int64(0); i <= s.allowedClockSkew; i++ {
attempts++
if hex.EncodeToString(s.Sha256Sum(tokenAttempt, now+i)) == signatureAttempt {
//log.Info("attempts: ", attempts)
return nil
}
if i == int64(0) {
continue
}
attempts++
if hex.EncodeToString(s.Sha256Sum(tokenAttempt, now-i)) == signatureAttempt {
//log.Info("attempts: ", attempts)
return nil
}
}
//log.Info("attempts: ", attempts)
return errors.New("invalid signature" + signatureAttempt)
}
func (s Sha256) Sha256Sum(token string, timeStamp int64) []byte {
signature := sha256.Sum256([]byte(token + s.sharedSecret + strconv.FormatInt(timeStamp, 10)))
return signature[:]
}