Skip to content

Commit 0d1774c

Browse files
committed
feat: support authenticating with ibm cloud private
1 parent 9710f60 commit 0d1774c

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/base_service.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,19 @@ function hasCredentials(obj: any): boolean {
7373
}
7474

7575
function hasBasicCredentials(obj: any): boolean {
76-
return obj && obj.username && obj.password && obj.username !== 'apikey';
76+
return obj && obj.username && obj.password && !usesBasicForIam(obj);
7777
}
7878

7979
function hasIamCredentials(obj: any): boolean {
8080
return obj && (obj.iam_apikey || obj.iam_access_token);
8181
}
8282

83+
// returns true if the user provides basic auth creds with the intention
84+
// of using IAM auth
85+
function usesBasicForIam(obj: any): boolean {
86+
return obj.username === 'apikey' && !obj.password.startsWith('icp-');
87+
}
88+
8389
export class BaseService {
8490
static URL: string;
8591
name: string;
@@ -135,7 +141,7 @@ export class BaseService {
135141
iamAccessToken: _options.iam_access_token,
136142
iamUrl: _options.iam_url
137143
});
138-
} else if (_options.username === 'apikey') {
144+
} else if (usesBasicForIam(_options)) {
139145
this.tokenManager = new IamTokenManagerV1({
140146
iamApikey: _options.password,
141147
iamUrl: _options.iam_url
@@ -280,7 +286,7 @@ export class BaseService {
280286
'api_key, and iam_access_token.';
281287
throw new Error(errorMessage);
282288
}
283-
if (!hasIamCredentials(_options) && _options.username !== 'apikey') {
289+
if (!hasIamCredentials(_options) && !usesBasicForIam(_options)) {
284290
if (hasBasicCredentials(_options)) {
285291
// Calculate and add Authorization header to base options
286292
const encodedCredentials = bufferFrom(

test/unit/test.base_service.js

+10
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,14 @@ describe('BaseService', function() {
254254
assert.equal(instance.tokenManager.iamApikey, apikey);
255255
assert.equal(instance._options.headers, undefined);
256256
});
257+
258+
it('should create a basic auth header if username is `apikey` and password starts with `icp-`', function() {
259+
const instance = new TestService({
260+
username: 'apikey',
261+
password: 'icp-1234',
262+
});
263+
const authHeader = instance._options.headers.Authorization;
264+
assert.equal(instance.tokenManager, null);
265+
assert(authHeader.startsWith('Basic'));
266+
});
257267
});

0 commit comments

Comments
 (0)