UNPKG

react-native-biometrics-authentication

Version:

React Native biometric functionality for signing and encryption with optional fallback passcode option in IOS.

275 lines 14.2 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; import { NativeModules } from "react-native"; var bridge = NativeModules.ReactNativeBiometrics; /** * Enum for touch id sensor type */ export var TouchID = "TouchID"; /** * Enum for face id sensor type */ export var FaceID = "FaceID"; /** * Enum for generic biometrics (this is the only value available on android) */ export var Biometrics = "Biometrics"; /** * List of Biometrices Error Comes. */ export var BIOMETRY_ERROR_TYPE = { limitExceed: "Too many attempts. Try again later.", sensorDisable: "Too many attempts. Fingerprint sensor disabled.", signatureInvalidate: "Error generating signature", fingerPrintDisabled: "Your Finger Print is Locked.", }; /** * List of Biometrices Error Comes during enrollement. */ export var BIOMETRY_ENROLLED_ERROR_TYPE = { biometricNotEnroll: "BIOMETRIC_ERROR_NONE_ENROLLED", biometricNotSupport: "BIOMETRIC_ERROR_NO_HARDWARE", biometricHWUnavailable: "BIOMETRIC_ERROR_HW_UNAVAILABLE", biometricPasscodeNotEnrolled: 'Error Domain=com.apple.LocalAuthentication Code=-5 "Passcode not set." UserInfo={NSDebugDescription=Passcode not set., NSLocalizedDescription=Passcode is not set.}', biometricNotEnrollIOS: 'Error Domain=com.apple.LocalAuthentication Code=-7 "No identities are enrolled." UserInfo={NSDebugDescription=No identities are enrolled., NSLocalizedDescription=Biometry is not enrolled.}', biometricDeviceLockOut: 'Error Domain=com.apple.LocalAuthentication Code=-8 "Biometry is locked out." UserInfo={NSDebugDescription=Biometry is locked out., NSLocalizedDescription=Biometry is locked out.}', }; export var BiometryTypes = { TouchID: TouchID, FaceID: FaceID, Biometrics: Biometrics, }; export var ReactNativeBiometricsLegacy; (function (ReactNativeBiometricsLegacy) { /** * Returns promise that resolves to an object with object.biometryType = Biometrics | TouchID | FaceID * @returns {Promise<Object>} Promise that resolves to an object with details about biometrics available */ function isSensorAvailable() { return new ReactNativeBiometrics().isSensorAvailable(); } ReactNativeBiometricsLegacy.isSensorAvailable = isSensorAvailable; /** * Creates a public private key pair,returns promise that resolves to * an object with object.publicKey, which is the public key of the newly generated key pair * @returns {Promise<Object>} Promise that resolves to object with details about the newly generated public key */ function createKeys() { return new ReactNativeBiometrics().createKeys(); } ReactNativeBiometricsLegacy.createKeys = createKeys; /** * Returns promise that resolves to an object with object.keysExists = true | false * indicating if the keys were found to exist or not * @returns {Promise<Object>} Promise that resolves to object with details about the existence of keys */ function biometricKeysExist() { return new ReactNativeBiometrics().biometricKeysExist(); } ReactNativeBiometricsLegacy.biometricKeysExist = biometricKeysExist; /** * Returns promise that resolves to an object with true | false * indicating if the keys were properly deleted * @returns {Promise<Object>} Promise that resolves to an object with details about the deletion */ function deleteKeys() { return new ReactNativeBiometrics().deleteKeys(); } ReactNativeBiometricsLegacy.deleteKeys = deleteKeys; /** * Prompts user with biometrics dialog using the passed in prompt message and * returns promise that resolves to an object with object.signature, * which is cryptographic signature of the payload * @param {Object} createSignatureOptions * @param {string} createSignatureOptions.promptMessage * @param {string} createSignatureOptions.payload * @returns {Promise<Object>} Promise that resolves to an object cryptographic signature details */ function createSignature(createSignatureOptions) { return new ReactNativeBiometrics().createSignature(createSignatureOptions); } ReactNativeBiometricsLegacy.createSignature = createSignature; /** * Prompts user with biometrics dialog using the passed in prompt message and * returns promise that resolves to an object with object.success = true if the user passes, * object.success = false if the user cancels, and rejects if anything fails * @param {Object} simplePromptOptions * @param {string} simplePromptOptions.promptMessage * @param {string} simplePromptOptions.fallbackPromptMessage * @returns {Promise<Object>} Promise that resolves an object with details about the biometrics result */ function simplePrompt(simplePromptOptions) { return new ReactNativeBiometrics().simplePrompt(simplePromptOptions); } ReactNativeBiometricsLegacy.simplePrompt = simplePrompt; })(ReactNativeBiometricsLegacy || (ReactNativeBiometricsLegacy = {})); var ReactNativeBiometrics = /** @class */ (function () { /** * @param {Object} rnBiometricsOptions * @param {boolean} rnBiometricsOptions.allowDeviceCredentials */ function ReactNativeBiometrics(rnBiometricsOptions) { var _a; this.allowDeviceCredentials = false; var allowDeviceCredentials = (_a = rnBiometricsOptions === null || rnBiometricsOptions === void 0 ? void 0 : rnBiometricsOptions.allowDeviceCredentials) !== null && _a !== void 0 ? _a : false; this.allowDeviceCredentials = allowDeviceCredentials; } /** * Returns promise that resolves to an object with object.biometryType = Biometrics | TouchID | FaceID * @returns {Promise<Object>} Promise that resolves to an object with details about biometrics available */ ReactNativeBiometrics.prototype.isSensorAvailable = function () { return bridge.isSensorAvailable({ allowDeviceCredentials: this.allowDeviceCredentials, }); }; /** * Creates a public private key pair,returns promise that resolves to * an object with object.publicKey, which is the public key of the newly generated key pair * @returns {Promise<Object>} Promise that resolves to object with details about the newly generated public key */ ReactNativeBiometrics.prototype.createKeys = function () { return bridge.createKeys({ allowDeviceCredentials: this.allowDeviceCredentials, }); }; /** * Returns promise that resolves to an object with object.keysExists = true | false * indicating if the keys were found to exist or not * @returns {Promise<Object>} Promise that resolves to object with details aobut the existence of keys */ ReactNativeBiometrics.prototype.biometricKeysExist = function () { return bridge.biometricKeysExist(); }; /** * Returns promise that resolves to an object with true | false * indicating if the keys were properly deleted * @returns {Promise<Object>} Promise that resolves to an object with details about the deletion */ ReactNativeBiometrics.prototype.deleteKeys = function () { return bridge.deleteKeys(); }; /** * Prompts user with biometrics dialog using the passed in prompt message and * returns promise that resolves to an object with object.signature, * which is cryptographic signature of the payload * @param {Object} createSignatureOptions * @param {string} createSignatureOptions.promptMessage * @param {string} createSignatureOptions.payload * @returns {Promise<Object>} Promise that resolves to an object cryptographic signature details */ ReactNativeBiometrics.prototype.createSignature = function (createSignatureOptions) { var _a; createSignatureOptions.cancelButtonText = (_a = createSignatureOptions.cancelButtonText) !== null && _a !== void 0 ? _a : "Cancel"; return bridge.createSignature(__assign({ allowDeviceCredentials: this.allowDeviceCredentials }, createSignatureOptions)); }; /** * Prompts user with biometrics dialog using the passed in prompt message and * returns promise that resolves to an object with object.success = true if the user passes, * object.success = false if the user cancels, and rejects if anything fails * @param {Object} simplePromptOptions * @param {string} simplePromptOptions.promptMessage * @param {string} simplePromptOptions.fallbackPromptMessage * @returns {Promise<Object>} Promise that resolves an object with details about the biometrics result */ ReactNativeBiometrics.prototype.simplePrompt = function (simplePromptOptions) { var _a, _b; simplePromptOptions.cancelButtonText = (_a = simplePromptOptions.cancelButtonText) !== null && _a !== void 0 ? _a : "Cancel"; simplePromptOptions.fallbackPromptMessage = (_b = simplePromptOptions.fallbackPromptMessage) !== null && _b !== void 0 ? _b : "Use Passcode"; return bridge.simplePrompt(__assign({ allowDeviceCredentials: this.allowDeviceCredentials }, simplePromptOptions)); }; ReactNativeBiometrics.prototype.generateKey = function () { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.createKeys()]; case 1: response = _a.sent(); return [2 /*return*/, response.publicKey]; } }); }); }; ReactNativeBiometrics.prototype._addDeviceAuthenticationUtil = function (authenticationOptions) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.simplePrompt({ promptMessage: authenticationOptions.promptMessage, fallbackPromptMessage: authenticationOptions.fallbackPromptMessage, cancelButtonText: authenticationOptions.cancelButtonText, })]; case 1: response = _a.sent(); if (response.success) { return [2 /*return*/, authenticationOptions.successCallback({ publicKey: authenticationOptions.encryption ? this.generateKey() : "", })]; } else if (response.error) { return [2 /*return*/, authenticationOptions.errorCallback({ error: response.error, })]; } return [2 /*return*/]; } }); }); }; ReactNativeBiometrics.prototype.addDeviceAuthentication = function (authenticationOptions) { return this._addDeviceAuthenticationUtil(authenticationOptions); }; return ReactNativeBiometrics; }()); export default ReactNativeBiometrics; //# sourceMappingURL=index.js.map