Skip to content

Commit 1e2d28d

Browse files
authored
Merge pull request #755 from watson-developer-cloud/fix-multi-auth-bug
fix: if basic and iam creds given, use iam and dont set basic auth he…
2 parents 3c6882c + e91e7b3 commit 1e2d28d

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

lib/base_service.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ function hasBasicCredentials(obj: any): boolean {
7676
return obj && obj.username && obj.password && obj.username !== 'apikey';
7777
}
7878

79+
function hasIamCredentials(obj: any): boolean {
80+
return obj && (obj.iam_apikey || obj.iam_access_token);
81+
}
82+
7983
export class BaseService {
8084
static URL: string;
8185
name: string;
@@ -125,7 +129,7 @@ export class BaseService {
125129
options,
126130
_options
127131
);
128-
if (_options.iam_apikey || _options.iam_access_token) {
132+
if (hasIamCredentials(_options)) {
129133
this.tokenManager = new IamTokenManagerV1({
130134
iamApikey: _options.iam_apikey,
131135
iamAccessToken: _options.iam_access_token,
@@ -276,15 +280,17 @@ export class BaseService {
276280
'api_key, and iam_access_token.';
277281
throw new Error(errorMessage);
278282
}
279-
if (hasBasicCredentials(_options)) {
280-
// Calculate and add Authorization header to base options
281-
const encodedCredentials = bufferFrom(
282-
`${_options.username}:${_options.password}`
283-
).toString('base64');
284-
const authHeader = { Authorization: `Basic ${encodedCredentials}` };
285-
_options.headers = extend(authHeader, _options.headers);
286-
} else {
287-
_options.qs = extend({ api_key: _options.api_key }, _options.qs);
283+
if (!hasIamCredentials(_options) && _options.username !== 'apikey') {
284+
if (hasBasicCredentials(_options)) {
285+
// Calculate and add Authorization header to base options
286+
const encodedCredentials = bufferFrom(
287+
`${_options.username}:${_options.password}`
288+
).toString('base64');
289+
const authHeader = { Authorization: `Basic ${encodedCredentials}` };
290+
_options.headers = extend(authHeader, _options.headers);
291+
} else {
292+
_options.qs = extend({ api_key: _options.api_key }, _options.qs);
293+
}
288294
}
289295
}
290296
return _options;

test/unit/test.base_service.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,16 @@ describe('BaseService', function() {
242242
assert.equal(instance.tokenManager.iamApikey, apikey);
243243
assert.equal(instance._options.headers, undefined);
244244
});
245+
246+
it('should not create a basic auth header if iam creds are given', function() {
247+
const apikey = 'abcd-1234';
248+
const instance = new TestService({
249+
iam_apikey: apikey,
250+
username: 'notarealuser',
251+
password: 'badpassword1',
252+
});
253+
assert.notEqual(instance.tokenManager, null);
254+
assert.equal(instance.tokenManager.iamApikey, apikey);
255+
assert.equal(instance._options.headers, undefined);
256+
});
245257
});

0 commit comments

Comments
 (0)