Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sign In / Confirm Sign In With MFA_SETUP
Browse files Browse the repository at this point in the history
jjarvisp committed Aug 27, 2024
1 parent df6cba9 commit a852083
Showing 8 changed files with 306 additions and 68 deletions.
6 changes: 4 additions & 2 deletions packages/auth/src/providers/cognito/apis/confirmSignIn.ts
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ import {
} from '../utils/clients/CognitoIdentityProvider/types';
import { tokenOrchestrator } from '../tokenProvider';
import { dispatchSignedInHubEvent } from '../utils/dispatchSignedInHubEvent';
import { resetMfaSetupState } from '../utils/mfaSetupStore';

/**
* Continues or completes the sign in process when required by the initial call to `signIn`.
@@ -71,8 +72,8 @@ export async function confirmSignIn(
throw new AuthError({
name: AuthErrorCodes.SignInException,
message: `
An error occurred during the sign in process.
An error occurred during the sign in process.
This most likely occurred due to:
1. signIn was not called before confirmSignIn.
2. signIn threw an exception.
@@ -110,6 +111,7 @@ export async function confirmSignIn(

if (AuthenticationResult) {
cleanActiveSignInState();
resetMfaSetupState();
await cacheCognitoTokens({
username,
...AuthenticationResult,
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ import {
} from '../utils/clients/CognitoIdentityProvider/types';
import { tokenOrchestrator } from '../tokenProvider';
import { dispatchSignedInHubEvent } from '../utils/dispatchSignedInHubEvent';
import { resetMfaSetupState } from '../utils/mfaSetupStore';

/**
* Signs a user in using a custom authentication flow with SRP
@@ -67,6 +68,7 @@ export async function signInWithCustomSRPAuth(
);

try {
resetMfaSetupState();
const {
ChallengeName: handledChallengeName,
ChallengeParameters: handledChallengeParameters,
2 changes: 2 additions & 0 deletions packages/auth/src/providers/cognito/apis/signInWithSRP.ts
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ import {
import { cacheCognitoTokens } from '../tokenProvider/cacheTokens';
import { tokenOrchestrator } from '../tokenProvider';
import { dispatchSignedInHubEvent } from '../utils/dispatchSignedInHubEvent';
import { resetMfaSetupState } from '../utils/mfaSetupStore';

/**
* Signs a user in
@@ -67,6 +68,7 @@ export async function signInWithSRP(
);

try {
resetMfaSetupState();
const {
ChallengeName: handledChallengeName,
ChallengeParameters: handledChallengeParameters,
77 changes: 77 additions & 0 deletions packages/auth/src/providers/cognito/utils/mfaSetupStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { AuthMFAType } from '../../../types';

import { Reducer, Store } from './types';

type MfaSetupInitial = undefined;

interface MfaSetupSelectionRequired {
status: 'IN_PROGRESS';
options: AuthMFAType[];
}
interface MfaSetupSelectionComplete {
status: 'COMPLETE';
value: AuthMFAType;
options: AuthMFAType[];
}

type MfaSetupState =
| MfaSetupInitial
| MfaSetupSelectionRequired
| MfaSetupSelectionComplete;

type MfaSetupAction =
| { type: 'RESET' }
| { type: 'IN_PROGRESS'; value: AuthMFAType[] }
| { type: 'COMPLETE'; value: AuthMFAType }
| { type: 'AUTO'; value: Omit<MfaSetupSelectionComplete, 'status'> };

const mfaSetupReducer: Reducer<MfaSetupState, MfaSetupAction> = (
state,
action,
) => {
if (action.type === 'RESET') {
return;
}
if (action.type === 'IN_PROGRESS') {
return {
status: 'IN_PROGRESS',
options: action.value,
};
}
if (state?.status === 'IN_PROGRESS' && action.type === 'COMPLETE') {
return {
...state,
status: 'COMPLETE',
value: action.value,
};
}
if (action.type === 'AUTO') {
return {
status: 'COMPLETE',
options: action.value.options,
value: action.value.value,
};
}

return state;
};

const createStore: Store<MfaSetupState, MfaSetupAction> = reducer => {
let currentState: MfaSetupState;

return {
getState: () => currentState,
dispatch: action => {
currentState = reducer(currentState, action);
},
};
};

export const mfaSetupStore = createStore(mfaSetupReducer);

export const resetMfaSetupState = () => {
mfaSetupStore.dispatch({ type: 'RESET' });
};
Loading

0 comments on commit a852083

Please sign in to comment.