@nevis-security/nevis-mobile-authentication-sdk-react
Version:
React Native plugin for Nevis Mobile Authentication SDK. Supports only mobile.
154 lines (151 loc) • 6.39 kB
JavaScript
"use strict";
/**
* Copyright © 2023-2024 Nevis Security AG. All rights reserved.
*/
import uuid from 'react-native-uuid';
import { HttpOperation, HttpOperationImpl } from './HttpOperation';
import { UserInteractionPlatformOperationImpl } from '../cache/operation/UserInteractionPlatformOperation';
import { PlatformOperationCache } from '../cache/PlatformOperationCache';
import { AuthCloudApiErrorConverter } from '../error/authCloudApi/AuthCloudApiErrorConverter';
import { NativeEventListener } from '../event/NativeEventListener';
import NevisMobileAuthenticationSdkReact from '../MobileAuthenticationSdk';
import { AuthCloudApiRegistrationMessage } from '../model/messages/out/AuthCloudApiRegistrationMessage';
/**
* The object that can be used to trigger a registration operation from the response to the
* [Authentication Cloud API enroll](https://docs.nevis.net/apidoc/authcloud/#enroll-your-access-app)
* request.
*
* Usage example:
* ```ts
* class AuthenticatorSelectorImpl extends AuthenticatorSelector {
* async selectAuthenticator(
* context: AuthenticatorSelectionContext,
* handler: AuthenticatorSelectionHandler
* ): Promise<void> {
* await handler.aaid(aaid).catch(console.error);
* }
* }
*
* class BiometricUserVerifierImpl extends BiometricUserVerifier {
* async verifyBiometric(
* context: BiometricUserVerificationContext,
* handler: BiometricUserVerificationHandler
* ): Promise<void> {
* await handler
* .listenForOsCredentials(
* BiometricPromptOptions.create(
* 'Biometric authentication required',
* 'Cancel',
* 'Please identify yourself.'
* )
* )
* .catch(console.error);
* }
* }
*
* async register(
* client: MobileAuthenticationClient,
* enrollResponse: string,
* deviceInformation: DeviceInformation
* ): Promise<void> {
* await client.operations.authCloudApiRegistration
* .enrollResponse(enrollResponse)
* .deviceInformation(deviceInformation)
* .authenticatorSelector(new AuthenticatorSelectorImpl())
* .biometricUserVerifier(new BiometricUserVerifierImpl())
* .onSuccess(() => {
* // handle success
* })
* .onError((_error) => {
* // handle error
* })
* .execute();
* }
* ```
*
* The biometric, device passcode and fingerprint authenticators are enrolled at the OS level. That is why,
* if one of them must be registered, the user must authenticate through {@link BiometricUserVerifier},
* {@link DevicePasscodeUserVerifier} or {@link FingerprintUserVerifier}.
* In the case of the PIN and password, the credentials are enrolled during, so no authentication is needed.
*
* @see {@link Operations.authCloudApiRegistration}
*/
export class AuthCloudApiRegistration extends HttpOperation {}
export class AuthCloudApiRegistrationImpl extends HttpOperationImpl {
enrollResponse(enrollResponse) {
this._enrollResponse = enrollResponse;
return this;
}
appLinkUri(appLinkUri) {
this._appLinkUri = appLinkUri;
return this;
}
deviceInformation(deviceInformation) {
this._deviceInformation = deviceInformation;
return this;
}
allowClass2AndroidSensors(allowClass2AndroidSensors) {
this._allowClass2AndroidSensors = allowClass2AndroidSensors;
return this;
}
allowDevicePasscodeAsFallback(allowDevicePasscodeAsFallback) {
this._allowDevicePasscodeAsFallback = allowDevicePasscodeAsFallback;
return this;
}
invalidateOnNewOsBiometrics(invalidateOnNewOsBiometrics) {
this._invalidateOnNewOsBiometrics = invalidateOnNewOsBiometrics;
return this;
}
authenticatorSelector(authenticatorSelector) {
this._authenticatorSelector = authenticatorSelector;
return this;
}
pinEnroller(pinEnroller) {
this._pinEnroller = pinEnroller;
return this;
}
passwordEnroller(passwordEnroller) {
this._passwordEnroller = passwordEnroller;
return this;
}
biometricUserVerifier(biometricUserVerifier) {
this._biometricUserVerifier = biometricUserVerifier;
return this;
}
devicePasscodeUserVerifier(devicePasscodeUserVerifier) {
this._devicePasscodeUserVerifier = devicePasscodeUserVerifier;
return this;
}
fingerprintUserVerifier(fingerprintUserVerifier) {
this._fingerprintUserVerifier = fingerprintUserVerifier;
return this;
}
onSuccess(onSuccess) {
this._onSuccess = onSuccess;
return this;
}
onError(onError) {
this._onError = onError;
return this;
}
async execute() {
const operationId = uuid.v4();
const operation = new UserInteractionPlatformOperationImpl(operationId, undefined, this._authenticatorSelector, this._pinEnroller, this._passwordEnroller, undefined, undefined, undefined, undefined, this._biometricUserVerifier, this._devicePasscodeUserVerifier, this._fingerprintUserVerifier);
PlatformOperationCache.getInstance().put(operation);
NativeEventListener.getInstance().start(operationId);
const message = new AuthCloudApiRegistrationMessage(operationId, false, this._authenticatorSelector !== undefined, this._pinEnroller !== undefined, this._passwordEnroller !== undefined, false, false, this._biometricUserVerifier !== undefined, this._devicePasscodeUserVerifier !== undefined, this._fingerprintUserVerifier !== undefined, this._onSuccess !== undefined, this._onError !== undefined, this.httpRequestHeaders, this._enrollResponse, this._appLinkUri, this._deviceInformation, this._pinEnroller?.pinPolicy, this._allowClass2AndroidSensors, this._allowDevicePasscodeAsFallback, this._invalidateOnNewOsBiometrics);
function finish() {
NativeEventListener.getInstance().stop(operationId);
PlatformOperationCache.getInstance().delete(operationId);
}
return NevisMobileAuthenticationSdkReact.authCloudApiRegister(message).then(() => {
finish();
this._onSuccess?.();
}).catch(error => {
finish();
const authCloudApiError = new AuthCloudApiErrorConverter(error).convert();
this._onError?.(authCloudApiError);
});
}
}
//# sourceMappingURL=AuthCloudApiRegistration.js.map