-
Notifications
You must be signed in to change notification settings - Fork 1
/
SelectAccountViewModel.ts
176 lines (166 loc) · 5.91 KB
/
SelectAccountViewModel.ts
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Copyright © 2023 Nevis Security AG. All rights reserved.
*/
import {
AccountSelectionHandler,
AuthorizationProvider,
} from '@nevis-security/nevis-mobile-authentication-sdk-react';
import { useNavigation } from '@react-navigation/native';
import type { NativeStackNavigationProp } from '@react-navigation/native-stack';
import type { RootStackParamList } from './RootStackParamList';
import { AppErrorAuthorizationProviderNotFound, AppErrorUnknownError } from '../error/AppError';
import { ErrorHandler } from '../error/ErrorHandler';
import { OperationType } from '../model/OperationType';
import {
AuthenticatorSelectorImpl,
AuthenticatorSelectorOperation,
} from '../userInteraction/AuthenticatorSelectorImpl';
import { BiometricUserVerifierImpl } from '../userInteraction/BiometricUserVerifierImpl';
import { DevicePasscodeUserVerifierImpl } from '../userInteraction/DevicePasscodeUserVerifierImpl';
import { FingerprintUserVerifierImpl } from '../userInteraction/FingerprintUserVerifierImpl';
import { PasswordChangerImpl } from '../userInteraction/PasswordChangerImpl';
import { PasswordUserVerifierImpl } from '../userInteraction/PasswordUserVerifierImpl';
import { PinChangerImpl } from '../userInteraction/PinChangerImpl';
import { PinUserVerifierImpl } from '../userInteraction/PinUserVerifierImpl';
import { AuthorizationUtils } from '../utility/AuthorizationUtils';
import { ClientProvider } from '../utility/ClientProvider';
import * as RootNavigation from '../utility/RootNavigation';
const useSelectAccountViewModel = () => {
const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();
async function select(
username: string,
operation: OperationType,
transactionConfirmationData?: string,
handler?: AccountSelectionHandler
) {
if (transactionConfirmationData) {
// Transaction confirmation data is received from the SDK
// Show it to the user for confirmation or cancellation
// The AccountSelectionHandler will be invoked or cancelled there.
RootNavigation.navigate('TransactionConfirmation', {
transactionConfirmationData: transactionConfirmationData,
selectedUsername: username,
accountSelectionHandler: handler,
});
return;
}
switch (operation) {
case OperationType.authentication:
case OperationType.deregistration:
await authenticate(username, operation);
break;
case OperationType.pinChange:
await pinChange(username, operation);
break;
case OperationType.passwordChange:
await passwordChange(username, operation);
break;
default:
await handler?.username(username).catch((error) => {
ErrorHandler.handle.bind(
null,
operation,
new AppErrorUnknownError('Account selection failed', error.message)
);
});
}
}
async function authenticate(username: string, operation: OperationType) {
const client = ClientProvider.getInstance().client;
client?.operations.authentication
.username(username)
.authenticatorSelector(
new AuthenticatorSelectorImpl(AuthenticatorSelectorOperation.authentication)
)
.pinUserVerifier(new PinUserVerifierImpl())
.passwordUserVerifier(new PasswordUserVerifierImpl())
.biometricUserVerifier(new BiometricUserVerifierImpl())
.devicePasscodeUserVerifier(new DevicePasscodeUserVerifierImpl())
.fingerprintUserVerifier(new FingerprintUserVerifierImpl())
.onSuccess(async (authorizationProvider?: AuthorizationProvider) => {
console.log('In-Band authentication succeeded.');
AuthorizationUtils.printAuthorizationInfo(authorizationProvider);
switch (operation) {
case OperationType.authentication:
navigation.navigate('Result', {
operation: operation,
});
break;
case OperationType.deregistration:
if (!authorizationProvider) {
return ErrorHandler.handle(
operation,
new AppErrorAuthorizationProviderNotFound(
'Missing authorization provider for deregistration.'
)
);
}
await deregister(username, operation, authorizationProvider);
break;
default:
ErrorHandler.handle(
operation,
new AppErrorUnknownError('Unsupported operation type.')
);
}
})
.onError((error) => {
AuthorizationUtils.printSessionInfo(error.sessionProvider);
ErrorHandler.handle(operation, error);
})
.execute()
.catch(ErrorHandler.handle.bind(null, operation));
}
async function deregister(
username: string,
operation: OperationType,
authorizationProvider: AuthorizationProvider
) {
const client = ClientProvider.getInstance().client;
client?.operations.deregistration
.username(username)
.authorizationProvider(authorizationProvider)
.onSuccess(() => {
console.log('Deregistration succeeded.');
navigation.navigate('Result', {
operation: operation,
});
})
.onError(ErrorHandler.handle.bind(null, operation))
.execute();
}
async function pinChange(username: string, operation: OperationType) {
const client = ClientProvider.getInstance().client;
client?.operations.pinChange
.username(username)
.pinChanger(new PinChangerImpl())
.onSuccess(() => {
console.log('PIN Change succeeded.');
navigation.navigate('Result', {
operation: operation,
});
})
.onError(ErrorHandler.handle.bind(null, OperationType.pinChange))
.execute()
.catch(ErrorHandler.handle.bind(null, operation));
}
async function passwordChange(username: string, operation: OperationType) {
const client = ClientProvider.getInstance().client;
client?.operations.passwordChange
.username(username)
.passwordChanger(new PasswordChangerImpl())
.onSuccess(() => {
console.log('Password Change succeeded.');
navigation.navigate('Result', {
operation: operation,
});
})
.onError(ErrorHandler.handle.bind(null, OperationType.passwordChange))
.execute()
.catch(ErrorHandler.handle.bind(null, operation));
}
return {
select,
};
};
export default useSelectAccountViewModel;