Skip to content

Commit

Permalink
fix: use correct authenticator type constants (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
padamstx committed Oct 15, 2021
1 parent 7a1d6ad commit ae19adc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
4 changes: 2 additions & 2 deletions auth/authenticators/authenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ export class Authenticator implements AuthenticatorInterface {
*/
static AUTHTYPE_BASIC = 'basic';

static AUTHTYPE_BEARERTOKEN = 'bearertoken';
static AUTHTYPE_BEARERTOKEN = 'bearerToken';

static AUTHTYPE_IAM = 'iam';

static AUTHTYPE_CONTAINER = 'container';

static AUTHTYPE_CP4D = 'cp4d';

static AUTHTYPE_NOAUTH = 'noauth';
static AUTHTYPE_NOAUTH = 'noAuth';

static AUTHTYPE_UNKNOWN = 'unknown';

Expand Down
43 changes: 19 additions & 24 deletions auth/utils/get-authenticator-from-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,34 +74,29 @@ export function getAuthenticatorFromEnvironment(serviceName: string): Authentica
authType = credentials.authtype;
}
if (!authType || typeof authType !== 'string') {
authType = credentials.apikey ? 'iam' : 'container';
authType = credentials.apikey ? Authenticator.AUTHTYPE_IAM : Authenticator.AUTHTYPE_CONTAINER;
}

// create and return the appropriate authenticator
// Create and return the appropriate authenticator.
let authenticator;

// fold authType to lower case for case insensitivity
switch (authType.toLowerCase()) {
case Authenticator.AUTHTYPE_NOAUTH:
authenticator = new NoAuthAuthenticator();
break;
case Authenticator.AUTHTYPE_BASIC:
authenticator = new BasicAuthenticator(credentials);
break;
case Authenticator.AUTHTYPE_BEARERTOKEN:
authenticator = new BearerTokenAuthenticator(credentials);
break;
case Authenticator.AUTHTYPE_CP4D:
authenticator = new CloudPakForDataAuthenticator(credentials);
break;
case Authenticator.AUTHTYPE_IAM:
authenticator = new IamAuthenticator(credentials);
break;
case Authenticator.AUTHTYPE_CONTAINER:
authenticator = new ContainerAuthenticator(credentials);
break;
default:
throw new Error(`Invalid value for AUTH_TYPE: ${authType}`);
// Compare the authType against our constants case-insensitively to
// determine which authenticator type needs to be constructed.
authType = authType.toLowerCase();
if (authType === Authenticator.AUTHTYPE_NOAUTH.toLowerCase()) {
authenticator = new NoAuthAuthenticator();
} else if (authType === Authenticator.AUTHTYPE_BASIC.toLowerCase()) {
authenticator = new BasicAuthenticator(credentials);
} else if (authType === Authenticator.AUTHTYPE_BEARERTOKEN.toLowerCase()) {
authenticator = new BearerTokenAuthenticator(credentials);
} else if (authType === Authenticator.AUTHTYPE_CP4D.toLowerCase()) {
authenticator = new CloudPakForDataAuthenticator(credentials);
} else if (authType === Authenticator.AUTHTYPE_IAM.toLowerCase()) {
authenticator = new IamAuthenticator(credentials);
} else if (authType === Authenticator.AUTHTYPE_CONTAINER.toLowerCase()) {
authenticator = new ContainerAuthenticator(credentials);
} else {
throw new Error(`Invalid value for AUTH_TYPE: ${authType}`);
}

return authenticator;
Expand Down
8 changes: 4 additions & 4 deletions test/unit/get-authenticator-from-environment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ function setUpNoAuthPayload() {

function setUpBasicPayload() {
readExternalSourcesMock.mockImplementation(() => ({
authtype: 'basic',
authtype: 'bAsIC',
username: 'a',
password: 'b',
}));
}

function setUpBearerTokenPayload() {
readExternalSourcesMock.mockImplementation(() => ({
authType: 'bearerToken',
authType: 'BeaRerToken',
bearerToken: 'a',
}));
}
Expand All @@ -174,7 +174,7 @@ function setUpIamPayloadWithScope() {

function setUpCp4dPayload() {
readExternalSourcesMock.mockImplementation(() => ({
authtype: 'cp4d',
authtype: 'cP4d',
username: 'a',
password: 'b',
authUrl: TOKEN_URL,
Expand All @@ -193,7 +193,7 @@ function setUpAuthPropsPayload() {

function setUpContainerPayload() {
readExternalSourcesMock.mockImplementation(() => ({
authType: 'container',
authType: 'conTAINer',
crTokenFilename: '/path/to/file',
iamProfileName: IAM_PROFILE_NAME,
iamProfileId: 'some-id',
Expand Down

0 comments on commit ae19adc

Please sign in to comment.