UNPKG

@nevis-security/nevis-mobile-authentication-sdk-react

Version:

React Native plugin for Nevis Mobile Authentication SDK. Supports only mobile.

95 lines (92 loc) 3.58 kB
"use strict"; /** * Copyright © 2023-2024 Nevis Security AG. All rights reserved. */ import uuid from 'react-native-uuid'; import { OutOfBandPlatformOperation } from '../../cache/operation/OutOfBandPlatformOperation'; import { PlatformOperationCache } from '../../cache/PlatformOperationCache'; import { OutOfBandOperationErrorConverter } from '../../error/outOfBand/operation/OutOfBandOperationErrorConverter'; import NevisMobileAuthenticationSdkReact from '../../MobileAuthenticationSdk'; import { OperationTypeMessage } from '../../model/messages/in/OperationTypeMessage'; import { OutOfBandOperationMessage } from '../../model/messages/out/OutOfBandOperationMessage'; import { HttpOperation, HttpOperationImpl } from '../HttpOperation'; /** * The operation managing an {@link OutOfBandPayload}. * * An {@link OutOfBandPayload} can be provided through different means: * - a push notification, * - a QR code or * - an application link. * * This operation will process the payload, decrypt it if needed and send it to the server. If the * payload is successfully handled by the server, then the SDK will identify whether the operation * associated with the payload is a registration or an authentication. Depending on that the * {@link onRegistration} or the {@link onAuthentication} will be invoked. * * Usage example: * ```ts * [...] * async authenticateUsingOutOfBandPayload( * client: MobileAuthenticationClient, * payload: OutOfBandPayload * ): Promise<void> { * await client.operations.outOfBandOperation * .payload(payload) * .onRegistration((registration) => { * // handle registration * }) * .onAuthentication((authentication) => { * // handle authentication * }) * .onError((_error) => { * // handle out-of-band error * }) * .execute(); * } * [...] * ``` * * @see * - {@link OutOfBandPayload} * - {@link OutOfBandRegistration} * - {@link OutOfBandAuthentication} */ export class OutOfBandOperation extends HttpOperation {} export class OutOfBandOperationImpl extends HttpOperationImpl { payload(payload) { this._payload = payload; return this; } onRegistration(onRegistration) { this._onRegistration = onRegistration; return this; } onAuthentication(onAuthentication) { this._onAuthentication = onAuthentication; return this; } onError(onError) { this._onError = onError; return this; } async execute() { const operationId = uuid.v4(); const subOperationId = uuid.v4(); const operation = new OutOfBandPlatformOperation(operationId, subOperationId, this._onRegistration, this._onAuthentication); PlatformOperationCache.getInstance().put(operation); const message = new OutOfBandOperationMessage(operationId, subOperationId, false, this.onError !== undefined, this.httpRequestHeaders, this._payload, this._onRegistration !== undefined, this._onAuthentication !== undefined); function finish() { PlatformOperationCache.getInstance().delete(operationId); } return NevisMobileAuthenticationSdkReact.oobOperation(message).then(json => { const message = OperationTypeMessage.fromJson(json); operation.selectOperation(message.operationType); finish(); }).catch(error => { finish(); const operationError = new OutOfBandOperationErrorConverter(error).convert(); this._onError?.(operationError); }); } } //# sourceMappingURL=OutOfBandOperation.js.map