@@ -111,19 +111,36 @@ class RPCClient {
111111 throw new Error ( `"config.endpoint" must starts with 'https://' or 'http://'.` ) ;
112112 }
113113 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+
117137
118138 if ( config . endpoint . endsWith ( '/' ) ) {
119139 config . endpoint = config . endpoint . slice ( 0 , - 1 ) ;
120140 }
121141
122142 this . endpoint = config . endpoint ;
123143 this . apiVersion = config . apiVersion ;
124- this . accessKeyId = config . accessKeyId ;
125- this . accessKeySecret = accessKeySecret ;
126- this . securityToken = config . securityToken ;
127144 this . verbose = verbose === true ;
128145 // 非 codes 里的值,将抛出异常
129146 this . codes = new Set ( [ 200 , '200' , 'OK' , 'Success' , 'success' ] ) ;
@@ -145,6 +162,7 @@ class RPCClient {
145162 }
146163
147164 async request ( action , params = { } , opts = { } ) {
165+ const credentials = await this . credentialsProvider . getCredentials ( ) ;
148166 // 1. compose params and opts
149167 opts = Object . assign ( {
150168 headers : {
@@ -164,20 +182,36 @@ class RPCClient {
164182 if ( opts . formatParams !== false ) {
165183 params = formatParams ( params ) ;
166184 }
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 ) ;
169200
170- // 2. caculate signature
171201 const method = ( opts . method || 'GET' ) . toUpperCase ( ) ;
172202 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+
181215 // 3. generate final url
182216 const url = opts . method === 'POST' ? `${ this . endpoint } /` : `${ this . endpoint } /?${ canonicalize ( normalized ) } ` ;
183217 // 4. send request
0 commit comments