forked from GBSEcom/NPM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcredentials.go
More file actions
79 lines (64 loc) · 2 KB
/
credentials.go
File metadata and controls
79 lines (64 loc) · 2 KB
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
package fiserv
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"time"
"github.com/gofrs/uuid"
)
type Credentials struct {
ApiKey string
ApiSecret string
}
func (crd Credentials) GenRequestHeaders(payload []byte) (*HeaderData, error) {
clientRequestId, err := uuid.NewV4()
if err != nil {
return nil, err
}
return crd.genHeaders(payload, clientRequestId.String(), time.Now())
}
func (crd Credentials) genHeaders(payload []byte, clientRequestId string, now time.Time) (*HeaderData, error) {
// equivalent python code:
//
// self.api_key = params['api_key']
// self.api_secret = params['api_secret']
// self.client_request_id = str(uuid.uuid4())
// self.timestamp = str(int(round(time.time()*1000)))
//
// data = self.api_key + self.client_request_id + self.timestamp
// if payload:
// data += json.dumps(self.sanitize_for_serialization(payload))
msSinceEpoch := now.Unix() * 1000
toSign := []byte(crd.ApiKey + clientRequestId + fmt.Sprintf("%d", msSinceEpoch))
if len(payload) > 0 {
toSign = append(toSign, payload...)
}
sig, err := genMsgSignature(crd.ApiSecret, toSign)
if err != nil {
return nil, err
}
return &HeaderData{
ApiKey: ApiKeyParam(crd.ApiKey),
ContentType: ContentTypeParam_application_json,
ClientRequestId: ClientRequestIdParam(clientRequestId),
Timestamp: TimestampParam(msSinceEpoch),
MessageSignature: MessageSignatureParam(sig),
}, nil
}
func genMsgSignature(apiSecret string, data []byte) (string, error) {
// equivalent python code:
//
// h = hmac.new(self.api_secret, data, hashlib.sha256).hexdigest()
// return base64.b64encode(h)
// equivalent JS code:
// var genMsgSignature = function (secret, data) {
// return Base64.stringify(crypto_js_1.HmacSHA256(data, secret));
// };
hasher := hmac.New(sha256.New, []byte(apiSecret))
if _, err := hasher.Write(data); err != nil {
return "", fmt.Errorf("unable to hash data: %v", err)
}
b64 := base64.StdEncoding.EncodeToString(hasher.Sum(nil))
return b64, nil
}