Flutter Package for Using Biometric Authentication

biometric_package

A Flutter package to help with biometric authentication

Getting Started

This package use BLoC design to enable the creation of a widget that will:

  • List Biometric devices availabe (currently devices on have 1, but there is allowence for more)
  • Authenticate using the device with exception handling for failure, lockout, passcode not being set, etc
  • Handles reporting/managing of timeouts to limit how long an authentication is valid
  • Enable/Disable a device from use

The BLoC pattern uses flutter_bloc defines a BiometricCubit to handle events and emit state changes.

States

The following states:

  1. AuthenticatedState - Reporting on how long a user has been authenticated
  2. AvailabilityState - List of one or more biometric sensors
  3. BiometricallyAuthenticated - When the biometric device has authenticated the user
  4. CancelState - When authentication has been canceled before time out reached
  5. EnableSensorState, - Enable/Disable a biometric device, this would be a preference option on biometric use
  6. NoBiometricsState - Device does not support biometric authentication
  7. ExceptionState - Reasons authentication failed (eg: not biometric data registered, device lockout, etc)
  8. InitialState - The first BLoC state when builder is first created
  9. LockedoutState - The device is reporting a temporary or permanent lockout
  10. PlatformError - Any error generated by the device/system outside the scope of the package
  11. TimeoutState - The timeout limit was reached and authentication is now invalid
class AuthenticatedState extends BiometricSensorState {
  final Duration duration;
  const AuthenticatedState(this.duration) : super(BiometricBuilderState.AuthenticatedState);
  @override
  List<Object> get props => [duration, biometricSensorState];
}

class BiometricallyAuthenticated extends BiometricSensorState {
  const BiometricallyAuthenticated() : super(BiometricBuilderState.BiometricallyAuthenticated);
}

class AvailabilityState extends BiometricSensorState {
  final List<BiometricSensorType> listOfSensors;
  const AvailabilityState(this.listOfSensors) : super(BiometricBuilderState.AvailabilityState);
  @override
  List<Object> get props => [listOfSensors, biometricSensorState];
}

class LockedoutState extends BiometricSensorState {
  final BiometricException exception;
  final Duration duration;
  const LockedoutState(this.exception, this.duration) : super(BiometricBuilderState.LockedoutState);
  @override
  List<Object> get props => [exception, duration, biometricSensorState];
}

class CancelState extends BiometricSensorState {
  final Duration timeout;
  const CancelState(this.timeout) : super(BiometricBuilderState.CancelState);
  @override
  List<Object> get props => [timeout, biometricSensorState];
}

class EnableSensorState extends BiometricSensorState {
  final bool enabled;
  final BiometricSensorType sensor;
  const EnableSensorState(this.sensor, this.enabled) : super(BiometricBuilderState.EnableSensorState);
  @override
  List<Object> get props => [sensor, enabled, biometricSensorState];
}

class ExceptionState extends BiometricSensorState {
  final BiometricException biometricException;
  const ExceptionState(this.biometricException) : super(BiometricBuilderState.ExceptionState);
  @override
  List<Object> get props => [biometricException, biometricSensorState];
}

class InitialState extends BiometricSensorState {
  const InitialState() : super(BiometricBuilderState.InitialState);
}

class NoBiometricsState extends BiometricSensorState {
  final BiometricSupport biometricSupport;
  const NoBiometricsState(this.biometricSupport) : super(BiometricBuilderState.NoBiometricsState);
}

class PlatformError extends BiometricSensorState {
  final AppException platformError;
  const PlatformError(this.platformError) : super(BiometricBuilderState.PlatformError);
  @override
  List<Object> get props => [platformError, biometricSensorState];
}

class TimeoutState extends BiometricSensorState {
  final Duration timeout;
  const TimeoutState(this.timeout) : super(BiometricBuilderState.TimeoutState);
  @override
  List<Object> get props => [timeout, biometricSensorState];
}

BLoC

The ‘Business LOgic Component’(BLoC) is managed by the BiometicCubit. This can be customised to fit the tasks associated with different authentication packages. The design uses pub.dev local_auth to handle events, and host memories that will generate State.

BiometricSensorCubit has the following methods:

/// To cancel an active authencation
void cancelAuthentication() async{}

/// Will emit the Duration an authentication has been active for, or report lockout, or timeout
void reportDuration({Duration delayDuration}) async {}

/// Query the device (if not previously locked out or previously authenticated)
void sensorAuthenticate({@required BiometricSensorType usingSensor}) async {}

/// Enable/Disable a sensor from use (a preference setting, not effect on hardware)
void setSensorEnabled({@required BiometricSensorType sensor, @required bool enabled}) async {}

/// Set up the authentication useage: Get sensors or report if no sensors,
void setup({@required Duration timeout}) async {}

Repository

To abstract from a specific biometric package (say: local_auth), a Repository design is used: biometric_repository.dart shows the kinds of reply from the biometric layer:

  Authentication - Device authentication
  Availability - List of devices
  Error - Errors with device usuage
  Exception - Reason authentication failed
  Sensors - Information about sensor state

The repo local_biometric_respository.dart implements biometric_repository and is uses pub.dev local_auth to communitate with a biometric device.

Usage

The example example.dart has the minimal implementation, as far as showing STATE.

NOTE:

BiometricBuilderState.BiometricallyAuthenticated
BiometricBuilderState.EnableSensorState:

When these states are emitted a call backback to _cubit.reportDuration(delayDuration:) for periodic updates on how long a user has been authenticated AND enforce Timeout event/state.

EXTRAS

  • Widget that displays a slide switch and controls the enable/disable of a sensor
class SensorWidget extends StatelessWidget {
  final BiometricCubit biometricCubit;
  final BiometricSensorType sensor;
  final Widget trueCaption;
  final Widget falseCaption;
  final double iconSize;
  const SensorWidget({
    Key key,
    @required this.biometricCubit,
    @required this.sensor,
    @required this.trueCaption,
    @required this.falseCaption,
    this.iconSize = 32.0,
  })
  • Test if user has ever been authenticated using biometrics (returns null if never authenticated)
  final DateTime lastAuthorization = await MostRecentAuthentication.get();

Download Details:

Author: GitHubStuff

Source Code: https://github.com/GitHubStuff/biometric_package

#flutter #dart #mobile-apps

Flutter Package for Using Biometric Authentication
5.70 GEEK