Skip to content

Commit

Permalink
fix: share service request wrapper instance with token managers (#36)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dpopp07 authored Aug 7, 2019
1 parent 06ef43e commit e7609e2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions auth/iam-token-manager-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type Options = {
iamAccessToken?: string;
iamClientId?: string;
iamClientSecret?: string;
requestWrapper?: any;
}

// this interface is a representation of the response
Expand Down
1 change: 1 addition & 0 deletions auth/icp4d-token-manager-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type Options = {
username?: string;
password?: string;
disableSslVerification?: boolean;
requestWrapper?: any;
}

// this interface is a representation of the response
Expand Down
4 changes: 3 additions & 1 deletion auth/jwt-token-manager-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function getCurrentTime(): number {
export type Options = {
accessToken?: string;
url?: string;
requestWrapper?: any;
}

export class JwtTokenManagerV1 {
Expand Down Expand Up @@ -61,7 +62,8 @@ export class JwtTokenManagerV1 {
this.userAccessToken = options.accessToken;
}

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

/**
Expand Down
9 changes: 7 additions & 2 deletions lib/base_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,20 +200,24 @@ export class BaseService {
// used to disable ssl checking for icp
this._options.rejectUnauthorized = !options.disable_ssl_verification;

this.requestWrapperInstance = new RequestWrapper(this._options);

if (_options.authentication_type === 'iam' || hasIamCredentials(_options)) {
this.tokenManager = new IamTokenManagerV1({
iamApikey: _options.iam_apikey,
accessToken: _options.iam_access_token,
url: _options.iam_url,
iamClientId: _options.iam_client_id,
iamClientSecret: _options.iam_client_secret,
requestWrapper: this.requestWrapperInstance,
});
} else if (usesBasicForIam(_options)) {
this.tokenManager = new IamTokenManagerV1({
iamApikey: _options.password,
url: _options.iam_url,
iamClientId: _options.iam_client_id,
iamClientSecret: _options.iam_client_secret,
requestWrapper: this.requestWrapperInstance,
});
} else if (isForICP4D(_options)) {
if (!_options.icp4d_url && !_options.icp4d_access_token) {
Expand All @@ -225,12 +229,11 @@ export class BaseService {
password: _options.password,
accessToken: _options.icp4d_access_token,
disableSslVerification: options.disable_ssl_verification,
requestWrapper: this.requestWrapperInstance,
});
} else {
this.tokenManager = null;
}

this.requestWrapperInstance = new RequestWrapper(this._options);
}

/**
Expand Down Expand Up @@ -298,10 +301,12 @@ export class BaseService {
accessToken: access_token,
url: this._options.icp4d_url,
disableSslVerification: this._options.disable_ssl_verification,
requestWrapper: this.requestWrapperInstance,
});
} else {
this.tokenManager = new IamTokenManagerV1({
accessToken: access_token,
requestWrapper: this.requestWrapperInstance,
});
}
}
Expand Down
39 changes: 39 additions & 0 deletions test/unit/base-service-integration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const util = require('util');
const BaseService = require('../../lib/base_service').BaseService;

function TestService(options) {
BaseService.call(this, options);
}

util.inherits(TestService, BaseService);

TestService.prototype.name = 'test';
TestService.prototype.version = 'v1';

TestService.URL = 'https://gateway.watsonplatform.net/test/api';

/*
* Test the way the BaseService interacts with other modules
*/
describe('Base service - token manager - integration', function() {
it('should propagate request properties to token managers', function() {
const hostname = 'jabba.the.hutt';
const port = 'mos eisley';

const instance = new TestService({
iam_apikey: 'r2-d2',
proxy: {
hostname,
port,
},
});

expect(instance.tokenManager).toBeDefined();

const axiosOptions = instance.tokenManager.requestWrapperInstance.axiosInstance.defaults;
expect(axiosOptions.proxy.hostname).toBe(hostname);
expect(axiosOptions.proxy.port).toBe(port);
});
});

0 comments on commit e7609e2

Please sign in to comment.