-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
276 lines (241 loc) · 6.55 KB
/
helper.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
package myinfoconnectorgolang
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"sort"
"strconv"
"strings"
"time"
"github.com/golang-jwt/jwt"
"golang.org/x/oauth2/jws"
"gopkg.in/square/go-jose.v2"
"software.sslmate.com/src/go-pkcs12"
)
type Params struct {
Name string
Value string
}
type ParamsSort []Params
func (slice ParamsSort) Len() int {
return len(slice)
}
func (slice ParamsSort) Less(i, j int) bool {
return slice[i].Name < slice[j].Name
}
func (slice ParamsSort) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
func spaceFieldsJoin(str string) string {
return strings.Join(strings.Fields(str), "")
}
func GenerateSignature(privateKey *rsa.PrivateKey, baseString string) (string, error) {
h := sha256.New()
h.Write([]byte(baseString))
d := h.Sum(nil)
sigBin, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, d)
if err != nil {
return "", err
}
encodeSignature := base64.StdEncoding.EncodeToString(sigBin)
return encodeSignature, nil
}
func GenerateBaseString(httpMethod string, apiURL string, appId string, params ParamsSort, contentType string, nonceValue string, timestamp string) string {
var defaultAuthHeader = []Params{
{
Name: APP_ID,
Value: appId,
},
{
Name: NONCE,
Value: nonceValue,
},
{
Name: SIGNATURE_METHOD,
Value: RS256,
},
{
Name: TIMESTAMP,
Value: timestamp,
},
}
// Remove params unless Content-Type is "application/x-www-form-urlencoded"
if (httpMethod == HTTP_METHOD_POST) && (contentType != CONTENT_TYPE) {
params = ParamsSort{}
} else {
params = append(params, defaultAuthHeader...)
}
sort.Sort(params)
var strParams string
for _, v := range params {
strParams += fmt.Sprintf("&%s=%s", v.Name, v.Value)
}
// concatenate request elements (HTTP method + url + base string parameters)
baseString := httpMethod + "&" + apiURL + spaceFieldsJoin(strParams)
return baseString
}
/*
This methods will decrypt P12 Certificate and retrieve the Private key with the passphrase.
Returns private key from p12.
*/
func DecryptPrivateKey(secureCertLocation string, passphrase string) (*rsa.PrivateKey, error) {
fileData, err := ioutil.ReadFile(secureCertLocation)
if err != nil {
return nil, err
}
parsedKey, _, _, err := pkcs12.DecodeChain(fileData, passphrase)
if err != nil {
return nil, err
}
privateKey, ok := parsedKey.(*rsa.PrivateKey)
if !ok {
return nil, errors.New(FAILED_TO_PARSE_RSA_PRIVATE_KEY)
}
return privateKey, nil
}
/*
This method helps to generate unique Transaction ID(txnNo).
Returns random hex(txnNo).
*/
func GenerateRandomHex(count int) (string, error) {
bytes := make([]byte, count)
_, err := rand.Read(bytes)
if err != nil {
return "", err
}
randomHex := hex.EncodeToString(bytes)
return randomHex, nil
}
/*
This method helps to generate the authorization header and sign it using the private key.
This is required to be used for both Token and Person API.
Returns Signed Header
*/
func GenerateAuthorizationHeader(apiURL string, params ParamsSort, httpMethod string, contentType string, environment string, appId string, privateKey *rsa.PrivateKey, clientSecret string) (string, error) {
nonceValue, err := GenerateRandomHex(20)
if err != nil {
return "", err
}
timestamp := strconv.Itoa(int(time.Now().UnixMilli()))
if (environment == SINPASS_TEST_ENVIRONMENT) || (environment == SINPASS_PRODUCTION_ENVIRONMENT) {
// Only when environment is TEST or PRODUCTION
baseString := GenerateBaseString(httpMethod, apiURL, appId, params, contentType, nonceValue, timestamp)
signature, err := GenerateSignature(privateKey, baseString)
if err != nil {
return "", err
}
strAuthHeader := "PKI_SIGN timestamp=\"" + timestamp +
"\",nonce=\"" + nonceValue +
"\",app_id=\"" + appId +
"\",signature_method=\"RS256\"" +
",signature=\"" + signature +
"\""
return strAuthHeader, nil
} else {
return "", nil
}
}
/*
This method helps to decode the payload data into normal form.
Returns normalized(decoded) []byte.
*/
func Decode(payload string) ([]byte, error) {
s := strings.Split(payload, ".")
if len(s) < 2 {
return nil, errors.New(INVALID_TOKEN)
}
decodedData, err := base64.RawStdEncoding.DecodeString(s[1])
if err != nil {
return nil, err
}
return decodedData, err
}
/*
This method takes in a JSON Web Signature and will check against
the public key for its validity and to retrieve the decoded data.
This verification is required for the decoding of the access token and
response from Person API.
Returns decoded data.
*/
func VerifyJWS(publicCert string, accessToken string) ([]byte, error) {
keyData, err := ioutil.ReadFile(publicCert)
if err != nil {
return nil, err
}
key, err := jwt.ParseRSAPublicKeyFromPEM(keyData)
if err != nil {
return nil, err
}
err = jws.Verify(accessToken, key)
if err != nil {
return nil, err
}
parts := strings.Split(accessToken, ".")
err = jwt.SigningMethodRS256.Verify(strings.Join(parts[0:2], "."), parts[2], key)
if err != nil {
return nil, err
}
claimSet, err := Decode(accessToken)
if err != nil {
return nil, err
}
return claimSet, nil
}
/*
This method takes in a JSON Web Encrypted string and will decrypt it using the private key.
This is required to decrypt the data from Person API.
Returns decrypted data.
*/
func DecryptJWE(pemPrivaKey *rsa.PrivateKey, compactJWE string) (string, error) {
payload, err := jose.ParseEncrypted(compactJWE)
if err != nil {
return "", err
}
decrypted, err := payload.Decrypt(pemPrivaKey)
if err != nil {
return "", err
}
return string(decrypted), nil
}
func Unmarshal(data []byte, v interface{}) error {
err := json.Unmarshal(data, v)
if err != nil {
return err
}
return nil
}
/*
This method removes the duplication use of environment based condition for generating auth header.
Returns fully generated auth Header as string.
*/
func AuthHeader(apiURL string, params ParamsSort, httpMethod string, contentType string, environment string, appId string, privateKey *rsa.PrivateKey, clientSecret string) (string, error) {
var authHeader string
var err error
if environment == SINPASS_SANDBOX_ENVIRONMENT {
// No Headers
} else if (environment == SINPASS_TEST_ENVIRONMENT) || (environment == SINPASS_PRODUCTION_ENVIRONMENT) {
authHeader, err = GenerateAuthorizationHeader(
apiURL,
params,
httpMethod,
contentType,
environment,
appId,
privateKey,
clientSecret,
)
if err != nil {
return authHeader, err
}
} else {
return authHeader, errors.New(ERROR_UNKNOWN_AUTH_LEVEL)
}
return authHeader, nil
}