local_auth_device_credentials 0.6.3+8

  • Readme
  • Changelog
  • Example
  • Installing
  • 88

local_auth with device credential option #

This is a fork of the local_auth

There is an open Pull Request to merge into bases local_auth package.

Addresses issue: #49703

Device credentials #

The original plugin only supports biometric authentication. This package add a method that allows for device credentials if biometric authentication is not available.

localAuth.authenticate() behaves the same as the original plugin's localAuth.authenticateWithBiometrics() but also allows device credentials (pin, pattern, passcode) to be use.

Check if device supports #

final LocalAuthentication auth = LocalAuthentication();

// API 23 and up with security features enabled for Android
// Any iOS device
bool isSupported = await auth.isDeviceSupported();

Authenticate by any means #

final LocalAuthentication auth = LocalAuthentication();

authenticated = await auth.authenticate(
    localizedReason: 'Let OS determine authentication method',
    useErrorDialogs: true,
    stickyAuth: true);

Check biometrics #

final LocalAuthentication auth = LocalAuthentication();

bool isBiometricsAvailable = await auth.canCheckBiometrics();

Authenticate with only biometrics #

final LocalAuthentication auth = LocalAuthentication();

authenticated = await auth.authenticateWithBiometrics(
    localizedReason: 'Biometrics only',
    useErrorDialogs: true,
    stickyAuth: true);

Change log #

0.6.3+8 #

  • Android: Add result listener to registrar in constructor

0.6.3+7 #

  • Android: Add getActivity() method to avoid null error

0.6.3+6 #

  • Android: Use binding to fetch current activity

0.6.3+5 #

  • Android: Use application context when creating intent

0.6.3+4 #

  • Cancel authentication even when biometric prompt not used

0.6.3+3 #

  • Handles authentication for API 23 and up
  • Returns unsupported status if device is using API 22 or below

0.6.3+2 #

  • Bug fix: set Android variable authInProgress to false when authentication cancelled

0.6.3+1 #

  • Handle .isDeviceSupported() on ios (always true)

0.6.3 #

  • Add method .isDeviceSupported() to determine if device is supported.
    • Supports Android 9.0 or higher
    • Supports below Android 9.0 only if fingerprint reader hardware available

0.6.2+4 #

  • Fix issues in older versions of android
  • Return not supported error for Android 8 or older that do not have biometric hardware

0.6.2+3 #

0.6.2+2 #

  • Update auth string to be more generic

0.6.2+1 #

  • Fix bug with package name

0.6.2 #

  • Add authenticate method that uses biometric authentication, but allows users to also use device authentication - pin, pattern, passcode.

From local_auth #

0.6.1+2 #

  • Support v2 embedding.

0.6.1+1 #

  • Remove the deprecated author: field from pubspec.yaml
  • Migrate the plugin to the pubspec platforms manifest.
  • Require Flutter SDK 1.10.0 or greater.

0.6.1 #

  • Added ability to stop authentication (For Android).

0.6.0+3 #

  • Remove AndroidX warnings.

0.6.0+2 #

  • Update and migrate iOS example project.
  • Define clang module for iOS.

