-
Notifications
You must be signed in to change notification settings - Fork 1
/
easycodefgo.go
269 lines (233 loc) · 7.56 KB
/
easycodefgo.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
package easycodefgo
import (
"errors"
"reflect"
)
// 액세스 토큰 관리 구조체
type accessToken struct {
product string
demo string
sandbox string
}
// CODEF API
type Codef struct {
accessToken accessToken // OAUTH2.0 토큰
demoClientID string // 데모 엑세스 토큰 밝브을 위한 클라이언트 아이디
demoClientSecret string // 데모 엑세스 토큰 밝브을 위한 클라이언트 시크릿
clientID string // 정식 엑세스 토큰 발급을 위한 클라이언트 아이디
clientSecret string // 정식 엑세스 토큰 발급을 위한 클라이언트 시크릿
PublicKey string // 유저 퍼블릭키
}
// 요청 정보
type requestInfo struct {
domain string
clientID string
clientSecret string
}
// 상품 요청
func (self *Codef) RequestProduct(
productPath string,
serviceType ServiceType,
param map[string]interface{},
) (string, error) {
// 클라이언트 정보 체크
if !self.checkClientInfo(serviceType) {
res := newResponseByMessage(messageEmptyClientInfo)
return res.WriteValueAsString(), nil
}
// 퍼블릭키 정보 체크
if TrimAll(self.PublicKey) == "" {
res := newResponseByMessage(messageEmptyPublicKey)
return res.WriteValueAsString(), nil
}
// 추가인증 키워드 체크
if !isEmptyTwoWayKeyword(param) {
res := newResponseByMessage(messageInvalid2WayKeyword)
return res.WriteValueAsString(), nil
}
reqInfo := self.getReqInfoByServiceType(serviceType)
res, err := execute(productPath, param, self.getAccessToken(serviceType), reqInfo)
if err != nil {
return "", err
}
return res.WriteValueAsString(), nil
}
// 상품 추가인증 요청
func (self *Codef) RequestCertification(
productPath string,
serviceType ServiceType,
param map[string]interface{},
) (string, error) {
// 클라이언트 정보 체크
if !self.checkClientInfo(serviceType) {
res := newResponseByMessage(messageEmptyClientInfo)
return res.WriteValueAsString(), nil
}
// 퍼블릭키 정보 체크
if TrimAll(self.PublicKey) == "" {
res := newResponseByMessage(messageEmptyPublicKey)
return res.WriteValueAsString(), nil
}
// 추가인증 파라미터 필수 입력 체크
if !hasTwoWayInfo(param) {
res := newResponseByMessage(messageInvalid2WayInfo)
return res.WriteValueAsString(), nil
}
reqInfo := self.getReqInfoByServiceType(serviceType)
// 상품 조회 요청
res, err := execute(productPath, param, self.getAccessToken(serviceType), reqInfo)
if err != nil {
return "", err
}
return res.WriteValueAsString(), nil
}
// 클라이언트 정보 검사
func (self *Codef) checkClientInfo(serviceType ServiceType) bool {
switch serviceType {
case TypeProduct:
if TrimAll(self.clientID) == "" || TrimAll(self.clientSecret) == "" {
return false
}
case TypeDemo:
if TrimAll(self.demoClientID) == "" || TrimAll(self.demoClientSecret) == "" {
return false
}
default:
if TrimAll(SandboxClientID) == "" || TrimAll(SandboxClientSecret) == "" {
return false
}
}
return true
}
// connectedID 발급을 위한 계정 등록
func (self *Codef) CreateAccount(serviceType ServiceType, param map[string]interface{}) (string, error) {
return self.RequestProduct(PathCreateAccount, serviceType, param)
}
// 게정 정보 추가
func (self *Codef) AddAccount(serviceType ServiceType, param map[string]interface{}) (string, error) {
return self.RequestProduct(PathAddAccount, serviceType, param)
}
// 계정 정보 수정
func (self *Codef) UpdateAccount(serviceType ServiceType, param map[string]interface{}) (string, error) {
return self.RequestProduct(PathUpdateAccount, serviceType, param)
}
// 계정 정보 삭제
func (self *Codef) DeleteAccount(serviceType ServiceType, param map[string]interface{}) (string, error) {
return self.RequestProduct(PathDeleteAccount, serviceType, param)
}
// connectedID로 등록된 계정 목록 조회
func (self *Codef) GetAccountList(serviceType ServiceType, param map[string]interface{}) (string, error) {
return self.RequestProduct(PathGetAccountList, serviceType, param)
}
// 클라이언트 정보로 등록된 모든 connectedID 목록 조회
func (self *Codef) GetConnectedIDList(serviceType ServiceType, param map[string]interface{}) (string, error) {
return self.RequestProduct(PathGetCIDList, serviceType, param)
}
// 토큰 발급
func (self *Codef) RequestToken(serviceType ServiceType) (map[string]interface{}, error) {
existClientInfo := self.checkClientInfo(serviceType)
if !existClientInfo {
return nil, errors.New("The ClientID and ClientSecret values are empty. Please set the value according to the service type.")
}
switch serviceType {
case TypeProduct:
return requestToken(self.clientID, self.clientSecret)
case TypeDemo:
return requestToken(self.demoClientID, self.demoClientSecret)
default:
return requestToken(SandboxClientID, SandboxClientSecret)
}
}
// 클라이언트 시크릿 반환
func (self *Codef) getClientSecret(serviceType ServiceType) string {
switch serviceType {
case TypeProduct:
return self.clientSecret
case TypeDemo:
return self.demoClientSecret
default:
return SandboxClientSecret
}
}
// 정식서버 사용을 위한 클라이언트 정보 설정
func (self *Codef) SetClientInfo(clientID, clientSecret string) {
self.clientID = clientID
self.clientSecret = clientSecret
}
// 데모 서버 사용을 위한 클라이언트 정보 설정
func (self *Codef) SetClientInfoForDemo(clientId, clientSecret string) {
self.demoClientID = clientId
self.demoClientSecret = clientSecret
}
// 액세스 토큰 정보 셋팅
func (self *Codef) SetAccessToken(accessToken string, serviceType ServiceType) {
switch serviceType {
case TypeProduct:
self.accessToken.product = accessToken
case TypeDemo:
self.accessToken.demo = accessToken
default:
self.accessToken.sandbox = accessToken
}
}
// 액세스 토큰 정보 셋팅
func (self *Codef) getAccessToken(serviceType ServiceType) *string {
switch serviceType {
case TypeProduct:
return &self.accessToken.product
case TypeDemo:
return &self.accessToken.demo
default:
return &self.accessToken.sandbox
}
}
// 서비스 상태에 해당하는 요청 정보를 가져온다
// return (domain, clientID, clientSecret)
func (self *Codef) getReqInfoByServiceType(serviceType ServiceType) *requestInfo {
switch serviceType {
case TypeProduct:
return &requestInfo{APIDomain, self.clientID, self.clientSecret}
case TypeDemo:
return &requestInfo{DemoDomain, self.demoClientID, self.demoClientSecret}
default:
return &requestInfo{SandboxDomain, SandboxClientID, SandboxClientSecret}
}
}
// 2Way 키워드가 없는지 확인
func isEmptyTwoWayKeyword(param map[string]interface{}) bool {
if _, ok := param["is2Way"]; ok {
return false
}
if _, ok := param["twoWayInfo"]; ok {
return false
}
return true
}
// 2way 상품 요청 시 필수 데이터 존재하는지 확인
func hasTwoWayInfo(param map[string]interface{}) bool {
is2Way, ok := param["is2Way"]
if !ok || reflect.TypeOf(is2Way).Kind() != reflect.Bool || !is2Way.(bool) {
return false
}
twoWayInfo, ok := param["twoWayInfo"].(map[string]interface{})
if !ok || twoWayInfo == nil {
return false
}
return checkNeedValueInTwoWayInfo(twoWayInfo)
}
// twoWayInfo 정보 내부에 필요한 데이터가 존재하는지 체크
func checkNeedValueInTwoWayInfo(twoWayInfo map[string]interface{}) bool {
if _, ok := twoWayInfo["jobIndex"]; !ok {
return false
}
if _, ok := twoWayInfo["threadIndex"]; !ok {
return false
}
if _, ok := twoWayInfo["jti"]; !ok {
return false
}
if _, ok := twoWayInfo["twoWayTimestamp"]; !ok {
return false
}
return true
}