2017-03-20 16:38:14 -04:00
import { Injectable } from '@angular/core' ;
2017-12-28 07:28:44 -05:00
import { Cordova , IonicNativePlugin , Plugin } from '@ionic-native/core' ;
2016-12-06 08:33:03 -05:00
2017-03-20 16:38:14 -04:00
export interface AFAAuthOptions {
2016-12-06 08:33:03 -05:00
/**
2016-12-27 06:57:54 -06:00
* Required
2016-12-06 08:33:03 -05:00
* Used as the alias for your key in the Android Key Store.
*/
clientId : string ;
/**
2016-12-27 06:57:54 -06:00
* Used to create credential string for encrypted token and as alias to retrieve the cipher.
2016-12-06 08:33:03 -05:00
*/
2016-12-27 06:57:54 -06:00
username? : string ;
2016-12-06 08:33:03 -05:00
2016-12-27 06:57:54 -06:00
/**
* Used to create credential string for encrypted token
*/
password? : string ;
2016-12-27 08:43:06 -05:00
2016-12-27 06:57:54 -06:00
/**
* Required for decrypt()
* Encrypted user credentials to decrypt upon successful authentication.
*/
token? : string ;
2016-12-27 08:43:06 -05:00
2016-12-06 08:33:03 -05:00
/**
* Set to true to remove the "USE BACKUP" button
*/
disableBackup? : boolean ;
/**
* Change the language. (en_US or es)
*/
2016-12-06 09:53:46 -05:00
locale? : string ;
2016-12-27 08:43:06 -05:00
2016-12-27 06:57:54 -06:00
/**
* The device max is 5 attempts. Set this parameter if you want to allow fewer than 5 attempts.
*/
maxAttempts? : number ;
2016-12-27 08:43:06 -05:00
2016-12-27 06:57:54 -06:00
/**
2016-12-27 08:43:06 -05:00
* Require the user to authenticate with a fingerprint to authorize every use of the key.
* New fingerprint enrollment will invalidate key and require backup authenticate to
2016-12-27 06:57:54 -06:00
* re-enable the fingerprint authentication dialog.
*/
userAuthRequired? : boolean ;
2016-12-27 08:43:06 -05:00
2016-12-27 06:57:54 -06:00
/**
* Set the title of the fingerprint authentication dialog.
*/
dialogTitle? : string ;
2016-12-27 08:43:06 -05:00
2016-12-27 06:57:54 -06:00
/**
* Set the message of the fingerprint authentication dialog.
*/
dialogMessage? : string ;
2016-12-27 08:43:06 -05:00
2016-12-27 06:57:54 -06:00
/**
* Set the hint displayed by the fingerprint icon on the fingerprint authentication dialog.
*/
dialogHint? : string ;
2016-12-06 08:33:03 -05:00
}
2017-03-20 16:38:14 -04:00
export interface AFADecryptOptions {
/**
* Biometric authentication
*/
withFingerprint : boolean ;
/**
* Authentication using backup credential activity
*/
withBackup : boolean ;
/**
* FingerprintAuth.CipherMode.DECRYPT
* Decrypted password
*/
password : string ;
}
export interface AFAEncryptResponse {
/**
* Biometric authentication
*/
withFingerprint : boolean ;
/**
* Authentication using backup credential activity
*/
withBackup : boolean ;
/**
* base64encoded string representation of user credentials
*/
token : string ;
}
2018-04-08 18:51:08 +02:00
export interface AFAAvailableResponse {
isAvailable : boolean ;
isHardwareDetected : boolean ;
hasEnrolledFingerprints : boolean ;
}
export interface AFADeleteOptions {
clientId : string ;
username : string ;
}
2016-07-23 07:13:35 -04:00
/**
* @name Android Fingerprint Auth
* @description
* This plugin will open a native dialog fragment prompting the user to authenticate using their fingerprint. If the device has a secure lockscreen (pattern, PIN, or password), the user may opt to authenticate using that method as a backup.
* @usage
* ```typescript
2018-10-10 16:13:45 -05:00
* import { AndroidFingerprintAuth } from '@ionic-native/android-fingerprint-auth/ngx';
2017-03-20 16:38:14 -04:00
*
* constructor(private androidFingerprintAuth: AndroidFingerprintAuth) { }
*
* ...
*
2016-07-23 07:13:35 -04:00
*
2017-03-20 16:38:14 -04:00
* this.androidFingerprintAuth.isAvailable()
2016-07-23 07:13:35 -04:00
* .then((result)=> {
* if(result.isAvailable){
* // it is available
*
2017-04-30 20:36:22 +02:00
* this.androidFingerprintAuth.encrypt({ clientId: 'myAppName', username: 'myUsername', password: 'myPassword' })
2016-07-23 07:13:35 -04:00
* .then(result => {
2016-12-27 06:57:54 -06:00
* if (result.withFingerprint) {
2017-04-30 20:36:22 +02:00
* console.log('Successfully encrypted credentials.');
* console.log('Encrypted credentials: ' + result.token);
2016-12-27 06:57:54 -06:00
* } else if (result.withBackup) {
2016-07-23 07:13:35 -04:00
* console.log('Successfully authenticated with backup password!');
* } else console.log('Didn\'t authenticate!');
* })
2016-12-27 06:57:54 -06:00
* .catch(error => {
2017-05-09 23:38:30 +02:00
* if (error === this.androidFingerprintAuth.ERRORS.FINGERPRINT_CANCELLED) {
2017-04-30 20:36:22 +02:00
* console.log('Fingerprint authentication cancelled');
2016-12-27 06:57:54 -06:00
* } else console.error(error)
* });
2016-07-23 07:13:35 -04:00
*
* } else {
* // fingerprint auth isn't available
* }
* })
* .catch(error => console.error(error));
* ```
2016-12-06 08:33:03 -05:00
* @interfaces
2017-03-20 16:38:14 -04:00
* AFAAuthOptions
* AFAEncryptResponse
* AFADecryptOptions
2018-04-08 18:51:08 +02:00
* AFAAvailableResponse
* AFADeleteOptions
2016-07-23 07:13:35 -04:00
*/
@Plugin ( {
2016-10-27 12:48:50 -05:00
pluginName : 'AndroidFingerprintAuth' ,
2016-07-23 07:13:35 -04:00
plugin : 'cordova-plugin-android-fingerprint-auth' ,
pluginRef : 'FingerprintAuth' ,
2017-03-28 08:24:04 -04:00
repo : 'https://github.com/mjwheatley/cordova-plugin-android-fingerprint-auth' ,
platforms : [ 'Android' ]
2016-07-23 07:13:35 -04:00
} )
2017-03-20 16:38:14 -04:00
@Injectable ( )
2017-04-27 00:36:12 -04:00
export class AndroidFingerprintAuth extends IonicNativePlugin {
2018-06-22 18:13:47 +02:00
/**
* Convenience property containing all possible errors
*/
2017-05-09 23:38:30 +02:00
ERRORS : {
2018-06-22 18:13:47 +02:00
BAD_PADDING_EXCEPTION : string ;
CERTIFICATE_EXCEPTION : string ;
FINGERPRINT_CANCELLED : string ;
FINGERPRINT_DATA_NOT_DELETED : string ;
FINGERPRINT_ERROR : string ;
FINGERPRINT_NOT_AVAILABLE : string ;
FINGERPRINT_PERMISSION_DENIED : string ;
FINGERPRINT_PERMISSION_DENIED_SHOW_REQUEST : string ;
ILLEGAL_BLOCK_SIZE_EXCEPTION : string ;
INIT_CIPHER_FAILED : string ;
INVALID_ALGORITHM_PARAMETER_EXCEPTION : string ;
IO_EXCEPTION : string ;
JSON_EXCEPTION : string ;
MINIMUM_SDK : string ;
MISSING_ACTION_PARAMETERS : string ;
MISSING_PARAMETERS : string ;
NO_SUCH_ALGORITHM_EXCEPTION : string ;
SECURITY_EXCEPTION : string ;
} = {
BAD_PADDING_EXCEPTION : 'BAD_PADDING_EXCEPTION' ,
CERTIFICATE_EXCEPTION : 'CERTIFICATE_EXCEPTION' ,
FINGERPRINT_CANCELLED : 'FINGERPRINT_CANCELLED' ,
FINGERPRINT_DATA_NOT_DELETED : 'FINGERPRINT_DATA_NOT_DELETED' ,
FINGERPRINT_ERROR : 'FINGERPRINT_ERROR' ,
FINGERPRINT_NOT_AVAILABLE : 'FINGERPRINT_NOT_AVAILABLE' ,
FINGERPRINT_PERMISSION_DENIED : 'FINGERPRINT_PERMISSION_DENIED' ,
FINGERPRINT_PERMISSION_DENIED_SHOW_REQUEST : 'FINGERPRINT_PERMISSION_DENIED_SHOW_REQUEST' ,
ILLEGAL_BLOCK_SIZE_EXCEPTION : 'ILLEGAL_BLOCK_SIZE_EXCEPTION' ,
INIT_CIPHER_FAILED : 'INIT_CIPHER_FAILED' ,
INVALID_ALGORITHM_PARAMETER_EXCEPTION : 'INVALID_ALGORITHM_PARAMETER_EXCEPTION' ,
IO_EXCEPTION : 'IO_EXCEPTION' ,
JSON_EXCEPTION : 'JSON_EXCEPTION' ,
MINIMUM_SDK : 'MINIMUM_SDK' ,
MISSING_ACTION_PARAMETERS : 'MISSING_ACTION_PARAMETERS' ,
MISSING_PARAMETERS : 'MISSING_PARAMETERS' ,
NO_SUCH_ALGORITHM_EXCEPTION : 'NO_SUCH_ALGORITHM_EXCEPTION' ,
SECURITY_EXCEPTION : 'SECURITY_EXCEPTION'
2017-05-09 23:38:30 +02:00
} ;
2016-07-23 07:13:35 -04:00
/**
* Opens a native dialog fragment to use the device hardware fingerprint scanner to authenticate against fingerprints registered for the device.
2018-04-08 18:51:08 +02:00
* @param {AFAAuthOptions} options Options
* @returns {Promise<AFAEncryptResponse>}
2016-07-23 07:13:35 -04:00
*/
@Cordova ( )
2017-12-28 07:28:44 -05:00
encrypt ( options : AFAAuthOptions ) : Promise < AFAEncryptResponse > {
return ;
}
2016-12-27 08:43:06 -05:00
2016-12-27 06:57:54 -06:00
/**
* Opens a native dialog fragment to use the device hardware fingerprint scanner to authenticate against fingerprints registered for the device.
2018-04-08 18:51:08 +02:00
* @param {AFAAuthOptions} options Options
* @returns {Promise<AFADecryptOptions>}
2016-12-27 06:57:54 -06:00
*/
@Cordova ( )
2017-12-28 07:28:44 -05:00
decrypt ( options : AFAAuthOptions ) : Promise < AFADecryptOptions > {
return ;
}
2016-07-23 07:13:35 -04:00
/**
* Check if service is available
2018-04-08 18:51:08 +02:00
* @returns {Promise<AFAAvailableResponse>} Returns a Promise that resolves if fingerprint auth is available on the device
2016-07-23 07:13:35 -04:00
*/
@Cordova ( )
2018-04-08 18:51:08 +02:00
isAvailable ( ) : Promise < AFAAvailableResponse > {
2017-12-28 07:28:44 -05:00
return ;
}
2016-12-27 08:43:06 -05:00
2016-12-27 06:57:54 -06:00
/**
* Delete the cipher used for encryption and decryption by username
2018-04-08 18:51:08 +02:00
* @param {AFADeleteOptions} options Options
* @returns {Promise<{ deleted: boolean }>} Returns a Promise that resolves if the cipher was successfully deleted
2016-12-27 06:57:54 -06:00
*/
@Cordova ( )
2018-04-08 18:51:08 +02:00
delete ( options : AFADeleteOptions ) : Promise < { deleted : boolean } > {
2017-12-28 07:28:44 -05:00
return ;
}
2016-07-23 07:13:35 -04:00
}