Skip to content

Commit

Permalink
feat: add IcpTokenManagerV1 as a top-level export of the package
Browse files Browse the repository at this point in the history
Also, make `authentication_type` non-case-sensitive
  • Loading branch information
dpopp07 committed May 28, 2019
1 parent ee1ddad commit cfa3e1b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ This Class is the base class that all generated service-specific classes inherit
### IamTokenManagerV1
This Class contains logic for managing an IAM token over its lifetime. Tokens can be requested or set manually. When requested, the token manager will either return the current token, request a new token or refresh the current token if it is expired. If a token is manually set, it must be managed by the user.

### IcpTokenManagerV1
This Class is similar in function to IamTokenManagerV1. The only difference is that the `url` parameter is required, it takes a `username` and `password` instead of an API key, and manages tokens for instances of ICP4D. To use this token manager in an SDK, the parameter `authentication_type` must be set to `icp4d` in the constructor.

### isFileParam
This function takes an Object and returns `true` if the object is a Stream, a Buffer, has a `value` property, or has a `data` property that is a file param (checked recursively).

Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

export { BaseService } from './lib/base_service';
export { IamTokenManagerV1 } from './iam-token-manager/v1';
export { IcpTokenManagerV1 } from './auth/icp-token-manager';
export * from './lib/helper';
export { default as qs } from './lib/querystring';
export { default as contentType } from './lib/content-type';
Expand Down
10 changes: 10 additions & 0 deletions lib/base_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface Credentials {
icp_access_token?: string;
iam_apikey?: string;
iam_url?: string;
authentication_type?: string;
}

