@@ -26,6 +26,7 @@ import {
2626 defaultConstants ,
2727 EncryptOptions ,
2828 EnvironmentName ,
29+ Environments ,
2930 getAddressP2PKH ,
3031 getSharedSecret ,
3132 GetSharingKeyOptions ,
@@ -128,8 +129,10 @@ export class BitGoAPI implements BitGoBase {
128129 protected readonly _clientId ?: string ;
129130 protected readonly _clientSecret ?: string ;
130131 protected _validate : boolean ;
132+ public readonly cookiesPropagationEnabled : boolean ;
131133
132134 constructor ( params : BitGoAPIOptions = { } ) {
135+ this . cookiesPropagationEnabled = false ;
133136 if (
134137 ! common . validateParams (
135138 params ,
@@ -183,10 +186,23 @@ export class BitGoAPI implements BitGoBase {
183186 if ( params . stellarFederationServerUrl ) {
184187 common . Environments [ env ] . stellarFederationServerUrl = params . stellarFederationServerUrl ;
185188 }
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+ }
186197 } else {
187198 env = params . env || ( process . env . BITGO_ENV as EnvironmentName ) ;
188199 }
189200
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+
190206 if ( params . authVersion !== undefined ) {
191207 this . _authVersion = params . authVersion ;
192208 }
@@ -275,6 +291,18 @@ export class BitGoAPI implements BitGoBase {
275291 } ) ;
276292 }
277293
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+ }
278306 /**
279307 * Create a basecoin object
280308 * @param name
@@ -303,7 +331,7 @@ export class BitGoAPI implements BitGoBase {
303331 * @param method
304332 */
305333 private requestPatch ( method : typeof patchedRequestMethods [ number ] , url : string ) {
306- let req : superagent . SuperAgentRequest = superagent [ method ] ( url ) ;
334+ let req = this . getAgentRequest ( method , url ) ;
307335 if ( this . _proxy ) {
308336 debug ( 'proxying request through %s' , this . _proxy ) ;
309337 req = req . proxy ( this . _proxy ) ;
@@ -536,7 +564,7 @@ export class BitGoAPI implements BitGoBase {
536564 // client constants call cannot be authenticated using the normal HMAC validation
537565 // scheme, so we need to use a raw superagent instance to do this request.
538566 // 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' ) ) ;
540568 resultPromise . set ( 'BitGo-SDK-Version' , this . _version ) ;
541569 const result = await ( this . _proxy ? resultPromise . proxy ( this . _proxy ) : resultPromise ) ;
542570 BitGoAPI . _constants [ env ] = result . body . constants ;
0 commit comments