forked from apiguy/go-hmacauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmacauth.go
183 lines (154 loc) · 4.05 KB
/
hmacauth.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package hmacauth
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"hash"
"sort"
"strings"
"time"
"github.com/valyala/fasthttp"
)
const (
// common parameters
authorizationHeader = "Authorization"
apiKeyParam = "APIKey"
signatureParam = "Signature"
timestampParam = "Timestamp"
// timestamp validation
maxNegativeTimeOffset time.Duration = -5 * time.Minute
// parsing bits
empty = ""
comma = ","
space = " "
eqSign = "="
newline = "\n"
)
type (
KeyLocator func(string) string
)
type Options struct {
SignedHeaders []string
SecretKey KeyLocator
SignatureExpiresIn time.Duration
HashLib func() hash.Hash
}
type authBits struct {
APIKey string
Signature string
TimestampString string
Timestamp time.Time
}
func (ab *authBits) IsValid() bool {
return ab.APIKey != empty &&
ab.Signature != empty &&
!ab.Timestamp.IsZero()
}
func (ab *authBits) SetTimestamp(isoTime string) (err error) {
ab.Timestamp, err = time.Parse(time.RFC3339, isoTime)
if err == nil {
ab.TimestampString = isoTime
}
return
}
func AuthenticateFHTTP(options *Options, ctx *fasthttp.RequestCtx) error {
var err error
var ab *authBits
if ab, err = parseAuthHeader(string(ctx.Request.Header.Peek(authorizationHeader))); err == nil {
if err = validateTimestamp(ab.Timestamp, options); err == nil {
var sts string
if sts, err = stringToSignFHTTP(ctx, options, ab.TimestampString); err == nil {
if sk := options.SecretKey(ab.APIKey); sk != empty {
if ab.Signature != signString(sts, sk, options) {
err = HMACAuthError{invalidSignature}
}
} else {
err = HMACAuthError{invalidAPIKey}
}
}
}
}
return err
}
func signString(str string, secret string, options *Options) string {
var hashlib hash.Hash
if options.HashLib == nil {
hashlib = hmac.New(sha256.New, []byte(secret))
} else {
hashlib = hmac.New(options.HashLib, []byte(secret))
}
hashlib.Write([]byte(str))
return base64.StdEncoding.EncodeToString(hashlib.Sum(nil))
}
func stringToSignFHTTP(ctx *fasthttp.RequestCtx, options *Options, timestamp string) (string, error) {
var buffer bytes.Buffer
// Standard
buffer.Write(ctx.Request.Header.Method())
buffer.WriteString(newline)
buffer.Write(ctx.Request.Header.Host())
buffer.WriteString(newline)
buffer.Write(ctx.Request.URI().RequestURI())
buffer.WriteString(newline)
buffer.WriteString(timestamp)
buffer.WriteString(newline)
// Headers
sort.Strings(options.SignedHeaders)
for _, header := range options.SignedHeaders {
val := ctx.Request.Header.Peek(header)
if len(val) == 0 {
return empty, HeaderMissingError{header}
}
buffer.Write(val)
buffer.WriteString(newline)
}
return buffer.String(), nil
}
func parseAuthHeader(header string) (*authBits, error) {
if header == empty {
return nil, HeaderMissingError{authorizationHeader}
}
ab := new(authBits)
parts := strings.Split(header, comma)
for _, part := range parts {
kv := strings.SplitN(strings.Trim(part, space), eqSign, 2)
if kv[0] == apiKeyParam {
if ab.APIKey != empty {
return nil, RepeatedParameterError{kv[0]}
}
ab.APIKey = kv[1]
} else if kv[0] == signatureParam {
if ab.Signature != empty {
return nil, RepeatedParameterError{kv[0]}
}
ab.Signature = kv[1]
} else if kv[0] == timestampParam {
if !ab.Timestamp.IsZero() {
return nil, RepeatedParameterError{kv[0]}
}
if ab.SetTimestamp(kv[1]) != nil {
return nil, HMACAuthError{invalidTimestamp}
}
} else {
return nil, HMACAuthError{invalidParameter}
}
}
if !ab.IsValid() {
return nil, HMACAuthError{missingParameter}
}
return ab, nil
}
func validateTimestamp(ts time.Time, options *Options) error {
reqAge := time.Since(ts)
// Allow for about `maxNegativeTimeOffset` of difference, some servers are
// ahead and some are behind
if reqAge < maxNegativeTimeOffset {
return HMACAuthError{tsOutOfRange}
}
if options.SignatureExpiresIn != 0 {
if reqAge > options.SignatureExpiresIn {
return HMACAuthError{signatureExpired}
}
}
return nil
}