-
Notifications
You must be signed in to change notification settings - Fork 1
/
OutOfBandOperationHandler.ts
123 lines (115 loc) · 4.52 KB
/
OutOfBandOperationHandler.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
/**
* Copyright © 2023 Nevis Security AG. All rights reserved.
*/
import {
AuthorizationProvider,
type MobileAuthenticationClient,
OutOfBandAuthentication,
OutOfBandPayload,
OutOfBandRegistration,
} from '@nevis-security/nevis-mobile-authentication-sdk-react';
import { AccountSelectorImpl } from './AccountSelectorImpl';
import {
AuthenticatorSelectorImpl,
AuthenticatorSelectorOperation,
} from './AuthenticatorSelectorImpl';
import { BiometricUserVerifierImpl } from './BiometricUserVerifierImpl';
import { DevicePasscodeUserVerifierImpl } from './DevicePasscodeUserVerifierImpl';
import { FingerprintUserVerifierImpl } from './FingerprintUserVerifierImpl';
import { PasswordEnrollerImpl } from './PasswordEnrollerImpl';
import { PasswordUserVerifierImpl } from './PasswordUserVerifierImpl';
import { PinEnrollerImpl } from './PinEnrollerImpl';
import { PinUserVerifierImpl } from './PinUserVerifierImpl';
import { AppErrorPayloadDecodeError, AppErrorQrCodeError } from '../error/AppError';
import { ErrorHandler } from '../error/ErrorHandler';
import { OperationType } from '../model/OperationType';
import { AuthorizationUtils } from '../utility/AuthorizationUtils';
import { ClientProvider } from '../utility/ClientProvider';
import { DeviceInformationUtils } from '../utility/DeviceInformationUtils';
import * as RootNavigation from '../utility/RootNavigation';
async function handleRegistration(
registration: OutOfBandRegistration,
client?: MobileAuthenticationClient
) {
const deviceInformation =
(await client?.localData.deviceInformation()) ?? DeviceInformationUtils.create();
await registration
.deviceInformation(deviceInformation)
.authenticatorSelector(
new AuthenticatorSelectorImpl(AuthenticatorSelectorOperation.registration)
)
.pinEnroller(new PinEnrollerImpl())
.passwordEnroller(new PasswordEnrollerImpl())
.biometricUserVerifier(new BiometricUserVerifierImpl())
.devicePasscodeUserVerifier(new DevicePasscodeUserVerifierImpl())
.fingerprintUserVerifier(new FingerprintUserVerifierImpl())
.onSuccess(() => {
console.log('Out-of-Band registration succeeded.');
RootNavigation.navigate('Result', {
operation: OperationType.registration,
});
})
.onError(ErrorHandler.handle.bind(null, OperationType.registration))
.execute();
}
async function handleAuthentication(authentication: OutOfBandAuthentication) {
await authentication
.accountSelector(new AccountSelectorImpl())
.authenticatorSelector(
new AuthenticatorSelectorImpl(AuthenticatorSelectorOperation.authentication)
)
.pinUserVerifier(new PinUserVerifierImpl())
.passwordUserVerifier(new PasswordUserVerifierImpl())
.biometricUserVerifier(new BiometricUserVerifierImpl())
.devicePasscodeUserVerifier(new DevicePasscodeUserVerifierImpl())
.fingerprintUserVerifier(new FingerprintUserVerifierImpl())
.onSuccess((authorizationProvider?: AuthorizationProvider) => {
console.log('Out-of-Band authentication succeeded.');
AuthorizationUtils.printAuthorizationInfo(authorizationProvider);
RootNavigation.navigate('Result', {
operation: OperationType.authentication,
});
})
.onError(ErrorHandler.handle.bind(null, OperationType.authentication))
.execute();
}
async function handleOutOfBandPayload(
payload: OutOfBandPayload,
client?: MobileAuthenticationClient
) {
client?.operations.outOfBandOperation
.payload(payload)
.onRegistration(async (registration) => {
await handleRegistration(registration, client).catch(
ErrorHandler.handle.bind(null, OperationType.registration)
);
})
.onAuthentication(async (authentication) => {
await handleAuthentication(authentication).catch(
ErrorHandler.handle.bind(null, OperationType.authentication)
);
})
.onError(ErrorHandler.handle.bind(null, OperationType.unknown))
.execute()
.catch(ErrorHandler.handle.bind(null, OperationType.unknown));
}
export async function decodePayload(base64UrlEncoded: string) {
if (base64UrlEncoded === '') {
return ErrorHandler.handle(OperationType.payloadDecode, new AppErrorQrCodeError());
}
const client = ClientProvider.getInstance().client;
await client?.operations.outOfBandPayloadDecode
.base64UrlEncoded(base64UrlEncoded)
.onSuccess(async (payload) => {
if (!payload) {
return ErrorHandler.handle(
OperationType.payloadDecode,
new AppErrorPayloadDecodeError('No payload is returned by the SDK.')
);
}
console.log('Out-of-Band payload decode succeeded.');
await handleOutOfBandPayload(payload, client);
})
.onError(ErrorHandler.handle.bind(null, OperationType.payloadDecode))
.execute();
}