0.6.0+1 #

  • Update the intl constraint to ">=0.15.1 <0.17.0" (0.16.0 isn't really a breaking change).

0.6.0 #

  • Define a new parameter for signaling that the transaction is sensitive.
  • Up the biometric version to beta01.
  • Handle no device credential error.

0.5.3 #

  • Add face id detection as well by not relying on FingerprintCompat.

0.5.2+4 #

  • Update README to fix syntax error.

0.5.2+3 #

  • Update documentation to clarify the need for FragmentActivity.

0.5.2+2 #

  • Add missing template type parameter to invokeMethod calls.
  • Bump minimum Flutter version to 1.5.0.
  • Replace invokeMethod with invokeMapMethod wherever necessary.

0.5.2+1 #

  • Use post instead of postDelayed to show the dialog onResume.

0.5.2 #

  • Executor thread needs to be UI thread.

0.5.1 #

  • Fix crash on Android versions earlier than 28.
  • authenticateWithBiometrics will not return result unless Biometric Dialog is closed.
  • Added two more error codes LockedOut and PermanentlyLockedOut.

0.5.0 #

  • Breaking change. Update the Android API to use androidx Biometric package. This gives the prompt the updated Material look. However, it also requires the activity to be a FragmentActivity. Users can switch to FlutterFragmentActivity in their main app to migrate.

0.4.0+1 #

  • Log a more detailed warning at build time about the previous AndroidX migration.

0.4.0 #

  • Breaking change. Migrate from the deprecated original Android Support Library to AndroidX. This shouldn't result in any functional changes, but it requires any Android apps using this plugin to also migrate if they're using the original support library.

0.3.1 #

  • Fix crash on Android versions earlier than 24.

0.3.0 #

  • Breaking change. Add canCheckBiometrics and getAvailableBiometrics which leads to a new API.

0.2.1 #

  • Updated Gradle tooling to match Android Studio 3.1.2.

0.2.0 #

  • Breaking change. Set SDK constraints to match the Flutter beta release.

0.1.2 #

  • Fixed Dart 2 type error.

0.1.1 #

  • Simplified and upgraded Android project template to Android SDK 27.
  • Updated package description.

0.1.0 #

  • Breaking change. Upgraded to Gradle 4.1 and Android Studio Gradle plugin 3.0.1. Older Flutter projects need to upgrade their Gradle setup as well in order to use this version of the plugin. Instructions can be found here.

0.0.3 #

  • Add FLT prefix to iOS types

0.0.2+1 #

  • Update messaging to support Face ID.

0.0.2 #

  • Support stickyAuth mode.

0.0.1 #

  • Initial release of local authentication plugin.

example/lib/main.dart

// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: public_member_api_docs

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:local_auth_device_credentials/local_auth.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final LocalAuthentication auth = LocalAuthentication();
  bool _canCheckBiometrics;
  List<BiometricType> _availableBiometrics;
  String _authorized = 'Not Authorized';
  bool _isAuthenticating = false;
  bool _isSupported;

  @override
  void initState() {
    super.initState();
    auth.isDeviceSupported().then((isSupported) => setState(() => _isSupported = isSupported));
  }

  Future<void> _checkBiometrics() async {
    bool canCheckBiometrics;
    try {
      canCheckBiometrics = await auth.canCheckBiometrics;
    } on PlatformException catch (e) {
      print(e);
    }
    if (!mounted) return;

    setState(() {
      _canCheckBiometrics = canCheckBiometrics;
    });
  }

  Future<void> _getAvailableBiometrics() async {
    List<BiometricType> availableBiometrics;
    try {
      availableBiometrics = await auth.getAvailableBiometrics();
    } on PlatformException catch (e) {
      print(e);
    }
    if (!mounted) return;

    setState(() {
      _availableBiometrics = availableBiometrics;
    });
  }

  Future<void> _authenticate() async {
    bool authenticated = false;
    try {
      setState(() {
        _isAuthenticating = true;
        _authorized = 'Authenticating';
      });
      authenticated = await auth.authenticate(
        localizedReason: 'Let OS determine authentication method',
        useErrorDialogs: true,
        stickyAuth: true,
      );
      setState(() => _authorized = authenticated ? 'Authorized' : 'Not Authorized');
    } on PlatformException catch (e) {
      setState(() => _authorized = e.message);
    } finally {
      setState(() => _isAuthenticating = false);
    }
  }

  Future<void> _authenticateWithBiometrics() async {
    bool authenticated = false;
    try {
      setState(() {
        _isAuthenticating = true;
        _authorized = 'Authenticating';
      });
      authenticated = await auth.authenticateWithBiometrics(localizedReason: 'Scan your fingerprint to authenticate', useErrorDialogs: true, stickyAuth: true);
      setState(() {
        _isAuthenticating = false;
        _authorized = authenticated ? 'Authorized' : 'Not Authorized';
      });
    } on PlatformException catch (e) {
      setState(() => _authorized = e.message);
    } finally {
      setState(() => _isAuthenticating = false);
    }
  }

  void _cancelAuthentication() async {
    await auth.stopAuthentication();
    setState(() => _isAuthenticating = false);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: ListView(
          padding: const EdgeInsets.symmetric(vertical: 30),
          children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                if (_isSupported == null) CircularProgressIndicator() else if (_isSupported) Text("This device is supported") else Text("This device is not supported"),
                Divider(height: 100),
                Text('Can check biometrics: $_canCheckBiometrics\n'),
                RaisedButton(
                  child: const Text('Check biometrics'),
                  onPressed: _checkBiometrics,
                ),
                Divider(height: 100),
                Text('Available biometrics: $_availableBiometrics\n'),
                RaisedButton(
                  child: const Text('Get available biometrics'),
                  onPressed: _getAvailableBiometrics,
                ),
                Divider(height: 100),
                Text('Current State: $_authorized\n'),
                Visibility(
                  visible: !_isAuthenticating,
                  child: Column(
                    children: [
                      RaisedButton(
                        child: Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Text('Authenticate'),
                            Icon(Icons.perm_device_information),
                          ],
                        ),
                        onPressed: _authenticate,
                      ),
                      RaisedButton(
                        child: Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Text(_isAuthenticating ? 'Cancel' : 'Authenticate: biometrics only'),
                            Icon(Icons.fingerprint),
                          ],
                        ),
                        onPressed: _authenticateWithBiometrics,
                      ),
                    ],
                  ),
                ),
                Visibility(
                  visible: _isAuthenticating,
                  child: RaisedButton(
                    onPressed: _cancelAuthentication,
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Text("Cancel Authentication"),
                        Icon(Icons.cancel),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  local_auth_device_credentials: ^0.6.3+8

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:local_auth_device_credentials/auth_strings.dart';
import 'package:local_auth_device_credentials/error_codes.dart';
import 'package:local_auth_device_credentials/local_auth.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
76
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
88
Learn more about scoring.

We analyzed this package on Jun 5, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.2
  • pana: 0.13.8-dev
  • Flutter: 1.17.1

Health suggestions

Format lib/local_auth.dart.

Run flutter format to format lib/local_auth.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
flutter 0.0.0
flutter_plugin_android_lifecycle ^1.0.2 1.0.8
intl >=0.15.1 <0.17.0 0.16.1
meta ^1.0.5 1.1.8
platform ^2.0.0 2.2.1
Transitive dependencies
collection 1.14.12
path 1.7.0
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
e2e ^0.2.1
flutter_driver
flutter_test