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

breaking change: do not throw IDX responses as errors #1205

Closed
wants to merge 6 commits into from
Closed
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: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ node_modules
/samples/generated/express-direct-auth-dynamic
.eslintrc.js
dist
/test/apps/app/public/oidc-app.js
/test/apps/app/target
3 changes: 1 addition & 2 deletions lib/idx/idxState/v1/generateIdxAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ const generateDirectFetch = function generateDirectFetch(authClient: OktaAuthInt
idxResponse.stepUp = true;
}

// Throw IDX response if request did not succeed. This behavior will be removed in version 7.0: OKTA-481844
throw idxResponse;
return idxResponse;
}
};
};
Expand Down
8 changes: 2 additions & 6 deletions lib/idx/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,10 @@ export async function register(
autoRemediate: false
});
if (!options.activationToken && enabledFeatures && !enabledFeatures.includes(IdxFeature.REGISTRATION)) {
const error = new AuthSdkError('Registration is not supported based on your current org configuration.');
throw error;
// return { status: IdxStatus.FAILURE, error } as unknown as IdxTransaction; // TODO: wny not just throw the error?
throw new AuthSdkError('Registration is not supported based on your current org configuration.');
}
if (options.activationToken && availableSteps?.some(({ name }) => name === 'identify')) {
const error = new AuthSdkError('activationToken is not supported based on your current org configuration.');
throw error;
// return { status: IdxStatus.FAILURE, error } as unknown as IdxTransaction; // TODO: wny not just throw the error?
throw new AuthSdkError('activationToken is not supported based on your current org configuration.');
}
}

Expand Down
51 changes: 22 additions & 29 deletions lib/idx/remediate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
filterValuesForRemediation,
getRemediator,
getNextStep,
handleIdxError
handleFailedResponse
} from './util';

