@@ -111,19 +111,36 @@ class RPCClient {
111
111
throw new Error ( `"config.endpoint" must starts with 'https://' or 'http://'.` ) ;
112
112
}
113
113
assert ( config . apiVersion , 'must pass "config.apiVersion"' ) ;
114
- assert ( config . accessKeyId , 'must pass "config.accessKeyId"' ) ;
115
- var accessKeySecret = config . secretAccessKey || config . accessKeySecret ;
116
- assert ( accessKeySecret , 'must pass "config.accessKeySecret"' ) ;
114
+ if ( config . credentialsProvider ) {
115
+ if ( typeof config . credentialsProvider . getCredentials !== 'function' ) {
116
+ throw new Error ( `must pass "config.credentialsProvider" with function "getCredentials()"` ) ;
117
+ }
118
+ this . credentialsProvider = config . credentialsProvider ;
119
+ } else {
120
+ assert ( config . accessKeyId , 'must pass "config.accessKeyId"' ) ;
121
+ var accessKeySecret = config . secretAccessKey || config . accessKeySecret ;
122
+ assert ( accessKeySecret , 'must pass "config.accessKeySecret"' ) ;
123
+ this . accessKeyId = config . accessKeyId ;
124
+ this . accessKeySecret = accessKeySecret ;
125
+ this . securityToken = config . securityToken ;
126
+ this . credentialsProvider = {
127
+ getCredentials : async ( ) => {
128
+ return {
129
+ accessKeyId : config . accessKeyId ,
130
+ accessKeySecret : accessKeySecret ,
131
+ securityToken : config . securityToken ,
132
+ } ;
133
+ }
134
+ } ;
135
+ }
136
+
117
137
118
138
if ( config . endpoint . endsWith ( '/' ) ) {
119
139
config . endpoint = config . endpoint . slice ( 0 , - 1 ) ;
120
140
}
121
141
122
142
this . endpoint = config . endpoint ;
123
143
this . apiVersion = config . apiVersion ;
124
- this . accessKeyId = config . accessKeyId ;
125
- this . accessKeySecret = accessKeySecret ;
126
- this . securityToken = config . securityToken ;
127
144
this . verbose = verbose === true ;
128
145
// 非 codes 里的值,将抛出异常
129
146
this . codes = new Set ( [ 200 , '200' , 'OK' , 'Success' , 'success' ] ) ;
@@ -145,6 +162,7 @@ class RPCClient {
145
162
}
146
163
147
164
async request ( action , params = { } , opts = { } ) {
165
+ const credentials = await this . credentialsProvider . getCredentials ( ) ;
148
166
// 1. compose params and opts
149
167
opts = Object . assign ( {
150
168
headers : {
@@ -164,20 +182,36 @@ class RPCClient {
164
182
if ( opts . formatParams !== false ) {
165
183
params = formatParams ( params ) ;
166
184
}
167
- const defaults = this . _buildParams ( ) ;
168
- params = Object . assign ( { Action : action } , defaults , params ) ;
185
+ const defaultParams = {
186
+ Format : 'JSON' ,
187
+ Timestamp : timestamp ( ) ,
188
+ Version : this . apiVersion ,
189
+ } ;
190
+ if ( credentials && credentials . accessKeyId && credentials . accessKeySecret ) {
191
+ defaultParams . SignatureMethod = 'HMAC-SHA1' ;
192
+ defaultParams . SignatureVersion = '1.0' ;
193
+ defaultParams . SignatureNonce = kitx . makeNonce ( ) ;
194
+ defaultParams . AccessKeyId = credentials . accessKeyId ;
195
+ if ( credentials . securityToken ) {
196
+ defaultParams . SecurityToken = credentials . securityToken ;
197
+ }
198
+ }
199
+ params = Object . assign ( { Action : action } , defaultParams , params ) ;
169
200
170
- // 2. caculate signature
171
201
const method = ( opts . method || 'GET' ) . toUpperCase ( ) ;
172
202
const normalized = normalize ( params ) ;
173
- const canonicalized = canonicalize ( normalized ) ;
174
- // 2.1 get string to sign
175
- const stringToSign = `${ method } &${ encode ( '/' ) } &${ encode ( canonicalized ) } ` ;
176
- // 2.2 get signature
177
- const key = this . accessKeySecret + '&' ;
178
- const signature = kitx . sha1 ( stringToSign , key , 'base64' ) ;
179
- // add signature
180
- normalized . push ( [ 'Signature' , encode ( signature ) ] ) ;
203
+ // 2. caculate signature
204
+ if ( credentials && credentials . accessKeyId && credentials . accessKeySecret ) {
205
+ const canonicalized = canonicalize ( normalized ) ;
206
+ // 2.1 get string to sign
207
+ const stringToSign = `${ method } &${ encode ( '/' ) } &${ encode ( canonicalized ) } ` ;
208
+ // 2.2 get signature
209
+ const key = credentials . accessKeySecret + '&' ;
210
+ const signature = kitx . sha1 ( stringToSign , key , 'base64' ) ;
211
+ // add signature
212
+ normalized . push ( [ 'Signature' , encode ( signature ) ] ) ;
213
+ }
214
+
181
215
// 3. generate final url
182
216
const url = opts . method === 'POST' ? `${ this . endpoint } /` : `${ this . endpoint } /?${ canonicalize ( normalized ) } ` ;
183
217
// 4. send request
0 commit comments