Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit e44b588

Browse files
authored
Merge pull request #362 from cloudant/add-iam-token-server-auth
Add option for client to authenticate with IAM token server.
2 parents 667d9db + 65674f1 commit e44b588

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# UNRELEASED
2+
- [NEW] Added option for client to authenticate with IAM token server.
3+
14
# 3.0.2 (2019-01-07)
25
- [FIXED] Remove unnecessary `@types/nano` dependancy.
36

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ var cloudant = new Cloudant({ url: myurl, maxAttempt: 5, plugins: [ 'iamauth', {
393393
394394
The production IAM token service at https://iam.bluemix.net/identity/token is
395395
used by default. You can set `iamTokenUrl` in your plugin configuration to
396-
override this.
396+
override this. To authenticate with the IAM token service set `iamClientId`
397+
and `iamClientSecret` in your plugin configuration.
397398
398399
For example:
399400
```js

plugins/iamauth.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2017 IBM Corp. All rights reserved.
1+
// Copyright © 2017, 2019 IBM Corp. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -125,11 +125,15 @@ class IAMPlugin extends BasePlugin {
125125
var accessToken = null;
126126
async.series([
127127
function(callback) {
128+
var accessTokenAuth;
129+
if (typeof cfg.iamClientId !== 'undefined' && typeof cfg.iamClientSecret !== 'undefined') {
130+
accessTokenAuth = { user: cfg.iamClientId, pass: cfg.iamClientSecret };
131+
}
128132
// get access token
129133
self._client({
130134
url: self.tokenUrl,
131135
method: 'POST',
132-
auth: { user: 'bx', pass: 'bx' },
136+
auth: accessTokenAuth,
133137
headers: { 'Accepts': 'application/json' },
134138
form: {
135139
'grant_type': 'urn:ibm:params:oauth:grant-type:apikey',

test/plugins/iamauth.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2017 IBM Corp. All rights reserved.
1+
// Copyright © 2017, 2019 IBM Corp. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -31,6 +31,8 @@ const TOKEN_SERVER = 'https://iam.bluemix.net';
3131
const DBNAME = `/nodejs-cloudant-${uuidv4()}`;
3232

3333
// mocks
34+
const MOCK_TOKEN_SERVER_USER = 'ASPialYTheOS';
35+
const MOCK_TOKEN_SERVER_PASS = 'Zr28yT54^y!Kk&$M';
3436
const MOCK_ACCESS_TOKEN = 'eyJraWQiOiIyMDE3MDQwMi0wMDowMDowMCIsImFsZyI6IlJTMj' +
3537
'U2In0.eeyJraWQiOiIyMDE3MDQwMi0wMDowMDowMCIsImFsZyI6IlJTMjU2In0.eyJpYW1faWQiO' +
3638
'iJJQk1pZC0yNzAwMDdHRjBEIiwiaWQiOiJJQk1pZC0yNzAwMDdHRjBEIiwicmVhbG1pZCI6IklCT' +
@@ -143,6 +145,45 @@ describe('#db IAMAuth Plugin', function() {
143145
});
144146
});
145147

148+
it('performs request and returns 200 response when authenticating with IAM token service', function(done) {
149+
if (process.env.NOCK_OFF) {
150+
this.skip();
151+
}
152+
153+
var iamMocks = nock(TOKEN_SERVER)
154+
.post('/identity/token', {
155+
'grant_type': 'urn:ibm:params:oauth:grant-type:apikey',
156+
'response_type': 'cloud_iam',
157+
'apikey': IAM_API_KEY
158+
})
159+
.basicAuth({ user: MOCK_TOKEN_SERVER_USER, pass: MOCK_TOKEN_SERVER_PASS })
160+
.reply(200, MOCK_IAM_TOKEN_RESPONSE);
161+
162+
var cloudantMocks = nock(SERVER)
163+
.post('/_iam_session', {access_token: MOCK_ACCESS_TOKEN})
164+
.reply(200, {ok: true}, MOCK_SET_IAM_SESSION_HEADER)
165+
.get(DBNAME)
166+
.reply(200, {doc_count: 0});
167+
168+
var cloudantClient = new Client({ plugins: { iamauth: {
169+
iamApiKey: IAM_API_KEY,
170+
iamClientId: MOCK_TOKEN_SERVER_USER,
171+
iamClientSecret: MOCK_TOKEN_SERVER_PASS
172+
}}});
173+
var req = { url: SERVER + DBNAME, method: 'GET' };
174+
cloudantClient.request(req, function(err, resp, data) {
175+
assert.equal(err, null);
176+
if (!process.env.NOCK_OFF) {
177+
assert.equal(resp.request.headers.cookie, MOCK_IAM_SESSION);
178+
}
179+
assert.equal(resp.statusCode, 200);
180+
assert.ok(data.indexOf('"doc_count":0') > -1);
181+
iamMocks.done();
182+
cloudantMocks.done();
183+
done();
184+
});
185+
});
186+
146187
it('performs multiple requests that return 200 responses with only a single session request', function(done) {
147188
// NOTE: Use NOCK_OFF=true to test using a real CouchDB instance.
148189
var iamMocks = nock(TOKEN_SERVER)

0 commit comments

Comments
 (0)