function hasCredentials(obj: any): boolean {
Expand Down Expand Up @@ -171,6 +172,12 @@ export class BaseService {
options,
_options
);

// make authentication_type non-case-sensitive
if (typeof _options.authentication_type === 'string') {
_options.authentication_type = _options.authentication_type.toLowerCase();
}

if (_options.authentication_type === 'iam' || hasIamCredentials(_options)) {
this.tokenManager = new IamTokenManagerV1({
iamApikey: _options.iam_apikey || _options.password,
Expand Down Expand Up @@ -231,6 +238,9 @@ export class BaseService {
if (this._options.icp_access_token) {
credentials.icp_access_token = this._options.icp_access_token;
}
if (this._options.authentication_type) {
credentials.authentication_type = this._options.authentication_type;
}
return credentials;
}

Expand Down
37 changes: 17 additions & 20 deletions test/unit/iamTokenManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
const requestWrapper = require('../../lib/requestwrapper');
requestWrapper.sendRequest = jest.fn();

const jwt = require('jsonwebtoken');
jwt.decode = jest.fn(() => {
return { exp: 100, iat: 100 };
});

const IamTokenManagerV1 = require('../../iam-token-manager/v1').IamTokenManagerV1;

const CLIENT_ID_SECRET_WARNING =
Expand All @@ -22,19 +27,16 @@ describe('iam_token_manager_v1', function() {
const userManagedToken = 'abcd-1234';
const instance = new IamTokenManagerV1({ iamAccessToken: userManagedToken });
const requestMock = jest.spyOn(instance, 'requestToken');
const refreshMock = jest.spyOn(instance, 'refreshToken');

instance.getToken(function(err, token) {
expect(token).toBe(userManagedToken);
expect(requestMock).not.toHaveBeenCalled();
expect(refreshMock).not.toHaveBeenCalled();
done();
});
});

it('should turn an iam apikey into an access token', function(done) {
const instance = new IamTokenManagerV1({ iamApikey: 'abcd-1234' });
const refreshMock = jest.spyOn(instance, 'refreshToken');

const accessToken = '9012';
const iamResponse = {
Expand All @@ -51,7 +53,6 @@ describe('iam_token_manager_v1', function() {

instance.getToken(function(err, token) {
expect(token).toBe(accessToken);
expect(refreshMock).not.toHaveBeenCalled();
done();
});
});
Expand Down Expand Up @@ -85,15 +86,14 @@ describe('iam_token_manager_v1', function() {

instance.getToken(function(err, token) {
expect(token).toBe(accessToken);
expect(requestMock).not.toHaveBeenCalled();
expect(requestMock).toHaveBeenCalled();
done();
});
});

it('should use a valid access token if one is stored', function(done) {
const instance = new IamTokenManagerV1({ iamApikey: 'abcd-1234' });
const requestMock = jest.spyOn(instance, 'requestToken');
const refreshMock = jest.spyOn(instance, 'refreshToken');

const accessToken = '1234';
const currentTokenInfo = {
Expand All @@ -105,10 +105,11 @@ describe('iam_token_manager_v1', function() {
};

instance.tokenInfo = currentTokenInfo;
instance.timeToLive = currentTokenInfo.expires_in;
instance.expireTime = currentTokenInfo.expiration;

instance.getToken(function(err, token) {
expect(token).toBe(accessToken);
expect(refreshMock).not.toHaveBeenCalled();
expect(requestMock).not.toHaveBeenCalled();
done();
});
Expand All @@ -117,7 +118,6 @@ describe('iam_token_manager_v1', function() {
it('should return a user-managed access token if one is set post-construction', function(done) {
const instance = new IamTokenManagerV1({ iamApikey: 'abcd-1234' });
const requestMock = jest.spyOn(instance, 'requestToken');
const refreshMock = jest.spyOn(instance, 'refreshToken');

const accessToken = '9012';
const currentTokenInfo = {
Expand All @@ -133,7 +133,6 @@ describe('iam_token_manager_v1', function() {

instance.getToken(function(err, token) {
expect(token).toBe(accessToken);
expect(refreshMock).not.toHaveBeenCalled();
expect(requestMock).not.toHaveBeenCalled();
done();
});
Expand Down Expand Up @@ -167,14 +166,13 @@ describe('iam_token_manager_v1', function() {

instance.getToken(function(err, token) {
expect(token).toBe(accessToken);
expect(requestMock).not.toHaveBeenCalled();
expect(requestMock).toHaveBeenCalled();
done();
});
});

it('should request a new token when refresh token does not have expiration field', function(done) {
const instance = new IamTokenManagerV1({ iamApikey: 'abcd-1234' });
const refreshMock = jest.spyOn(instance, 'refreshToken');

const currentTokenInfo = {
access_token: '1234',
Expand All @@ -199,7 +197,6 @@ describe('iam_token_manager_v1', function() {

instance.getToken(function(err, token) {
expect(token).toBe(accessToken);
expect(refreshMock).not.toHaveBeenCalled();
done();
});
});
Expand All @@ -208,7 +205,7 @@ describe('iam_token_manager_v1', function() {
const instance = new IamTokenManagerV1({ iamApikey: 'abcd-1234' });

requestWrapper.sendRequest.mockImplementation((parameters, _callback) => {
_callback();
_callback(null, { access_token: 'abcd' });
});

instance.getToken(function() {
Expand All @@ -227,7 +224,7 @@ describe('iam_token_manager_v1', function() {
});

requestWrapper.sendRequest.mockImplementation((parameters, _callback) => {
_callback();
_callback(null, { access_token: 'abcd' });
});

instance.getToken(function() {
Expand All @@ -252,7 +249,7 @@ describe('iam_token_manager_v1', function() {
console.log.mockRestore();

requestWrapper.sendRequest.mockImplementation((parameters, _callback) => {
_callback();
_callback(null, { access_token: 'abcd' });
});

instance.getToken(function() {
Expand All @@ -276,7 +273,7 @@ describe('iam_token_manager_v1', function() {
console.log.mockRestore();

requestWrapper.sendRequest.mockImplementation((parameters, _callback) => {
_callback();
_callback(null, { access_token: 'abcd' });
});

instance.getToken(function() {
Expand All @@ -295,7 +292,7 @@ describe('iam_token_manager_v1', function() {
instance.setIamAuthorizationInfo('foo', 'bar');

requestWrapper.sendRequest.mockImplementation((parameters, _callback) => {
_callback();
_callback(null, { access_token: 'abcd' });
});

instance.getToken(function() {
Expand All @@ -321,7 +318,7 @@ describe('iam_token_manager_v1', function() {
console.log.mockRestore();

requestWrapper.sendRequest.mockImplementation((parameters, _callback) => {
_callback();
_callback(null, { access_token: 'abcd' });
});

instance.getToken(function() {
Expand All @@ -347,7 +344,7 @@ describe('iam_token_manager_v1', function() {
console.log.mockRestore();

requestWrapper.sendRequest.mockImplementation((parameters, _callback) => {
_callback();
_callback(null, { access_token: 'abcd' });
});

instance.getToken(function() {
Expand All @@ -366,7 +363,7 @@ describe('iam_token_manager_v1', function() {
instance.setIamAuthorizationInfo(null, null);

requestWrapper.sendRequest.mockImplementation((parameters, _callback) => {
_callback();
_callback(null, { access_token: 'abcd' });
});

instance.getToken(function() {
Expand Down

0 comments on commit cfa3e1b

Please sign in to comment.