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

Commit 78f1a4b

Browse files
committed
Add lib/tokens/IamTokenManager.js.
1 parent b2b01f7 commit 78f1a4b

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

lib/tokens/IamTokenManager.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright © 2019 IBM Corp. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
'use strict';
15+
16+
const a = require('async');
17+
const debug = require('debug')('cloudant:tokens:iamtokenmanager');
18+
const TokenManager = require('./TokenManager');
19+
20+
class IAMTokenManager extends TokenManager {
21+
constructor(client, jar, sessionUrl, iamTokenUrl, iamApiKey, iamClientId, iamClientSecret) {
22+
super(client, jar, sessionUrl);
23+
24+
this._iamTokenUrl = iamTokenUrl;
25+
this._iamApiKey = iamApiKey;
26+
this._iamClientId = iamClientId;
27+
this._iamClientSecret = iamClientSecret;
28+
}
29+
30+
_getToken(done) {
31+
var self = this;
32+
33+
debug('Making IAM session request.');
34+
let accessToken;
35+
a.series([
36+
(callback) => {
37+
let accessTokenAuth;
38+
if (self._iamClientId && self._iamClientSecret) {
39+
accessTokenAuth = { user: self._iamClientId, pass: self._iamClientSecret };
40+
}
41+
debug('Getting access token.');
42+
self._client({
43+
url: self._iamTokenUrl,
44+
method: 'POST',
45+
auth: accessTokenAuth,
46+
headers: { 'Accepts': 'application/json' },
47+
form: {
48+
'grant_type': 'urn:ibm:params:oauth:grant-type:apikey',
49+
'response_type': 'cloud_iam',
50+
'apikey': self._iamApiKey
51+
},
52+
json: true
53+
}, (error, response, body) => {
54+
if (error) {
55+
callback(error);
56+
} else if (response.statusCode === 200) {
57+
if (body.access_token) {
58+
accessToken = body.access_token;
59+
debug('Retrieved access token from IAM token service.');
60+
callback();
61+
} else {
62+
callback(new Error('Invalid response from IAM token service'), response);
63+
}
64+
} else {
65+
let msg = `Failed to acquire access token. Status code: ${response.statusCode}`;
66+
callback(new Error(msg), response);
67+
}
68+
});
69+
},
70+
(callback) => {
71+
debug('Perform IAM cookie based user login.');
72+
self._client({
73+
url: self._sessionUrl,
74+
method: 'POST',
75+
form: { 'access_token': accessToken },
76+
jar: self._jar,
77+
json: true
78+
}, (error, response, body) => {
79+
if (error) {
80+
callback(error);
81+
} else if (response.statusCode === 200) {
82+
debug('Successfully renewed IAM session.');
83+
callback(null, response);
84+
} else {
85+
let msg = `Failed to exchange IAM token with Cloudant. Status code: ${response.statusCode}`;
86+
callback(new Error(msg), response);
87+
}
88+
});
89+
}
90+
], done);
91+
}
92+
93+
setIamApiKey(newIamApiKey) {
94+
this._iamApiKey = newIamApiKey;
95+
this.setForceRenew();
96+
}
97+
}
98+
99+
module.exports = IAMTokenManager;

0 commit comments

Comments
 (0)