public void EnableBiometrics(Action <bool> callback)
        {
            var passwordStorage = new PasswordStorageManager(this);
            var executor        = ContextCompat.GetMainExecutor(this);
            var authCallback    = new AuthenticationCallback();

            authCallback.Success += async(_, result) =>
            {
                try
                {
                    var password = await SecureStorageWrapper.GetDatabasePassword();

                    passwordStorage.Store(password, result.CryptoObject.Cipher);
                }
                catch (Exception e)
                {
                    Logger.Error(e);
                    callback(false);
                    return;
                }

                callback(true);
            };

            authCallback.Failed += delegate
            {
                callback(false);
            };

            authCallback.Error += delegate
            {
                // Do something, probably
                callback(false);
            };

            var prompt = new BiometricPrompt(this, executor, authCallback);

            var promptInfo = new BiometricPrompt.PromptInfo.Builder()
                             .SetTitle(GetString(Resource.String.setupBiometricUnlock))
                             .SetNegativeButtonText(GetString(Resource.String.cancel))
                             .SetConfirmationRequired(false)
                             .SetAllowedAuthenticators(BiometricManager.Authenticators.BiometricStrong)
                             .Build();

            Cipher cipher;

            try
            {
                cipher = passwordStorage.GetEncryptionCipher();
            }
            catch (Exception e)
            {
                Logger.Error(e);
                passwordStorage.Clear();
                callback(false);
                return;
            }

            prompt.Authenticate(promptInfo, new BiometricPrompt.CryptoObject(cipher));
        }
    public void Authenticate(Func <Task> action, string title)
    {
        try
        {
            switch (BiometricManager.From(_context).CanAuthenticate())
            {
            case BiometricManager.BiometricSuccess:
                var biometricPrompt = new BiometricPrompt(MainActivity, ContextCompat.GetMainExecutor(_context), GetBiometricAuthenticationCallback(action));
                var promptInfo      = new BiometricPrompt.PromptInfo.Builder()
                                      .SetTitle(title == null ? "Biometric login for Falcon" : $"{title}...")
                                      .SetNegativeButtonText("Cancel")
                                      .Build();
                biometricPrompt.Authenticate(promptInfo);
                return;

            case BiometricManager.BiometricErrorHwUnavailable:
                Tools.DisplayAlert(message: "Biometric hardware is currently unavailable. Try again later.");
                return;

            case BiometricManager.BiometricErrorNoneEnrolled:
                Tools.DisplayAlert(message: "The device does not have any biometrics enrolled. Please make sure you have set up any available biometrics in your phone Settings.");
                return;

            default:
                return;
            }
        }
        catch (Exception ex)
        {
            //DisplayAlertError("Something went wrong while using biometric authentication.");
        }
    }
        protected override async Task <FingerprintAuthenticationResult> NativeAuthenticateAsync(AuthenticationRequestConfiguration authRequestConfig, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(authRequestConfig.Title))
            {
                throw new ArgumentException("Title must not be null or empty on Android.", nameof(authRequestConfig.Title));
            }

            if (!(CrossFingerprint.CurrentActivity is FragmentActivity))
            {
                throw new InvalidOperationException($"Expected current activity to be '{typeof(FragmentActivity).FullName}' but was '{CrossFingerprint.CurrentActivity?.GetType().FullName}'. " +
                                                    "You need to use AndroidX. Have you installed Xamarin.AndroidX.Migration in your Android App project!?");
            }

            try
            {
                var cancel = string.IsNullOrWhiteSpace(authRequestConfig.CancelTitle) ?
                             Application.Context.GetString(Android.Resource.String.Cancel) :
                             authRequestConfig.CancelTitle;

                var handler = new AuthenticationHandler();
                var builder = new BiometricPrompt.PromptInfo.Builder()
                              .SetTitle(authRequestConfig.Title)
                              .SetConfirmationRequired(authRequestConfig.ConfirmationRequired)
                              .SetDescription(authRequestConfig.Reason);

                if (authRequestConfig.AllowAlternativeAuthentication)
                {
                    // It's not allowed to allow alternative auth & set the negative button
                    builder = builder.SetDeviceCredentialAllowed(authRequestConfig.AllowAlternativeAuthentication);
                }
                else
                {
                    builder = builder.SetNegativeButtonText(cancel);
                }
                var info     = builder.Build();
                var executor = Executors.NewSingleThreadExecutor();


                var activity = (FragmentActivity)CrossFingerprint.CurrentActivity;
                using var dialog = new BiometricPrompt(activity, executor, handler);
                await using (cancellationToken.Register(() => dialog.CancelAuthentication()))
                {
                    dialog.Authenticate(info);
                    var result = await handler.GetTask();

                    TryReleaseLifecycleObserver(activity, dialog);

                    return(result);
                }
            }
            catch (Exception e)
            {
                return(new FingerprintAuthenticationResult
                {
                    Status = FingerprintAuthenticationResultStatus.UnknownError,
                    ErrorMessage = e.Message
                });
            }
        }
        private void ShowBiometricPrompt()
        {
            var executor        = ContextCompat.GetMainExecutor(Context);
            var passwordStorage = new PasswordStorageManager(Context);
            var callback        = new AuthenticationCallback();

            callback.Succeeded += (_, result) =>
            {
                string password;

                try
                {
                    password = passwordStorage.Fetch(result.CryptoObject.Cipher);
                }
                catch (Exception e)
                {
                    Logger.Error(e);
                    Toast.MakeText(Context, Resource.String.genericError, ToastLength.Short);
                    return;
                }

                UnlockAttempted?.Invoke(this, password);
            };

            callback.Failed += delegate
            {
                FocusPasswordText();
            };

            callback.Errored += (_, result) =>
            {
                Toast.MakeText(Context, result.Message, ToastLength.Short).Show();
                FocusPasswordText();
            };

            _prompt = new BiometricPrompt(this, executor, callback);

            var promptInfo = new BiometricPrompt.PromptInfo.Builder()
                             .SetTitle(GetString(Resource.String.unlock))
                             .SetSubtitle(GetString(Resource.String.unlockBiometricsMessage))
                             .SetNegativeButtonText(GetString(Resource.String.cancel))
                             .SetConfirmationRequired(false)
                             .SetAllowedAuthenticators(BiometricManager.Authenticators.BiometricStrong)
                             .Build();

            try
            {
                var cipher = passwordStorage.GetDecryptionCipher();
                _prompt.Authenticate(promptInfo, new BiometricPrompt.CryptoObject(cipher));
            }
            catch (Exception e)
            {
                Logger.Error(e);
                _canUseBiometrics            = false;
                _useBiometricsButton.Enabled = false;
                FocusPasswordText();
            }
        }
Exemple #5
0
        public void StartListening(BiometricPrompt.AuthenticationCallback callback)
        {
            Kp2aLog.Log("Fingerprint: StartListening ");

            var executor = Executors.NewSingleThreadExecutor();

            _biometricPrompt = new BiometricPrompt(_activity, executor, callback);

            BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
                                                    .SetTitle(_activity.GetString(AppNames.AppNameResource))
                                                    .SetSubtitle(_activity.GetString(Resource.String.unlock_database_title))
                                                    .SetNegativeButtonText(_activity.GetString(Android.Resource.String.Cancel))
                                                    .SetConfirmationRequired(false)
                                                    .Build();


            _biometricPrompt.Authenticate(promptInfo, _cryptoObject);
        }