nativescript-security
Version:
Security NativeScript plugin.
134 lines • 6.38 kB
JavaScript
"use strict";
var app = require('application');
var utils_1 = require('utils/utils');
var Security = (function () {
function Security() {
this.KEY_NAME = 'nativescript-security';
this.SECRET_BYTE_ARRAY = Array.create('byte', 16);
this.REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 1;
this.AUTHENTICATION_DURATION = 15;
this.title = 'Please confirm your credentials.';
this.message = 'We are doing this for your own security.';
this.KeyguardManager = android.app.KeyguardManager;
this.ActivityCompat = android.support.v4.app.ActivityCompat;
this.Manifest = android.Manifest;
this.PackageManager = android.content.pm.PackageManager;
this.KeyStore = java.security.KeyStore;
this.Cipher = javax.crypto.Cipher;
this.KeyGenerator = javax.crypto.KeyGenerator;
this.KeyProperties = android.security.keystore.KeyProperties;
this.SecretKey = javax.crypto.SecretKey;
this.KeyGenParameterSpec = android.security.keystore.KeyGenParameterSpec;
}
Security.prototype.available = function () {
return new Promise(function (resolve, reject) {
try {
var keyguardManager = utils_1.ad.getApplicationContext().getSystemService("keyguard");
if (!keyguardManager.isKeyguardSecure()) {
resolve(false);
return;
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
var fingerprintManager = utils_1.ad.getApplicationContext().getSystemService("fingerprint");
if (!fingerprintManager.isHardwareDetected()) {
reject('Device doesn\'t support fingerprint authentication');
}
else if (!fingerprintManager.hasEnrolledFingerprints()) {
reject('User hasn\'t enrolled any fingerprints to authenticate with ');
}
else {
resolve(true);
}
}
}
catch (error) {
reject(error);
}
});
};
Security.prototype.verifyFingerPrint = function (args) {
return new Promise(function (resolve, reject) {
var activity = app.android.foregroundActivity;
try {
activity.onActivityResult = function onActivityResult(requestCode, resultCode, data) {
if (requestCode === this.REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {
if (resultCode === android.app.Activity.RESULT_OK) {
resolve('Congrats! You have just been authenticated successfully!');
}
else {
reject('The last authentication attempt was cancelled.');
}
}
};
var keyguardManager = utils_1.ad.getApplicationContext().getSystemService("keyguard");
if (keyguardManager == null) {
reject('Sorry, your device does not support keyguardManager.');
}
if (keyguardManager && !keyguardManager.isKeyguardSecure()) {
reject('Secure lock screen hasn\'t been set up.\n Go to "Settings -> Security -> Screenlock" to set up a lock screen.');
}
this.createKey();
this.tryEncrypt();
}
catch (ex) {
console.log("Error in verifyFingerprint: " + ex);
reject(ex);
}
});
};
Security.prototype.createKey = function () {
try {
var keyStore = this.KeyStore.getInstance('AndroidKeyStore');
keyStore.load(null);
var keyGenerator = this.KeyGenerator.getInstance(this.KeyProperties.KEY_ALGORITHM_AES, 'AndroidKeyStore');
keyGenerator.init(new this.KeyGenParameterSpec.Builder(this.KEY_NAME, this.KeyProperties.PURPOSE_ENCRYPT | this.KeyProperties.PURPOSE_DECRYPT)
.setBlockModes([this.KeyProperties.BLOCK_MODE_CBC])
.setUserAuthenticationRequired(true)
.setUserAuthenticationValidityDurationSeconds(this.AUTHENTICATION_DURATION)
.setEncryptionPaddings([this.KeyProperties.ENCRYPTION_PADDING_PKCS7])
.build());
keyGenerator.generateKey();
}
catch (error) {
if ((error.nativeException + '').indexOf('java.security.NoSuchAlgorithmException:') > -1) {
}
console.log(error);
}
};
Security.prototype.tryEncrypt = function () {
try {
var keyStore = this.KeyStore.getInstance('AndroidKeyStore');
keyStore.load(null);
var secretKey = keyStore.getKey(this.KEY_NAME, null);
var cipher = this.Cipher.getInstance(this.KeyProperties.KEY_ALGORITHM_AES + "/" +
this.KeyProperties.BLOCK_MODE_CBC + "/" +
this.KeyProperties.ENCRYPTION_PADDING_PKCS7);
cipher.init(this.Cipher.ENCRYPT_MODE, secretKey);
cipher.doFinal(this.SECRET_BYTE_ARRAY);
return true;
}
catch (error) {
if ((error.nativeException + '').indexOf('android.security.keystore.UserNotAuthenticatedException') > -1) {
this.showAuthenticationScreen();
}
else if ((error.nativeException + '').indexOf('android.security.keystore.KeyPermanentlyInvalidatedException') > -1) {
console.log(error);
}
else {
console.log(error);
}
return false;
}
};
Security.prototype.showAuthenticationScreen = function () {
var keyguardManager = utils_1.ad.getApplicationContext().getSystemService("keyguard");
var intent = keyguardManager.createConfirmDeviceCredentialIntent(this.title, this.message);
var activity = app.android.foregroundActivity;
if (intent != null) {
activity.startActivityForResult(intent, this.REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
}
};
return Security;
}());
exports.Security = Security;
//# sourceMappingURL=security.android.js.map