-
Notifications
You must be signed in to change notification settings - Fork 0
/
person.go
160 lines (133 loc) · 3.77 KB
/
person.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
package myinfoconnectorgolang
import (
"bytes"
"crypto/rsa"
"encoding/json"
"errors"
"net/http"
)
/*
This method calls the Person API and returns a JSON response with the personal data that was requested.
Your application needs to provide a valid "access token" in exchange for the JSON data.
Once your application receives this JSON data, you can use this data.
Returns the Person Data (Payload decrypted + Signature validated).
*/
func (appConfig AppConfig) GetPersonData(accessToken, txnNo string) ([]byte, error) {
if !isInitialized {
return nil, errors.New(ERROR_UNKNOWN_NOT_INIT)
}
privateKey, err := DecryptPrivateKey(appConfig.CLIENT_SECURE_CERT, appConfig.CLIENT_SECURE_CERT_PASSPHRASE)
if err != nil {
return nil, err
}
personData, err := appConfig.GetPersonDataWithKey(accessToken, txnNo, privateKey)
if err != nil {
return nil, err
}
return personData, nil
}
/*
This method will take in the accessToken from Token API and decode it to get the sub(eg either uinfin or uuid).
It will call the Person API using the token and sub.
It will verify the Person API data's signature and decrypt the result.
Returns decrypted result from calling Person API.
*/
func (appConfig AppConfig) GetPersonDataWithKey(accessToken, txnNo string, privateKey *rsa.PrivateKey) ([]byte, error) {
if !isInitialized {
return nil, errors.New(ERROR_UNKNOWN_NOT_INIT)
}
var resp []byte
tokenBytes, err := VerifyJWS(appConfig.MYINFO_SIGNATURE_CERT_PUBIC_CERT, accessToken)
if err != nil {
return resp, err
}
var data map[string]interface{}
err = json.NewDecoder(bytes.NewBuffer(tokenBytes)).Decode(&data)
if err != nil {
return resp, err
}
if data["sub"].(string) != "" {
personBytes, err := appConfig.CallPersonAPI(data["sub"].(string), accessToken, txnNo, privateKey)
if err != nil {
return resp, err
}
if appConfig.ENVIRONMENT == SINPASS_SANDBOX_ENVIRONMENT {
err := Unmarshal(personBytes, &resp)
if err != nil {
return resp, err
}
}
decryptedRes, err := DecryptJWE(privateKey, string(personBytes))
if err != nil {
return resp, err
}
decodedData, err := Decode(decryptedRes)
if err != nil {
return resp, err
}
return decodedData, nil
}
return resp, errors.New(UINFIN_NOT_FOUND)
}
/*
This method will generate the Authorization Header and call the Person API to get the encrypted Person Data.
Returns result from calling Person API.
*/
func (appConfig AppConfig) CallPersonAPI(sub, accessToken, txnNo string, privateKey *rsa.PrivateKey) ([]byte, error) {
if !isInitialized {
return nil, errors.New(ERROR_UNKNOWN_NOT_INIT)
}
var response []byte
var err error
callPersonURL := appConfig.PERSON_URL + "/" + sub + "/"
params := ParamsSort{
{
Name: PARAM_CLIENT_ID,
Value: appConfig.CLIENT_ID,
},
{
Name: PARAM_ATTRIBUTES,
Value: appConfig.ATTRIBUTES,
},
}
var txnExists bool
if txnNo != "" {
params = append(params, Params{
Name: PARAM_TXNNO,
Value: txnNo,
})
txnExists = true
}
authHeader, err := AuthHeader(
callPersonURL,
params,
HTTP_METHOD_GET,
"",
appConfig.ENVIRONMENT,
appConfig.CLIENT_ID,
privateKey,
appConfig.CLIENT_SECRET,
)
if err != nil {
return response, err
}
callPersonURL += "?" + PARAM_CLIENT_ID + "=" + appConfig.CLIENT_ID + "&" + PARAM_ATTRIBUTES + "=" + appConfig.ATTRIBUTES
if txnExists {
callPersonURL += "&" + PARAM_TXNNO + "=" + txnNo
}
request, err := http.NewRequest(HTTP_METHOD_GET, callPersonURL, nil)
if err != nil {
return response, err
}
request.Header.Set(CACHE_CONTROL, NO_CACHE)
if authHeader != "" {
request.Header.Set(AUTHORIZATION, authHeader+","+BEARER+" "+accessToken)
} else {
request.Header.Set(AUTHORIZATION, BEARER+" "+accessToken)
}
response, err = SendRequest(request)
if err != nil {
return response, err
}
return response, nil
}