Exemplo n.º 1
0
        public FingerprintService(
            FragmentActivity fragmentActivity,
            Context applicationContext,
            IObservable <Unit> applicationActivated,
            CoreDispatcher dispatcher,
            IScheduler backgroundScheduler,
            FuncAsync <BiometricPrompt.PromptInfo> promptInfoBuilder)
        {
            fragmentActivity.Validation().NotNull(nameof(fragmentActivity));
            applicationActivated.Validation().NotNull(nameof(applicationActivated));
            backgroundScheduler.Validation().NotNull(nameof(backgroundScheduler));
            _dispatcher        = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
            _promptInfoBuilder = promptInfoBuilder ?? throw new ArgumentNullException(nameof(promptInfoBuilder));

            var executor = ContextCompat.GetMainExecutor(applicationContext);
            var callback = new AuthenticationCallback(OnAuthenticationSucceeded, OnAuthenticationFailed, OnAuthenticationError);

            _BiometricPrompt  = new BiometricPrompt(fragmentActivity, executor, callback);
            _BiometricManager = BiometricManager.From(Application.Context);

            _keyStore = KeyStore.GetInstance(ANDROID_KEYSTORE);
            _keyStore.Load(null);

            _canAuthenticate =
                applicationActivated
                .ObserveOn(backgroundScheduler)
                .StartWith(backgroundScheduler, Unit.Default)
                .Select(_ => _BiometricManager.CanAuthenticate())
                .Replay(1, backgroundScheduler)
                .RefCount();
        }
        public DeviceSecurityLockScreenType GetDeviceLocalSecurityType()
        {
            try
            {
                var context = Application.Context;

                var keyguardManager = (KeyguardManager)Application.Context.GetSystemService(Context.KeyguardService);

                // first check for IsKeyguardSecure, if it's false then lock screen security is disabled
                if (!keyguardManager.IsKeyguardSecure)
                {
                    return(DeviceSecurityLockScreenType.None);
                }

                var manager = BiometricManager.From(Application.Context);
                var result  = manager.CanAuthenticate();
                if (result == BiometricManager.BiometricSuccess)
                {
                    return(DeviceSecurityLockScreenType.Biometric);
                }


                // at this point keyguardManager.IsKeyguardSecure = true, so at least pass is enabled
                return(DeviceSecurityLockScreenType.Pass);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"Biometric check error: {ex.Message}. StackTrace: {ex.StackTrace}");
                return(DeviceSecurityLockScreenType.Unknown);
            }
        }
    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.");
        }
    }
Exemplo n.º 4
0
        public void Authenticate(IBioAuthCompleted bioAuthCompleted)
        {
            var context          = CrossCurrentActivity.Current.AppContext;
            var biometricManager = BiometricManager.From(context);

            switch (biometricManager.CanAuthenticate())
            {
            case BiometricManager.BiometricSuccess:
                //Log.d("MY_APP_TAG", "App can authenticate using biometrics.");
                ShowBiometicPrompt(bioAuthCompleted);
                break;

            case BiometricManager.BiometricErrorNoHardware:
                bioAuthCompleted.OnCompleted(BioAuthStatus.ENROLL_BiometricErrorNoHardware);
                break;

            case BiometricManager.BiometricErrorHwUnavailable:
                bioAuthCompleted.OnCompleted(BioAuthStatus.ENROLL_BiometricErrorHwUnavailable);
                break;

            case BiometricManager.BiometricErrorNoneEnrolled:
                bioAuthCompleted.OnCompleted(BioAuthStatus.ENROLL_BiometricErrorNoneEnrolled);
                break;
            }

            //MessagingCenter.Send(this, "BioAuth", "Message from Android");
            //bioAuthCompleted.OnCompleted(BioAuthStatus.SUCCESS);
        }
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = base.OnCreateView(inflater, container, savedInstanceState);

            SetupToolbar(view, Resource.String.unlock);

            _preferences = new PreferenceWrapper(Context);
            _progressBar = view.FindViewById <ProgressBar>(Resource.Id.appBarProgressBar);

            _passwordLayout = view.FindViewById <TextInputLayout>(Resource.Id.editPasswordLayout);
            _passwordText   = view.FindViewById <TextInputEditText>(Resource.Id.editPassword);
            TextInputUtil.EnableAutoErrorClear(_passwordLayout);

            _passwordText.EditorAction += (_, e) =>
            {
                if (e.ActionId == ImeAction.Done)
                {
                    UnlockAttempted?.Invoke(this, _passwordText.Text);
                }
            };

            _unlockButton        = view.FindViewById <MaterialButton>(Resource.Id.buttonUnlock);
            _unlockButton.Click += delegate
            {
                UnlockAttempted?.Invoke(this, _passwordText.Text);
            };

            if (_preferences.AllowBiometrics)
            {
                var biometricManager = BiometricManager.From(Context);
                _canUseBiometrics = biometricManager.CanAuthenticate(BiometricManager.Authenticators.BiometricStrong) ==
                                    BiometricManager.BiometricSuccess;
            }

            _useBiometricsButton         = view.FindViewById <MaterialButton>(Resource.Id.buttonUseBiometrics);
            _useBiometricsButton.Enabled = _canUseBiometrics;
            _useBiometricsButton.Click  += delegate
            {
                ShowBiometricPrompt();
            };

            if (_canUseBiometrics)
            {
                ShowBiometricPrompt();
            }
            else
            {
                FocusPasswordText();
            }

            return(view);
        }
