A Flutter package to help with biometric authentication
This package use BLoC design to enable the creation of a widget that will:
The BLoC pattern uses flutter_bloc defines a BiometricCubit to handle events and emit state changes.
The following states:
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];
}
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 {}
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.
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.
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,
})
final DateTime lastAuthorization = await MostRecentAuthentication.get();
Author: GitHubStuff
Source Code: https://github.com/GitHubStuff/biometric_package
#flutter #dart #mobile-apps