nativescript-security
Version:
Security NativeScript plugin.
75 lines • 3.29 kB
JavaScript
;
var utils = require('utils/utils');
var Security = (function () {
function Security() {
this.keychainItemServiceName = null;
this.keychainItemIdentifier = "TouchIDKey";
}
Security.prototype.available = function () {
return new Promise(function (resolve, reject) {
try {
resolve(LAContext.new().canEvaluatePolicyError(LAPolicyDeviceOwnerAuthenticationWithBiometrics, null));
}
catch (error) {
reject(error);
}
});
};
Security.prototype.verifyFingerPrint = function (args) {
var _this = this;
return new Promise(function (resolve, reject) {
try {
if (_this.keychainItemServiceName === null) {
var bundleID = utils.ios.getter(NSBundle, NSBundle.mainBundle).infoDictionary.objectForKey("CFBundleIdentifier");
_this.keychainItemServiceName = bundleID + ".TouchID";
}
if (!_this.createKeyChainEntry()) {
return;
}
var message = "Scan your finger";
if (args && args.iOSMessage) {
message = args.iOSMessage;
}
var query = NSMutableDictionary.new();
query.setObjectForKey(kSecClassGenericPassword, kSecClass);
query.setObjectForKey(_this.keychainItemIdentifier, kSecAttrAccount);
query.setObjectForKey(_this.keychainItemServiceName, kSecAttrService);
query.setObjectForKey(message, kSecUseOperationPrompt);
var res = SecItemCopyMatching(query, null);
if (res === 0) {
resolve();
}
else {
reject(res);
}
}
catch (ex) {
console.log("Error in touchid.verifyFingerprint: " + ex);
reject(ex);
}
});
};
Security.prototype.createKeyChainEntry = function () {
var attributes = NSMutableDictionary.new();
attributes.setObjectForKey(kSecClassGenericPassword, kSecClass);
attributes.setObjectForKey(this.keychainItemIdentifier, kSecAttrAccount);
attributes.setObjectForKey(this.keychainItemServiceName, kSecAttrService);
var accessControlRef = SecAccessControlCreateWithFlags(kCFAllocatorDefault, kSecAttrAccessibleWhenUnlockedThisDeviceOnly, kSecAccessControlUserPresence, null);
if (accessControlRef === null) {
console.log("Can't store identifier '" + this.keychainItemIdentifier + "' in the KeyChain");
return false;
}
else {
attributes.setObjectForKey(accessControlRef, kSecAttrAccessControl);
attributes.setObjectForKey(1, kSecUseNoAuthenticationUI);
var htmlString = NSString.stringWithString("dummy content");
var nsData = htmlString.dataUsingEncoding(NSUTF8StringEncoding);
attributes.setObjectForKey(nsData, kSecValueData);
SecItemAdd(attributes, null);
return true;
}
};
return Security;
}());
exports.Security = Security;
//# sourceMappingURL=security.ios.js.map