Exemplo n.º 6
0
        public static BiometryImplementation GetBiometryImplementation()
        {
            // Biometry API is only available on devices running Android 6.0 and up.
            if (Build.VERSION.SdkInt >= BuildVersionCodes.M &&
                Application.Context.CheckSelfPermission(Android.Manifest.Permission.UseBiometric) == Permission.Granted)
            {
                var bm = BiometricManager.From(Application.Context);
                if (bm.CanAuthenticate() == BiometricManager.BiometricSuccess)
                {
                    // TODO differenciation between BiometryImplementation.FingerprintId and BiometryImplementation.FaceId;
                    return(BiometryImplementation.FingerprintId);
                }
            }

            return(BiometryImplementation.Unavailable);
        }
        private void UpdateSecuritySettingsEnabled()
        {
            if (!_preferences.PasswordProtected)
            {
                _fragment.FindPreference("pref_allowBiometrics").Enabled = false;
                _fragment.FindPreference("pref_timeout").Enabled         = false;
                return;
            }

            var biometricManager    = BiometricManager.From(this);
            var biometricsAvailable = biometricManager.CanAuthenticate() == BiometricManager.BiometricSuccess;

            _fragment.FindPreference("pref_allowBiometrics").Enabled = biometricsAvailable;

            _fragment.FindPreference("pref_timeout").Enabled = true;
        }
Exemplo n.º 8
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="BiometryService" /> class.
        /// </summary>
        /// <param name="fragmentActivity"></param>
        /// <param name="dispatcher"></param>
        /// <param name="promptInfoBuilder"></param>
        /// <param name="authenticators"></param>
        public BiometryService(
            FragmentActivity fragmentActivity,
            CoreDispatcher dispatcher,
            FuncAsync <BiometricPrompt.PromptInfo> promptInfoBuilder)
        {
            fragmentActivity.Validation().NotNull(nameof(fragmentActivity));
            _dispatcher        = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
            _promptInfoBuilder = promptInfoBuilder ?? throw new ArgumentNullException(nameof(promptInfoBuilder));

            _applicationContext = Application.Context;
            var executor = ContextCompat.GetMainExecutor(_applicationContext);
            var callback = new AuthenticationCallback(OnAuthenticationSucceeded, OnAuthenticationFailed, OnAuthenticationError);

            _biometricPrompt  = new BiometricPrompt(fragmentActivity, executor, callback);
            _biometricManager = BiometricManager.From(_applicationContext);

            _keyStore = KeyStore.GetInstance(ANDROID_KEYSTORE);
            _keyStore.Load(null);
        }
Exemplo n.º 9
0
 public FingerprintImplementation()
 {
     _manager = BiometricManager.From(Application.Context);
 }