Skip to content

Commit e7609e2

Browse files
authored
fix: share service request wrapper instance with token managers (#36)
this will allow users to configure all requests, rather than just the service requests this was causing a bug where users could not use the SDK behind a corporate proxy
1 parent 06ef43e commit e7609e2

5 files changed

+51
-3
lines changed

auth/iam-token-manager-v1.ts

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export type Options = {
4242
iamAccessToken?: string;
4343
iamClientId?: string;
4444
iamClientSecret?: string;
45+
requestWrapper?: any;
4546
}
4647

4748
// this interface is a representation of the response

auth/icp4d-token-manager-v1.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type Options = {
2424
username?: string;
2525
password?: string;
2626
disableSslVerification?: boolean;
27+
requestWrapper?: any;
2728
}
2829

2930
// this interface is a representation of the response

auth/jwt-token-manager-v1.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function getCurrentTime(): number {
2525
export type Options = {
2626
accessToken?: string;
2727
url?: string;
28+
requestWrapper?: any;
2829
}
2930

3031
export class JwtTokenManagerV1 {
@@ -61,7 +62,8 @@ export class JwtTokenManagerV1 {
6162
this.userAccessToken = options.accessToken;
6263
}
6364

64-
this.requestWrapperInstance = new RequestWrapper();
65+
// not required, but the SDK will always pass in a request wrapper instance
66+
this.requestWrapperInstance = options.requestWrapper || new RequestWrapper();
6567
}
6668

6769
/**

lib/base_service.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,24 @@ export class BaseService {
200200
// used to disable ssl checking for icp
201201
this._options.rejectUnauthorized = !options.disable_ssl_verification;
202202

203+
this.requestWrapperInstance = new RequestWrapper(this._options);
204+
203205
if (_options.authentication_type === 'iam' || hasIamCredentials(_options)) {
204206
this.tokenManager = new IamTokenManagerV1({
205207
iamApikey: _options.iam_apikey,
206208
accessToken: _options.iam_access_token,
207209
url: _options.iam_url,
208210
iamClientId: _options.iam_client_id,
209211
iamClientSecret: _options.iam_client_secret,
212+
requestWrapper: this.requestWrapperInstance,
210213
});
211214
} else if (usesBasicForIam(_options)) {
212215
this.tokenManager = new IamTokenManagerV1({
213216
iamApikey: _options.password,
214217
url: _options.iam_url,
215218
iamClientId: _options.iam_client_id,
216219
iamClientSecret: _options.iam_client_secret,
220+
requestWrapper: this.requestWrapperInstance,
217221
});
218222
} else if (isForICP4D(_options)) {
219223
if (!_options.icp4d_url && !_options.icp4d_access_token) {
@@ -225,12 +229,11 @@ export class BaseService {
225229
password: _options.password,
226230
accessToken: _options.icp4d_access_token,
227231
disableSslVerification: options.disable_ssl_verification,
232+
requestWrapper: this.requestWrapperInstance,
228233
});
229234
} else {
230235
this.tokenManager = null;
231236
}
232-
233-
this.requestWrapperInstance = new RequestWrapper(this._options);
234237
}
235238

236239
/**
@@ -298,10 +301,12 @@ export class BaseService {
298301
accessToken: access_token,
299302
url: this._options.icp4d_url,
300303
disableSslVerification: this._options.disable_ssl_verification,
304+
requestWrapper: this.requestWrapperInstance,
301305
});
302306
} else {
303307
this.tokenManager = new IamTokenManagerV1({
304308
accessToken: access_token,
309+
requestWrapper: this.requestWrapperInstance,
305310
});
306311
}
307312
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const util = require('util');
4+
const BaseService = require('../../lib/base_service').BaseService;
5+
6+
function TestService(options) {
7+
BaseService.call(this, options);
8+
}
9+
10+
util.inherits(TestService, BaseService);
11+
12+
TestService.prototype.name = 'test';
13+
TestService.prototype.version = 'v1';
14+
15+
TestService.URL = 'https://gateway.watsonplatform.net/test/api';
16+
17+
/*
18+
* Test the way the BaseService interacts with other modules
19+
*/
20+
describe('Base service - token manager - integration', function() {
21+
it('should propagate request properties to token managers', function() {
22+
const hostname = 'jabba.the.hutt';
23+
const port = 'mos eisley';
24+
25+
const instance = new TestService({
26+
iam_apikey: 'r2-d2',
27+
proxy: {
28+
hostname,
29+
port,
30+
},
31+
});
32+
33+
expect(instance.tokenManager).toBeDefined();
34+
35+
const axiosOptions = instance.tokenManager.requestWrapperInstance.axiosInstance.defaults;
36+
expect(axiosOptions.proxy.hostname).toBe(hostname);
37+
expect(axiosOptions.proxy.port).toBe(port);
38+
});
39+
});

0 commit comments

Comments
 (0)