Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Add biometry type detection to local_auth plugin #845

Merged
merged 8 commits into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
package io.flutter.plugins.localauth;

import android.app.Activity;
import android.hardware.fingerprint.FingerprintManager;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.plugins.localauth.AuthenticationHelper.AuthCompletionHandler;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

/** LocalAuthPlugin */
Expand Down Expand Up @@ -72,6 +74,18 @@ public void onError(String code, String error) {
}
});
authenticationHelper.authenticate();
} else if (call.method.equals("getAvailableBiometrics")) {
FingerprintManager fingerprintMgr =
registrar.activity().getSystemService(FingerprintManager.class);
ArrayList<String> biometrics = new ArrayList<String>();
if (fingerprintMgr.isHardwareDetected()) {
if (fingerprintMgr.hasEnrolledFingerprints()) {
biometrics.add("fingerprint");
} else {
biometrics.add("undefined");
}
}
result.success(biometrics);
} else {
result.notImplemented();
}
Expand Down
42 changes: 41 additions & 1 deletion packages/local_auth/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,40 @@ class MyApp extends StatefulWidget {
}

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

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

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

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

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

Future<Null> _authenticate() async {
final LocalAuthentication auth = LocalAuthentication();
bool authenticated = false;
try {
authenticated = await auth.authenticateWithBiometrics(
Expand Down Expand Up @@ -49,6 +79,16 @@ class _MyAppState extends State<MyApp> {
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text('Can check biometrics: $_canCheckBiometrics\n'),
RaisedButton(
child: const Text('Check biometrics'),
onPressed: _checkBiometrics,
),
Text('Available biometrics: $_availableBiometrics\n'),
RaisedButton(
child: const Text('Get available biometrics'),
onPressed: _getAvailableBiometrics,
),
Text('Current State: $_authorized\n'),
RaisedButton(
child: const Text('Authenticate'),
Expand Down
23 changes: 23 additions & 0 deletions packages/local_auth/ios/Classes/LocalAuthPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
if ([@"authenticateWithBiometrics" isEqualToString:call.method]) {
[self authenticateWithBiometrics:call.arguments withFlutterResult:result];
} else if ([@"getAvailableBiometrics" isEqualToString:call.method]) {
[self getAvailableBiometrics:result];
} else {
result(FlutterMethodNotImplemented);
}
Expand Down Expand Up @@ -62,6 +64,27 @@ - (void)alertMessage:(NSString *)message
completion:nil];
}

- (void)getAvailableBiometrics:(FlutterResult)result {
LAContext *context = [[LAContext alloc] init];
NSError *authError = nil;
pdefuns marked this conversation as resolved.
Show resolved Hide resolved
NSMutableArray<NSString *> *biometrics = [[NSMutableArray<NSString *> alloc] init];
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
error:&authError]) {
if (authError == nil) {
if (@available(iOS 11.0.1, *)) {
if (context.biometryType == LABiometryTypeFaceID) {
[biometrics addObject:@"face"];
} else if (context.biometryType == LABiometryTypeTouchID) {
[biometrics addObject:@"fingerprint"];
}
}
}
} else if (authError.code == LAErrorTouchIDNotEnrolled) {
[biometrics addObject:@"undefined"];
}
result(biometrics);
}

- (void)authenticateWithBiometrics:(NSDictionary *)arguments
withFlutterResult:(FlutterResult)result {
LAContext *context = [[LAContext alloc] init];
Expand Down
39 changes: 39 additions & 0 deletions packages/local_auth/lib/local_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import 'package:meta/meta.dart';
import 'auth_strings.dart';
import 'error_codes.dart';

enum BiometricType { face, fingerprint, iris }

const MethodChannel _channel = MethodChannel('plugins.flutter.io/local_auth');

/// A Flutter plugin for authenticating the user identity locally.
Expand Down Expand Up @@ -72,4 +74,41 @@ class LocalAuthentication {
}
return await _channel.invokeMethod('authenticateWithBiometrics', args);
}

/// Returns true if device is capable of checking biometrics
///
/// Returns a [Future] bool true or false:
Future<bool> canCheckBiometrics() async {
final List<dynamic> biometrics =
await _channel.invokeMethod('getAvailableBiometrics');
return biometrics.isNotEmpty;
pdefuns marked this conversation as resolved.
Show resolved Hide resolved
}

/// Returns a list of enrolled biometrics
///
/// Returns a [Future] List<BiometricType> with the following possibilities:
/// - BiometricType.face
/// - BiometricType.fingerprint
/// - BiometricType.iris (not yet implemented)
Future<List<BiometricType>> getAvailableBiometrics() async {
final List<String> result =
(await _channel.invokeMethod('getAvailableBiometrics')).cast<String>();
final List<BiometricType> biometrics = <BiometricType>[];
result.forEach((String value) {
switch (value) {
case 'face':
biometrics.add(BiometricType.face);
break;
case 'fingerprint':
biometrics.add(BiometricType.fingerprint);
break;
case 'iris':
biometrics.add(BiometricType.iris);
break;
case 'undefined':
break;
}
pdefuns marked this conversation as resolved.
Show resolved Hide resolved
});
return biometrics;
}
}