export interface RemediateActionWithOptionalParams {
Expand Down Expand Up @@ -98,11 +98,9 @@ export async function remediate(
let optionsWithoutExecutedAction = removeActionFromOptions(options, action);

if (typeof idxResponse.actions[action] === 'function') {
try {
idxResponse = await idxResponse.actions[action](params);
idxResponse = { ...idxResponse, requestDidSucceed: true };
} catch (e) {
return handleIdxError(authClient, e, remediator);
idxResponse = await idxResponse.actions[action](params);
if (idxResponse.requestDidSucceed === false) {
return handleFailedResponse(authClient, idxResponse, remediator);
}
if (action === 'cancel') {
return { idxResponse, canceled: true };
Expand All @@ -118,14 +116,10 @@ export async function remediate(
// search for action in remediation list
const remediationAction = neededToProceed.find(({ name }) => name === action);
if (remediationAction) {
try {
idxResponse = await idxResponse.proceed(action, params);
idxResponse = { ...idxResponse, requestDidSucceed: true };
idxResponse = await idxResponse.proceed(action, params);
if (idxResponse.requestDidSucceed === false) {
return handleFailedResponse(authClient, idxResponse, remediator);
}
catch (e) {
return handleIdxError(authClient, e, remediator);
}

return remediate(authClient, idxResponse, values, optionsWithoutExecutedAction); // recursive call
}
}
Expand All @@ -139,16 +133,17 @@ export async function remediate(
}

if (!remediator) {
// With options.step, remediator is not required
if (options.step) {
values = filterValuesForRemediation(idxResponse, options.step, values); // include only requested values
try {
idxResponse = await idxResponse.proceed(options.step, values);
idxResponse = { ...idxResponse, requestDidSucceed: true };
return { idxResponse };
} catch(e) {
return handleIdxError(authClient, e);
idxResponse = await idxResponse.proceed(options.step, values);
if (idxResponse.requestDidSucceed === false) {
return handleFailedResponse(authClient, idxResponse);
}
return { idxResponse };
}

// With default flow, remediator is not required
if (flow === 'default') {
return { idxResponse };
}
Expand All @@ -170,15 +165,13 @@ export async function remediate(

const name = remediator.getName();
const data = remediator.getData();
try {
idxResponse = await idxResponse.proceed(name, data);
idxResponse = { ...idxResponse, requestDidSucceed: true };
// We may want to trim the values bag for the next remediation
// Let the remediator decide what the values should be (default to current values)
values = remediator.getValuesAfterProceed();
options = { ...options, step: undefined }; // do not re-use the step
return remediate(authClient, idxResponse, values, options); // recursive call
} catch (e) {
return handleIdxError(authClient, e, remediator);
idxResponse = await idxResponse.proceed(name, data);
if (idxResponse.requestDidSucceed === false) {
return handleFailedResponse(authClient, idxResponse, remediator);
}
// We may want to trim the values bag for the next remediation
// Let the remediator decide what the values should be (default to current values)
values = remediator.getValuesAfterProceed();
options = { ...options, step: undefined }; // do not re-use the step
return remediate(authClient, idxResponse, values, options); // recursive call
}
26 changes: 3 additions & 23 deletions lib/idx/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
Tokens,
APIError,
} from '../types';
import { IdxMessage, IdxResponse, isIdxResponse } from './types/idx-js';
import { IdxMessage, IdxResponse } from './types/idx-js';
import { getSavedTransactionMeta, saveTransactionMeta } from './transactionMeta';
import { getAvailableSteps, getEnabledFeatures, getMessagesFromResponse, isTerminalResponse } from './util';
declare interface RunData {
Expand Down Expand Up @@ -294,22 +294,6 @@ async function finalizeData(authClient, data: RunData): Promise<RunData> {
};
}

function handleError(err, data: RunData): RunData {
let { error, status, shouldClearTransaction } = data;

// current version of idx-js will throw/reject IDX responses. Handle these differently than regular errors
if (isIdxResponse(err)) {
error = err;
status = IdxStatus.FAILURE;
shouldClearTransaction = true;
} else {
// error is not an IDX response, throw it like a regular error
throw err;
}

return { ...data, error, status, shouldClearTransaction };
}

export async function run(
authClient: OktaAuthInterface,
options: RunOptions = {},
Expand All @@ -320,12 +304,8 @@ export async function run(
};

data = initializeData(authClient, data);
try {
data = await getDataFromIntrospect(authClient, data);
data = await getDataFromRemediate(authClient, data);
} catch (err) {
data = handleError(err, data);
}
data = await getDataFromIntrospect(authClient, data);
data = await getDataFromRemediate(authClient, data);
data = await finalizeData(authClient, data);

const {
Expand Down
18 changes: 6 additions & 12 deletions lib/idx/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RemediationValues, Remediator, RemediatorConstructor } from './remediat
import { GenericRemediator } from './remediators/GenericRemediator';
import { proceed } from './proceed';
import { IdxFeature, NextStep, RemediateOptions, RemediationResponse } from './types';
import { IdxMessage, IdxRemediation, IdxRemediationValue, IdxResponse, isIdxResponse } from './types/idx-js';
import { IdxMessage, IdxRemediation, IdxRemediationValue, IdxResponse } from './types/idx-js';
import { OktaAuthInterface } from '../types';

export function isTerminalResponse(idxResponse: IdxResponse) {
Expand Down Expand Up @@ -251,17 +251,11 @@ export function getNextStep(
};
}

export function handleIdxError(authClient: OktaAuthInterface, e, remediator?): RemediationResponse {
// Handle idx messages
let idxResponse = isIdxResponse(e) ? e : null;
if (!idxResponse) {
// Thrown error terminates the interaction with idx
throw e;
}
idxResponse = {
...idxResponse,
requestDidSucceed: false
};
export function handleFailedResponse(
authClient: OktaAuthInterface,
idxResponse: IdxResponse,
remediator?: Remediator
): RemediationResponse {
const terminal = isTerminalResponse(idxResponse);
const messages = getMessagesFromResponse(idxResponse);
if (terminal) {
Expand Down
4 changes: 2 additions & 2 deletions scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export TEST_RESULT_FILE_DIR="${REPO}/build2"

if ! yarn tsd; then
echo "tsd failed! Exiting..."
exit ${TEST_FAILURE}
exit ${PUBLISH_TYPE_AND_RESULT_DIR_BUT_ALWAYS_FAIL}
fi

if ! yarn lint:report; then
echo "lint failed! Exiting..."
exit ${TEST_FAILURE}
exit ${PUBLISH_TYPE_AND_RESULT_DIR_BUT_ALWAYS_FAIL}
fi

mkdir -p ${TEST_RESULT_FILE_DIR}
Expand Down
12 changes: 6 additions & 6 deletions scripts/unit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ source ${OKTA_HOME}/${REPO}/scripts/setup.sh "v14.18.0"

export TEST_SUITE_TYPE="junit"
export TEST_RESULT_FILE_DIR="${REPO}/build2/reports/unit"
echo ${TEST_SUITE_TYPE} > ${TEST_SUITE_TYPE_FILE}
echo ${TEST_RESULT_FILE_DIR} > ${TEST_RESULT_FILE_DIR_FILE}

if ! yarn test:unit; then
echo "unit failed! Exiting..."
exit ${TEST_FAILURE}
exit ${PUBLISH_TYPE_AND_RESULT_DIR_BUT_ALWAYS_FAIL}
fi

if ! yarn test:bundle:esm:browser; then
echo "validate ESM browser bundle failed! Exiting..."
exit ${TEST_FAILURE}
exit ${PUBLISH_TYPE_AND_RESULT_DIR_BUT_ALWAYS_FAIL}
fi

if ! yarn test:bundle:esm:node; then
echo "validate ESM node bundle failed! Exiting..."
exit ${TEST_FAILURE}
exit ${PUBLISH_TYPE_AND_RESULT_DIR_BUT_ALWAYS_FAIL}
fi

if ! yarn test:bundle:cjs; then
echo "validate cjs bundle failed! Exiting..."
exit ${TEST_FAILURE}
exit ${PUBLISH_TYPE_AND_RESULT_DIR_BUT_ALWAYS_FAIL}
fi

echo ${TEST_SUITE_TYPE} > ${TEST_SUITE_TYPE_FILE}
echo ${TEST_RESULT_FILE_DIR} > ${TEST_RESULT_FILE_DIR_FILE}
exit ${PUBLISH_TYPE_AND_RESULT_DIR}
Loading