-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathhmac.go
54 lines (45 loc) · 1.32 KB
/
hmac.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
package hmac
import (
"github.com/Modulr-finance/modulr-hmac/hmac/signature"
"github.com/google/uuid"
"time"
)
const (
AuthorizationHeader = "Authorization"
DateHeader = "Date"
EmptyString = ""
NonceHeader = "x-mod-nonce"
Retry = "x-mod-retry"
RetryTrue = "true"
RetryFalse = "false"
)
var dateNow = time.Now
func GenerateHeaders(apiKey string, apiSecret string, nonce string, hasRetry bool) (map[string]string, *ValidationError) {
validationError := validateInput(apiKey, apiSecret)
if validationError != nil {
return nil, validationError
}
return constructHeadersMap(apiKey, apiSecret, nonce, hasRetry), nil
}
func constructHeadersMap(apiKey string, apiSecret string, nonce string, hasRetry bool) map[string]string {
headers := make(map[string]string)
date := dateNow().UTC().Format(time.RFC1123)
nonce = generateNonceIfEmpty(nonce)
headers[DateHeader] = date
headers[AuthorizationHeader] = signature.Build(apiKey, apiSecret, nonce, date)
headers[NonceHeader] = nonce
headers[Retry] = parseRetryBool(hasRetry)
return headers
}
func generateNonceIfEmpty(nonce string) string {
if nonce == EmptyString {
nonce = uuid.New().String()
}
return nonce
}
func parseRetryBool(hasRetry bool) string {
if hasRetry {
return RetryTrue
}
return RetryFalse
}