Skip to content

Commit

Permalink
feat: support authenticating with ibm cloud private
Browse files Browse the repository at this point in the history
  • Loading branch information
dpopp07 committed Sep 12, 2018
1 parent 9710f60 commit 0d1774c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/base_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,19 @@ function hasCredentials(obj: any): boolean {
}

function hasBasicCredentials(obj: any): boolean {
return obj && obj.username && obj.password && obj.username !== 'apikey';
return obj && obj.username && obj.password && !usesBasicForIam(obj);
}

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

// returns true if the user provides basic auth creds with the intention
// of using IAM auth
function usesBasicForIam(obj: any): boolean {
return obj.username === 'apikey' && !obj.password.startsWith('icp-');
}

export class BaseService {
static URL: string;
name: string;
Expand Down Expand Up @@ -135,7 +141,7 @@ export class BaseService {
iamAccessToken: _options.iam_access_token,
iamUrl: _options.iam_url
});
} else if (_options.username === 'apikey') {
} else if (usesBasicForIam(_options)) {
this.tokenManager = new IamTokenManagerV1({
iamApikey: _options.password,
iamUrl: _options.iam_url
Expand Down Expand Up @@ -280,7 +286,7 @@ export class BaseService {
'api_key, and iam_access_token.';
throw new Error(errorMessage);
}
if (!hasIamCredentials(_options) && _options.username !== 'apikey') {
if (!hasIamCredentials(_options) && !usesBasicForIam(_options)) {
if (hasBasicCredentials(_options)) {
// Calculate and add Authorization header to base options
const encodedCredentials = bufferFrom(
Expand Down
10 changes: 10 additions & 0 deletions test/unit/test.base_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,14 @@ describe('BaseService', function() {
assert.equal(instance.tokenManager.iamApikey, apikey);
assert.equal(instance._options.headers, undefined);
});

it('should create a basic auth header if username is `apikey` and password starts with `icp-`', function() {
const instance = new TestService({
username: 'apikey',
password: 'icp-1234',
});
const authHeader = instance._options.headers.Authorization;
assert.equal(instance.tokenManager, null);
assert(authHeader.startsWith('Basic'));
});
});

0 comments on commit 0d1774c

Please sign in to comment.