@@ -26,6 +26,7 @@ import {
26
26
defaultConstants ,
27
27
EncryptOptions ,
28
28
EnvironmentName ,
29
+ Environments ,
29
30
getAddressP2PKH ,
30
31
getSharedSecret ,
31
32
GetSharingKeyOptions ,
@@ -128,8 +129,10 @@ export class BitGoAPI implements BitGoBase {
128
129
protected readonly _clientId ?: string ;
129
130
protected readonly _clientSecret ?: string ;
130
131
protected _validate : boolean ;
132
+ public readonly cookiesPropagationEnabled : boolean ;
131
133
132
134
constructor ( params : BitGoAPIOptions = { } ) {
135
+ this . cookiesPropagationEnabled = false ;
133
136
if (
134
137
! common . validateParams (
135
138
params ,
@@ -183,10 +186,23 @@ export class BitGoAPI implements BitGoBase {
183
186
if ( params . stellarFederationServerUrl ) {
184
187
common . Environments [ env ] . stellarFederationServerUrl = params . stellarFederationServerUrl ;
185
188
}
189
+ if (
190
+ params . customRootURI &&
191
+ params . customRootURI !== Environments . prod . uri &&
192
+ params . customRootURI !== Environments . test . uri &&
193
+ params . cookiesPropagationEnabled
194
+ ) {
195
+ this . cookiesPropagationEnabled = true ;
196
+ }
186
197
} else {
187
198
env = params . env || ( process . env . BITGO_ENV as EnvironmentName ) ;
188
199
}
189
200
201
+ // if this hasn't been set to true already some conditions are not met
202
+ if ( params . cookiesPropagationEnabled && ! this . cookiesPropagationEnabled ) {
203
+ throw new Error ( 'Cookies are only allowed when custom URIs are in use' ) ;
204
+ }
205
+
190
206
if ( params . authVersion !== undefined ) {
191
207
this . _authVersion = params . authVersion ;
192
208
}
@@ -275,6 +291,18 @@ export class BitGoAPI implements BitGoBase {
275
291
} ) ;
276
292
}
277
293
294
+ /**
295
+ * Get a superagent request for specified http method and URL configured to the SDK configuration
296
+ * @param method - http method for the new request
297
+ * @param url - URL for the new request
298
+ */
299
+ protected getAgentRequest ( method : typeof patchedRequestMethods [ number ] , url : string ) : superagent . SuperAgentRequest {
300
+ let req : superagent . SuperAgentRequest = superagent [ method ] ( url ) ;
301
+ if ( this . cookiesPropagationEnabled ) {
302
+ req = req . withCredentials ( ) ;
303
+ }
304
+ return req ;
305
+ }
278
306
/**
279
307
* Create a basecoin object
280
308
* @param name
@@ -303,7 +331,7 @@ export class BitGoAPI implements BitGoBase {
303
331
* @param method
304
332
*/
305
333
private requestPatch ( method : typeof patchedRequestMethods [ number ] , url : string ) {
306
- let req : superagent . SuperAgentRequest = superagent [ method ] ( url ) ;
334
+ let req = this . getAgentRequest ( method , url ) ;
307
335
if ( this . _proxy ) {
308
336
debug ( 'proxying request through %s' , this . _proxy ) ;
309
337
req = req . proxy ( this . _proxy ) ;
@@ -536,7 +564,7 @@ export class BitGoAPI implements BitGoBase {
536
564
// client constants call cannot be authenticated using the normal HMAC validation
537
565
// scheme, so we need to use a raw superagent instance to do this request.
538
566
// Proxy settings must still be respected however
539
- const resultPromise = superagent . get ( this . url ( '/client/constants' ) ) ;
567
+ const resultPromise = this . getAgentRequest ( 'get' , this . url ( '/client/constants' ) ) ;
540
568
resultPromise . set ( 'BitGo-SDK-Version' , this . _version ) ;
541
569
const result = await ( this . _proxy ? resultPromise . proxy ( this . _proxy ) : resultPromise ) ;
542
570
BitGoAPI . _constants [ env ] = result . body . constants ;
0 commit comments