Skip to content

Commit cfa3e1b

Browse files
committed
feat: add IcpTokenManagerV1 as a top-level export of the package
Also, make `authentication_type` non-case-sensitive
1 parent ee1ddad commit cfa3e1b

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ This Class is the base class that all generated service-specific classes inherit
2424
### IamTokenManagerV1
2525
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.
2626

27+
### IcpTokenManagerV1
28+
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.
29+
2730
### isFileParam
2831
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).
2932

index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
export { BaseService } from './lib/base_service';
2222
export { IamTokenManagerV1 } from './iam-token-manager/v1';
23+
export { IcpTokenManagerV1 } from './auth/icp-token-manager';
2324
export * from './lib/helper';
2425
export { default as qs } from './lib/querystring';
2526
export { default as contentType } from './lib/content-type';

lib/base_service.ts

+10
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export interface Credentials {
6262
icp_access_token?: string;
6363
iam_apikey?: string;
6464
iam_url?: string;
65+
authentication_type?: string;
6566
}
6667

6768
function hasCredentials(obj: any): boolean {
@@ -171,6 +172,12 @@ export class BaseService {
171172
options,
172173
_options
173174
);
175+
176+
// make authentication_type non-case-sensitive
177+
if (typeof _options.authentication_type === 'string') {
178+
_options.authentication_type = _options.authentication_type.toLowerCase();
179+
}
180+
174181
if (_options.authentication_type === 'iam' || hasIamCredentials(_options)) {
175182
this.tokenManager = new IamTokenManagerV1({
176183
iamApikey: _options.iam_apikey || _options.password,
@@ -231,6 +238,9 @@ export class BaseService {
231238
if (this._options.icp_access_token) {
232239
credentials.icp_access_token = this._options.icp_access_token;
233240
}
241+
if (this._options.authentication_type) {
242+
credentials.authentication_type = this._options.authentication_type;
243+
}
234244
return credentials;
235245
}
236246

test/unit/iamTokenManager.test.js

+17-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
const requestWrapper = require('../../lib/requestwrapper');
55
requestWrapper.sendRequest = jest.fn();
66

7+
const jwt = require('jsonwebtoken');
8+
jwt.decode = jest.fn(() => {
9+
return { exp: 100, iat: 100 };
10+
});
11+
712
const IamTokenManagerV1 = require('../../iam-token-manager/v1').IamTokenManagerV1;
813

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

2731
instance.getToken(function(err, token) {
2832
expect(token).toBe(userManagedToken);
2933
expect(requestMock).not.toHaveBeenCalled();
30-
expect(refreshMock).not.toHaveBeenCalled();
3134
done();
3235
});
3336
});
3437

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

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

5254
instance.getToken(function(err, token) {
5355
expect(token).toBe(accessToken);
54-
expect(refreshMock).not.toHaveBeenCalled();
5556
done();
5657
});
5758
});
@@ -85,15 +86,14 @@ describe('iam_token_manager_v1', function() {
8586

8687
instance.getToken(function(err, token) {
8788
expect(token).toBe(accessToken);
88-
expect(requestMock).not.toHaveBeenCalled();
89+
expect(requestMock).toHaveBeenCalled();
8990
done();
9091
});
9192
});
9293

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

9898
const accessToken = '1234';
9999
const currentTokenInfo = {
@@ -105,10 +105,11 @@ describe('iam_token_manager_v1', function() {
105105
};
106106

107107
instance.tokenInfo = currentTokenInfo;
108+
instance.timeToLive = currentTokenInfo.expires_in;
109+
instance.expireTime = currentTokenInfo.expiration;
108110

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

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

134134
instance.getToken(function(err, token) {
135135
expect(token).toBe(accessToken);
136-
expect(refreshMock).not.toHaveBeenCalled();
137136
expect(requestMock).not.toHaveBeenCalled();
138137
done();
139138
});
@@ -167,14 +166,13 @@ describe('iam_token_manager_v1', function() {
167166

168167
instance.getToken(function(err, token) {
169168
expect(token).toBe(accessToken);
170-
expect(requestMock).not.toHaveBeenCalled();
169+
expect(requestMock).toHaveBeenCalled();
171170
done();
172171
});
173172
});
174173

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

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

200198
instance.getToken(function(err, token) {
201199
expect(token).toBe(accessToken);
202-
expect(refreshMock).not.toHaveBeenCalled();
203200
done();
204201
});
205202
});
@@ -208,7 +205,7 @@ describe('iam_token_manager_v1', function() {
208205
const instance = new IamTokenManagerV1({ iamApikey: 'abcd-1234' });
209206

210207
requestWrapper.sendRequest.mockImplementation((parameters, _callback) => {
211-
_callback();
208+
_callback(null, { access_token: 'abcd' });
212209
});
213210

214211
instance.getToken(function() {
@@ -227,7 +224,7 @@ describe('iam_token_manager_v1', function() {
227224
});
228225

229226
requestWrapper.sendRequest.mockImplementation((parameters, _callback) => {
230-
_callback();
227+
_callback(null, { access_token: 'abcd' });
231228
});
232229

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

254251
requestWrapper.sendRequest.mockImplementation((parameters, _callback) => {
255-
_callback();
252+
_callback(null, { access_token: 'abcd' });
256253
});
257254

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

278275
requestWrapper.sendRequest.mockImplementation((parameters, _callback) => {
279-
_callback();
276+
_callback(null, { access_token: 'abcd' });
280277
});
281278

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

297294
requestWrapper.sendRequest.mockImplementation((parameters, _callback) => {
298-
_callback();
295+
_callback(null, { access_token: 'abcd' });
299296
});
300297

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

323320
requestWrapper.sendRequest.mockImplementation((parameters, _callback) => {
324-
_callback();
321+
_callback(null, { access_token: 'abcd' });
325322
});
326323

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

349346
requestWrapper.sendRequest.mockImplementation((parameters, _callback) => {
350-
_callback();
347+
_callback(null, { access_token: 'abcd' });
351348
});
352349

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

368365
requestWrapper.sendRequest.mockImplementation((parameters, _callback) => {
369-
_callback();
366+
_callback(null, { access_token: 'abcd' });
370367
});
371368

372369
instance.getToken(function() {

0 commit comments

Comments
 (0)