-
Notifications
You must be signed in to change notification settings - Fork 0
/
gin_pow.go
380 lines (316 loc) · 9.87 KB
/
gin_pow.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package ginpow
import (
"encoding/hex"
"errors"
"strconv"
"github.com/gin-gonic/gin"
gopow "github.com/jeongy-cho/go-pow/v2"
gonanoid "github.com/matoous/go-nanoid"
)
// Middleware provides a proof of work implementation. On failure, an error 428 response is returned.
// On success, the middleware passes to the next handler. Clients can request a nonce by posting a json
// or xml request to NonceHandler, or by reading the set headers on an endpoint that uses the NonceHeaderMiddleware
type Middleware struct {
// NonceHeader is the name of the header on which to set a generated nonce.
// Defaults to `X-Nonce`
NonceHeader string
// NonceChecksumHeader is the name of the header on which to set a generated nonce.
// Defaults to `X-Nonce-Checksum`
NonceChecksumHeader string
// HashDifficultyHeader is the name of the header on which to set the difficulty.
// Defaults to `X-Hash-Difficulty`
HashDifficultyHeader string
// Pow is a gopow.Pow instance to handle proof of work implementation
Pow *gopow.Pow
// ExtractAll extracts all necessary data at once.
// Optional. If not set then uses the default methods defined in `ExtractData`,
// `ExtractNonce`, and `ExtractHash`. When ExtractAll is set, then `ExtractData`,
// `ExctractNonce`, `ExtractHash` is ignored.
ExtractAll func(c *gin.Context) (nonce string, nonceChecksum string, data string, hash string, err error)
// ExtractData extracts the data that the hash was generated against and
// passes it to do proof of work calculation.
// Required when `ExtractAll` isn't defined.
ExtractData func(c *gin.Context) (string, error)
// ExtractNonce extracts the nonce that is in the request.
// Defaults getting from header X-Nonce, X-Nonce-Checksum
ExtractNonce func(c *gin.Context) (nonce string, nonceChecksum string, error error)
// ExtractHash extracts the calculated hash calculated by the client.
// Defaults to getting from header X-Hash.
ExtractHash func(c *gin.Context) (hash string, error error)
// Difficulty sets the number of leading zeros required for a valid hash.
// Defaults to 0.
Difficulty int
// NonceLength sets the length of the nonce to be generated
// Defaults to 10.
NonceLength int
// Check is the flag to enable nonce checking
// Defaults to false.
Check bool
// Secret is a cryptographically secure random string to generate nonce checksums.
// only used when `Check` flag is true. Defaults to 256 bit cryptographically secure string.
Secret string
// the following is the keys in which to set nonces in gin.Context.
// Defaults:
// NonceContextKey: "nonce"
// NonceChecksumContextKey: "nonceChecksum"
// HashDifficultyContextKey: "hashDifficulty"
NonceContextKey string
NonceChecksumContextKey string
HashDifficultyContextKey string
// the following is the keys in which to set nonces in data of Middleware.NonceHandler.
// Defaults:
// NonceDataKey: "nonce"
// NonceChecksumDataKey: "nonce_checksum"
// HashDifficultyDataKey: "difficulty"
NonceDataKey string
NonceChecksumDataKey string
HashDifficultyDataKey string
// FailureStatusCode is the status code to send back to client
// when using default OnFailedVerification. defaults to 428.
FailureStatusCode int
// OnFailedVerification is called when a hash validation fails.
// By default request Aborts and returns with `Middleware.FailureStatusCode`.
OnFailedVerification func(c *gin.Context, err *VerificationError)
// Hash function for proof of work.
// Defaults to sha256
Hash gopow.HashFunction
// NonceGenerator returns a nonce.
NonceGenerator gopow.NonceGenerator
}
// New sets the config of a middleware. ExtractData definition is required.
func New(m *Middleware) (*Middleware, error) {
if err := m.middleWareInit(); err != nil {
return nil, err
}
return m, nil
}
func (pow *Middleware) middleWareInit() error {
if pow.ExtractData == nil && pow.ExtractAll == nil {
return errors.New("pow.ExtractData func not declared")
}
if pow.NonceHeader == "" {
pow.NonceHeader = "X-Nonce"
}
if pow.NonceChecksumHeader == "" {
pow.NonceChecksumHeader = "X-Nonce-Checksum"
}
if pow.HashDifficultyHeader == "" {
pow.HashDifficultyHeader = "X-Hash-Difficulty"
}
if pow.ExtractNonce == nil {
pow.ExtractNonce = func(c *gin.Context) (nonce string, nonceChecksum string, err error) {
return c.GetHeader("X-Nonce"), c.GetHeader("X-Nonce-Checksum"), nil
}
}
if pow.ExtractHash == nil {
pow.ExtractHash = func(c *gin.Context) (hash string, error error) {
return c.GetHeader("X-Hash"), nil
}
}
if pow.Check {
if pow.Secret == "" {
var err error
pow.Secret, err = gonanoid.ID(32)
if err != nil {
return err
}
}
}
if pow.NonceLength == 0 {
pow.NonceLength = 10
}
pow.Pow = gopow.New(&gopow.Pow{
Secret: []byte(pow.Secret),
Check: pow.Check,
Difficulty: pow.Difficulty,
NonceLength: pow.NonceLength,
Hash: pow.Hash,
NonceGenerator: pow.NonceGenerator,
})
if pow.NonceContextKey == "" {
pow.NonceContextKey = "nonce"
}
if pow.NonceChecksumContextKey == "" {
pow.NonceChecksumContextKey = "nonceChecksum"
}
if pow.HashDifficultyContextKey == "" {
pow.HashDifficultyContextKey = "hashDifficulty"
}
if pow.NonceDataKey == "" {
pow.NonceDataKey = "nonce"
}
if pow.NonceChecksumDataKey == "" {
pow.NonceChecksumDataKey = "nonce_checksum"
}
if pow.HashDifficultyDataKey == "" {
pow.HashDifficultyDataKey = "difficulty"
}
if pow.FailureStatusCode == 0 {
pow.FailureStatusCode = 428
}
if pow.OnFailedVerification == nil {
pow.OnFailedVerification = func(c *gin.Context, err *VerificationError) {
c.Abort()
c.String(pow.FailureStatusCode, err.Error())
}
}
return nil
}
// NonceHandler is the used by a client to get a nonce in JSON or XML depending on accept header
func (pow *Middleware) NonceHandler(c *gin.Context) {
nonce, nonceChecksum, err := pow.getNonce(c)
if err != nil {
c.Error(err)
return
}
var h gin.H
h = gin.H{
pow.NonceDataKey: nonce,
pow.HashDifficultyDataKey: pow.Difficulty,
}
if pow.Check {
h[pow.NonceChecksumDataKey] = nonceChecksum
}
c.Negotiate(200, gin.Negotiate{
Offered: []string{gin.MIMEJSON, gin.MIMEXML},
Data: h,
})
}
// NonceHeaderMiddleware is used by a client to get a nonce embedded in the header of a request
func (pow *Middleware) NonceHeaderMiddleware(c *gin.Context) {
nonce, nonceChecksum, err := pow.getNonce(c)
if err != nil {
c.Error(err)
return
}
c.Header(pow.NonceHeader, nonce)
c.Header(pow.HashDifficultyHeader, strconv.Itoa(pow.Difficulty))
if pow.Check {
c.Header(pow.NonceChecksumHeader, nonceChecksum)
}
}
// GenerateNonceMiddleware generates a nonce and sets it in the context.
// if other ginpow middleware is used after this middleware then it will
// use the nonce generated here.
func (pow *Middleware) GenerateNonceMiddleware(c *gin.Context) {
nonce, nonceChecksum, err := pow.Pow.GenerateNonce()
if err != nil {
c.Error(err)
return
}
c.Set(pow.NonceContextKey, string(nonce))
c.Set(pow.HashDifficultyContextKey, pow.Difficulty)
if pow.Check {
c.Set(pow.NonceChecksumContextKey, hex.EncodeToString(nonceChecksum))
}
}
// gets a nonce in context or generates one
func (pow *Middleware) getNonce(c *gin.Context) (string, string, error) {
n, nExists := c.Get(pow.NonceContextKey)
nc, _ := c.Get(pow.NonceChecksumContextKey)
if nExists {
if pow.Check {
return n.(string), nc.(string), nil
}
return n.(string), "", nil
}
nonce, nonceChecksum, err := pow.Pow.GenerateNonce()
return string(nonce), hex.EncodeToString(nonceChecksum), err
}
// VerifyNonceMiddleware validates a hash given a nonce, data string, difficulty,
// and, if `Middleware.Check == true`, nonce checksum. On failure, it will call
// OnVerifiedFailed method. By default will Abort response with status code 428
func (pow *Middleware) VerifyNonceMiddleware(c *gin.Context) {
var (
nonce string
nonceChecksum string
data string
hash string
err error
)
if pow.ExtractAll != nil {
nonce, nonceChecksum, data, hash, err = pow.ExtractAll(c)
if err != nil {
if !c.IsAborted() {
c.AbortWithError(500, err)
}
return
}
} else {
nonce, nonceChecksum, err = pow.ExtractNonce(c)
if err != nil {
if !c.IsAborted() {
c.AbortWithError(500, err)
}
return
}
if nonce == "" {
c.String(400, "no nonce in request")
c.Abort()
return
}
if pow.Check && nonceChecksum == "" {
c.String(400, "no nonce checksum in request")
c.Abort()
return
}
data, err = pow.ExtractData(c)
if err != nil {
if !c.IsAborted() {
c.AbortWithError(500, err)
}
return
}
hash, err = pow.ExtractHash(c)
if err != nil {
if !c.IsAborted() {
c.AbortWithError(500, err)
}
return
}
if hash == "" {
c.String(400, "no hash in request")
c.Abort()
return
}
}
hashBytes, err := hex.DecodeString(hash)
if err != nil {
c.String(400, "received hash is not a valid hex string")
c.Abort()
return
}
nonceChecksumBytes, err := hex.DecodeString(nonceChecksum)
if err != nil {
c.String(400, "received checksum is not a valid hex string")
c.Abort()
return
}
ok, verificationErr := pow.Pow.VerifyHashAtDifficulty([]byte(nonce), []byte(data), hashBytes, nonceChecksumBytes)
if !ok {
err := &VerificationError{
Hash: hash,
Nonce: nonce,
NonceChecksum: nonceChecksum,
Difficulty: pow.Difficulty,
Reason: verificationErr.Error(),
}
c.Error(err)
pow.OnFailedVerification(c, err)
}
}
// VerificationError reports the parameters that caused a verification to fail.
// Does _not_ include data parameter.
type VerificationError struct {
Hash string
Nonce string
NonceChecksum string
Difficulty int
Reason string
}
func (v *VerificationError) Error() string {
return v.Reason
}
type headerGetter interface {
GetHeader(key string) string
}