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));
        }
        private void ShowBiometricPrompt()
        {
            var executor        = ContextCompat.GetMainExecutor(this);
            var passwordStorage = new PasswordStorageManager(this);
            var callback        = new AuthenticationCallback();

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

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

                await AttemptUnlock(password);
            };

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

            callback.Error += (_, result) =>
            {
                Toast.MakeText(this, 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();

            Cipher cipher;

            try
            {
                cipher = passwordStorage.GetDecryptionCipher();
            }
            catch
            {
                _canUseBiometrics            = false;
                _useBiometricsButton.Enabled = false;
                return;
            }

            _prompt.Authenticate(promptInfo, new BiometricPrompt.CryptoObject(cipher));
        }
Example #3
0
        private async void OnSetPasswordButtonClick(object sender, EventArgs e)
        {
            if (_passwordText.Text != _passwordConfirmText.Text)
            {
                _passwordConfirmLayout.Error = GetString(Resource.String.passwordsDoNotMatch);
                return;
            }

            var newPassword = _passwordText.Text == "" ? null : _passwordText.Text;

            _setPasswordButton.Enabled = _cancelButton.Enabled = false;
            _setPasswordButton.SetText(newPassword != null ? Resource.String.encrypting : Resource.String.decrypting);
            SetCancelable(false);

            try
            {
                var currentPassword = await SecureStorageWrapper.GetDatabasePassword();

                await Database.SetPassword(currentPassword, newPassword);

                try
                {
                    await SecureStorageWrapper.SetDatabasePassword(newPassword);
                }
                catch
                {
                    // Revert changes
                    await Database.SetPassword(newPassword, currentPassword);

                    throw;
                }
            }
            catch
            {
                Toast.MakeText(Context, Resource.String.genericError, ToastLength.Short).Show();
                SetCancelable(true);
                UpdateSetPasswordButton();
                _cancelButton.Enabled = true;
                return;
            }

            _preferences.AllowBiometrics   = false;
            _preferences.PasswordProtected = newPassword != null;
            _preferences.PasswordChanged   = true;

            try
            {
                var manager = new PasswordStorageManager(Context);
                manager.Clear();
            }
            catch
            {
                // Not really an issue if this fails
            }

            await((SettingsActivity)Context).BaseApplication.Unlock(newPassword);
            Dismiss();
        }
Example #4
0
        public void ClearBiometrics()
        {
            var storage = new PasswordStorageManager(this);

            storage.Clear();
        }
        private async void OnSetPasswordButtonClick(object sender, EventArgs args)
        {
            if (_passwordText.Text != _passwordConfirmText.Text)
            {
                _passwordConfirmLayout.Error = GetString(Resource.String.passwordsDoNotMatch);
                return;
            }

            var newPassword = _passwordText.Text == "" ? null : _passwordText.Text;

            _setPasswordButton.Enabled = _cancelButton.Enabled = false;
            _setPasswordButton.SetText(newPassword != null ? Resource.String.encrypting : Resource.String.decrypting);
            _progressBar.Visibility = ViewStates.Visible;
            SetCancelable(false);

            try
            {
                var currentPassword = await SecureStorageWrapper.GetDatabasePassword();

                await _database.SetPassword(currentPassword, newPassword);

                try
                {
                    await SecureStorageWrapper.SetDatabasePassword(newPassword);
                }
                catch
                {
                    // Revert changes
                    await _database.SetPassword(newPassword, currentPassword);

                    throw;
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);

                Toast.MakeText(Context, Resource.String.genericError, ToastLength.Short).Show();
                _progressBar.Visibility = ViewStates.Invisible;
                SetCancelable(true);
                UpdateSetPasswordButton();
                _cancelButton.Enabled = true;
                return;
            }

            _preferences.AllowBiometrics   = false;
            _preferences.PasswordProtected = newPassword != null;
            _preferences.PasswordChanged   = true;

            if (newPassword == null && _preferences.DatabasePasswordBackup)
            {
                _preferences.DatabasePasswordBackup = false;
            }

            try
            {
                var manager = new PasswordStorageManager(Context);
                manager.Clear();
            }
            catch (Exception e)
            {
                // Not really an issue if this fails
                Logger.Error(e);
            }

            Dismiss();
        }