Skip to content

Commit defc37a

Browse files
committed
feat: auth service now supports returning IAM tokens
1 parent 872b29c commit defc37a

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

authorization/v1.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,31 @@ class AuthorizationV1 extends BaseService {
4444
}
4545

4646
/**
47-
* Get a percent-encoded authorization token based on resource query string param
47+
* If using an RC service, get an IAM access token. If using a CF service,
48+
* get a percent-encoded authorization token based on resource query string param
4849
*
4950
* @param {Object} [options]
5051
* @param {String} [options.url] defaults to url supplied to constructor (if any)
51-
* @param {Function(err, token)} callback - called with a %-encoded token
52+
* @param {Function(err, token)} callback - called with a %-encoded token if CF
5253
*/
5354
getToken(params, callback) {
5455
if (typeof params === 'function') {
5556
callback = params;
5657
params = { url: this['target_url'] };
5758
}
59+
60+
// if the service is an RC instance, return an IAM access token
61+
if (this.tokenManager) {
62+
// callback should expect (err, token) format,
63+
// which is what the token manager will pass in
64+
return this.tokenManager.getToken(callback);
65+
}
66+
67+
// otherwise, return a CF Watson token
5868
if (!params.url) {
5969
callback(new Error('Missing required parameters: url'));
6070
return;
6171
}
62-
6372
const parameters = {
6473
options: {
6574
method: 'GET',

test/unit/test.authorization.v1.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ const assert = require('assert');
44
const watson = require('../../index');
55
const nock = require('nock');
66

7+
const sinon = require('sinon');
8+
const IamTokenManagerV1 = require('../../iam-token-manager/v1').IamTokenManagerV1;
9+
710
describe('authorization', function() {
811
// Test params
912
const service_request = {
@@ -16,6 +19,11 @@ describe('authorization', function() {
1619
version: 'v1',
1720
};
1821

22+
const rc_service = {
23+
iam_apikey: 'abc123',
24+
version: 'v1',
25+
};
26+
1927
// tokens are URL-encoded when recieved from the service
2028
const mock_token = 'token';
2129

@@ -67,5 +75,21 @@ describe('authorization', function() {
6775
it('should default to url from credentials', function(done) {
6876
authorization.getToken(checkToken(done));
6977
});
78+
79+
it('should return an iam access token if given iam_api_key', function(done) {
80+
const rc_authorization = watson.authorization(rc_service);
81+
assert.notEqual(rc_authorization.tokenManager, null);
82+
83+
// mock the token manager
84+
const tokenManager = new IamTokenManagerV1({ iamApikey: rc_service.iam_apikey });
85+
const requestStub = sinon.stub(tokenManager, 'requestToken');
86+
requestStub.yields(null, { access_token: mock_token });
87+
88+
// use the mocked token manager - we have already asserted that the
89+
// authorization object created a token manager for itself
90+
rc_authorization.tokenManager = tokenManager;
91+
92+
rc_authorization.getToken(checkToken(done));
93+
});
7094
});
7195
});

test/unit/test.iam_token_manager.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('iam_token_manager_v1', function() {
2020
});
2121

2222
it('should turn an iam apikey into an access token', function(done) {
23-
const instance = new IamTokenManagerV1({ iam_api_key: 'abcd-1234' });
23+
const instance = new IamTokenManagerV1({ iamApikey: 'abcd-1234' });
2424
const requestStub = sinon.stub(instance, 'requestToken');
2525
const refreshSpy = sinon.spy(instance, 'refreshToken');
2626

@@ -43,7 +43,7 @@ describe('iam_token_manager_v1', function() {
4343
});
4444

4545
it('should refresh an expired access token', function(done) {
46-
const instance = new IamTokenManagerV1({ iam_api_key: 'abcd-1234' });
46+
const instance = new IamTokenManagerV1({ iamApikey: 'abcd-1234' });
4747
const requestSpy = sinon.spy(instance, 'requestToken');
4848
const refreshStub = sinon.stub(instance, 'refreshToken');
4949

@@ -76,7 +76,7 @@ describe('iam_token_manager_v1', function() {
7676
});
7777

7878
it('should use a valid access token if one is stored', function(done) {
79-
const instance = new IamTokenManagerV1({ iam_api_key: 'abcd-1234' });
79+
const instance = new IamTokenManagerV1({ iamApikey: 'abcd-1234' });
8080
const requestSpy = sinon.spy(instance, 'requestToken');
8181
const refreshSpy = sinon.spy(instance, 'refreshToken');
8282

@@ -100,7 +100,7 @@ describe('iam_token_manager_v1', function() {
100100
});
101101

102102
it('should return a user-managed access token if one is set post-construction', function(done) {
103-
const instance = new IamTokenManagerV1({ iam_api_key: 'abcd-1234' });
103+
const instance = new IamTokenManagerV1({ iamApikey: 'abcd-1234' });
104104
const requestSpy = sinon.spy(instance, 'requestToken');
105105
const refreshSpy = sinon.spy(instance, 'refreshToken');
106106

0 commit comments

Comments
 (0)