Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

select-enrollment-channel now accepts protocol-defined inputs #1263

Merged
merged 8 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## 6.7.4

### Fixes

- [#1263](https://github.com/okta/okta-auth-js/pull/1263) (IDX) `select-enrollment-channel` remediation now accepts protocol defined inputs, as well as conveniences
- [#1262](https://github.com/okta/okta-auth-js/pull/1262) Freezes `broadcast-channel` version at `4.13.0`, `4.14.0` requires node 14+

## 6.7.3
Expand Down
21 changes: 18 additions & 3 deletions lib/idx/remediators/SelectEnrollmentChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import { Remediator, RemediationValues } from './Base/Remediator';
import { IdxRemediationValueForm, IdxOption, IdxRemediationValue, IdxContext } from '../types/idx-js';
import { Authenticator } from '../types';
import { getAuthenticatorFromRemediation } from './util';
import { OktaAuthIdxInterface } from '../../types';

Expand All @@ -25,7 +26,18 @@ export class SelectEnrollmentChannel extends Remediator<SelectEnrollmentChannelV
static remediationName = 'select-enrollment-channel';

canRemediate() {
return Boolean(this.values.channel);
if (this.values.channel) {
return true;
}

if (this.values.authenticator) {
const { id, channel } = this.values.authenticator as Authenticator;
if (!!id && !!channel) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

|| instead of &&?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both the channel and id are required

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm, I miss read !! to ! ...

return true;
}
}

return false;
}

getNextStep(authClient: OktaAuthIdxInterface, context: IdxContext) {
Expand All @@ -52,15 +64,18 @@ export class SelectEnrollmentChannel extends Remediator<SelectEnrollmentChannelV
return {
authenticator: {
id: remediationValue.form.value[0].value,
channel: this.values.channel,
channel: (this.values.authenticator as Authenticator)?.channel || this.values.channel,
},
stateHandle: this.values.stateHandle,

};
}

getValuesAfterProceed(): SelectEnrollmentChannelValues {
let trimmedValues = Object.keys(this.values).filter(valueKey => valueKey !== 'channel');
this.values = super.getValuesAfterProceed();
delete this.values.authenticators; // required to prevent infinite loops from auto-remediating via values
const filterKey = this.values.channel ? 'channel' : 'authenticator';
let trimmedValues = Object.keys(this.values).filter(valueKey => valueKey !== filterKey);
return trimmedValues.reduce((values, valueKey) => ({...values, [valueKey]: this.values[valueKey]}), {});
}
}
1 change: 1 addition & 0 deletions lib/idx/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export type Authenticator = {
key?: string;
methodType?: string;
phoneNumber?: string;
channel?: string;
};

export function isAuthenticator(obj: any): obj is Authenticator {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": true,
"name": "@okta/okta-auth-js",
"description": "The Okta Auth SDK",
"version": "6.7.3",
"version": "6.7.4",
"homepage": "https://github.com/okta/okta-auth-js",
"license": "Apache-2.0",
"main": "build/cjs/index.js",
Expand Down
69 changes: 68 additions & 1 deletion test/spec/idx/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1967,7 +1967,7 @@ describe('idx/authenticate', () => {

});

it('can get Okta Verify link via SMS', async () => {
it('can get Okta Verify link via SMS (proceed via channel)', async () => {
const {
authClient,
selectAuthenticatorResponse,
Expand Down Expand Up @@ -2027,6 +2027,73 @@ describe('idx/authenticate', () => {

expect(enrollmentChannelDataSmsResponse.proceed).toHaveBeenCalled();
});

it('can get Okta Verify link via SMS (proceed via authenticator)', async () => {
const {
authClient,
selectAuthenticatorResponse,
enrollPollResponse,
enrollmentChannelDataSmsResponse,
successResponse
} = testContext;

chainResponses([
selectAuthenticatorResponse,
enrollPollResponse,
enrollmentChannelDataSmsResponse,
successResponse
]);

jest.spyOn(mocked.introspect, 'introspect')
.mockResolvedValueOnce(selectAuthenticatorResponse)
.mockResolvedValueOnce(enrollPollResponse)
.mockResolvedValueOnce(enrollPollResponse) // submit enrollment channel
.mockResolvedValueOnce(enrollmentChannelDataSmsResponse);

jest.spyOn(enrollPollResponse, 'proceed');
jest.spyOn(enrollmentChannelDataSmsResponse, 'proceed');

await authenticate(authClient, {
authenticator: AuthenticatorKey.OKTA_VERIFY
});
let res = await proceed(authClient, {
step: 'select-enrollment-channel'
});
const { options } = res.nextStep!;
expect(options).toContainEqual({
label: 'SMS',
value: 'sms'
});

res = await proceed(authClient, {
authenticator: {
id: 'string',
channel: 'phoneNumber'
}
});
const { inputs } = res.nextStep!;

expect(enrollPollResponse.proceed).toHaveBeenCalledWith(
'select-enrollment-channel',
expect.objectContaining({ authenticator: expect.objectContaining({ channel: 'phoneNumber' }) })
);

expect(inputs).toContainEqual({
name: 'phoneNumber',
label: 'Phone Number',
required: true,
type: 'string',
});

await proceed(authClient, {
phoneNumber: '+1234'
});

expect(enrollmentChannelDataSmsResponse.proceed).toHaveBeenCalledWith(
'enrollment-channel-data',
expect.objectContaining({ phoneNumber: '+1234' })
);
});
});
});

Expand Down