forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMultifactorAuthenticationMainContext.tsx
More file actions
125 lines (98 loc) · 5.79 KB
/
Copy pathMultifactorAuthenticationMainContext.tsx
File metadata and controls
125 lines (98 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import useBiometrics from '@components/MultifactorAuthentication/biometrics/useBiometrics';
import {getScenarioConfig} from '@components/MultifactorAuthentication/config';
import type {MultifactorAuthenticationScenario} from '@components/MultifactorAuthentication/config/types';
import {MFAMachine, snapshotToState} from '@components/MultifactorAuthentication/machine';
import addMFABreadcrumb from '@components/MultifactorAuthentication/observability/breadcrumbs';
import type {CredentialsState} from '@components/MultifactorAuthentication/observability/trackMFAFlowOutcome';
import trackMFAFlowStart from '@components/MultifactorAuthentication/observability/trackMFAFlowStart';
import useSyncMfaModalNavigatorWithHistory from '@components/MultifactorAuthentication/useSyncMfaModalNavigatorWithHistory';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useInspectedMachine from '@hooks/useInspectedMachine';
import useNetwork from '@hooks/useNetwork';
import getPlatform from '@libs/getPlatform';
import CONST from '@src/CONST';
import type {ReactNode} from 'react';
import React from 'react';
import type {MultifactorAuthenticationExecuteScenarioArgs, MultifactorAuthenticationExternalAPI} from './MultifactorAuthenticationExternalApiContext';
import type {MultifactorAuthenticationInternalApi} from './MultifactorAuthenticationInternalApiContext';
import MultifactorAuthenticationExternalAPIContext from './MultifactorAuthenticationExternalApiContext';
import MultifactorAuthenticationInternalApiContext from './MultifactorAuthenticationInternalApiContext';
const MFA_STATE = CONST.MULTIFACTOR_AUTHENTICATION.MFA_STATE;
type MultifactorAuthenticationContextProviderProps = {
children: ReactNode;
};
function MultifactorAuthenticationContextProvider({children}: MultifactorAuthenticationContextProviderProps) {
const {accountID} = useCurrentUserPersonalDetails();
const {isOffline} = useNetwork();
const platform = getPlatform();
const biometrics = useBiometrics();
const [snapshot, send] = useInspectedMachine(MFAMachine);
const state = snapshotToState(snapshot);
const captureCredentialsState = async (): Promise<CredentialsState> => {
const hasLocalCredentials = await biometrics.areLocalCredentialsKnownToServer();
return {
hasServerCredentials: biometrics.serverKnownCredentialIDs.length > 0,
hasLocalCredentials,
};
};
/**
* Initiates a multifactor authentication scenario: captures start-of-flow telemetry, then sends
* INIT. The machine takes over from there - the Provider holds no flow logic.
*/
const executeScenario = async <T extends MultifactorAuthenticationScenario>(scenarioName: T, ...args: MultifactorAuthenticationExecuteScenarioArgs<T>): Promise<void> => {
const [params] = args;
// Perf short-circuit: while the modal is open or closing the machine drops INIT, so skip the
// redundant captureCredentialsState() native call + breadcrumb on the happy path.
if (state.modalState !== MFA_STATE.CLOSED) {
return;
}
// The flow captures the account at INIT and keeps it for per-account device state, so a
// session that has not hydrated yet must not start a flow keyed to the placeholder account.
if (accountID === CONST.DEFAULT_NUMBER_ID) {
addMFABreadcrumb('Flow rejected: account not initialized', {scenario: scenarioName}, 'warning');
return;
}
const startCredentialsState = await captureCredentialsState();
addMFABreadcrumb('Flow started', {
scenario: scenarioName,
hasPayload: params !== undefined && Object.keys(params).length > 0,
platform,
isOffline,
serverHasAnyCredentials: startCredentialsState.hasServerCredentials,
});
trackMFAFlowStart({scenario: scenarioName, isOffline, credentialsState: startCredentialsState});
const scenario = getScenarioConfig(scenarioName);
send({type: 'INIT', accountID, scenarioName, scenario, payload: params && Object.keys(params).length > 0 ? params : undefined});
};
const closeModal = () => send({type: 'CLOSE_MODAL'});
const notifyModalClosed = () => send({type: 'MODAL_CLOSED'});
const approveSoftPrompt = () => send({type: 'SOFT_PROMPT_APPROVED'});
const submitValidateCode = (validateCode: string) => send({type: 'VALIDATE_CODE_ENTERED', validateCode});
const resendValidateCode = () => send({type: 'RESEND_VALIDATE_CODE'});
const notifyValidateCodeChanged = () => send({type: 'VALIDATE_CODE_CHANGED'});
// There is no cancel-confirmation dialog yet, so every cancel path closes the modal directly.
const requestCancel = () => send({type: 'CLOSE_MODAL'});
const hideCancelConfirm = () => send({type: 'CLOSE_MODAL'});
const confirmCancel = () => send({type: 'CLOSE_MODAL'});
useSyncMfaModalNavigatorWithHistory(state.modalState, requestCancel);
const externalApi: MultifactorAuthenticationExternalAPI = {executeScenario};
const internalApi: MultifactorAuthenticationInternalApi = {
state,
closeModal,
notifyModalClosed,
approveSoftPrompt,
submitValidateCode,
resendValidateCode,
notifyValidateCodeChanged,
requestCancel,
hideCancelConfirm,
confirmCancel,
};
return (
<MultifactorAuthenticationExternalAPIContext.Provider value={externalApi}>
<MultifactorAuthenticationInternalApiContext.Provider value={internalApi}>{children}</MultifactorAuthenticationInternalApiContext.Provider>
</MultifactorAuthenticationExternalAPIContext.Provider>
);
}
MultifactorAuthenticationContextProvider.displayName = 'MultifactorAuthenticationContextProvider';
export default MultifactorAuthenticationContextProvider;