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

[local_auth] Update app facing package to use default method channel implementation in new platform interface package. #5195

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/local_auth/local_auth/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## NEXT

* Adds OS version support information to README.
* Switches over to default method implementation in new platform interface.

## 1.1.11

Expand Down
7 changes: 5 additions & 2 deletions packages/local_auth/local_auth/lib/auth_strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
// ignore_for_file: public_member_api_docs

import 'package:intl/intl.dart';
import 'package:local_auth_platform_interface/types/auth_messages.dart';

/// Android side authentication messages.
///
/// Provides default values for all messages.
class AndroidAuthMessages {
class AndroidAuthMessages extends AuthMessages {
const AndroidAuthMessages({
this.biometricHint,
this.biometricNotRecognized,
Expand All @@ -38,6 +39,7 @@ class AndroidAuthMessages {
final String? goToSettingsDescription;
final String? signInTitle;

@override
Map<String, String> get args {
return <String, String>{
'biometricHint': biometricHint ?? androidBiometricHint,
Expand All @@ -62,7 +64,7 @@ class AndroidAuthMessages {
/// iOS side authentication messages.
///
/// Provides default values for all messages.
class IOSAuthMessages {
class IOSAuthMessages extends AuthMessages {
const IOSAuthMessages({
this.lockOut,
this.goToSettingsButton,
Expand All @@ -77,6 +79,7 @@ class IOSAuthMessages {
final String? cancelButton;
final String? localizedFallbackTitle;

@override
Map<String, String> get args {
return <String, String>{
'lockOut': lockOut ?? iOSLockOut,
Expand Down
95 changes: 29 additions & 66 deletions packages/local_auth/local_auth/lib/local_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ import 'dart:async';

import 'package:flutter/foundation.dart' show visibleForTesting;
import 'package:flutter/services.dart';
import 'package:local_auth_platform_interface/local_auth_platform_interface.dart';
import 'package:platform/platform.dart';

import 'auth_strings.dart';
import 'error_codes.dart';

enum BiometricType { face, fingerprint, iris }

const MethodChannel _channel = MethodChannel('plugins.flutter.io/local_auth');
export 'package:local_auth_platform_interface/types/biometric_type.dart';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remember to make the version update a breaking change when re-publishing this plugin, since this is where we're introducing the new enum values. (Which doesn't necessarily have to be a breaking change, but I think that's the best way to do it.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a plan, will update this in #4701.


Platform _platform = const LocalPlatform();

Expand All @@ -31,7 +28,7 @@ void setMockPathProviderPlatform(Platform platform) {
/// A Flutter plugin for authenticating the user identity locally.
class LocalAuthentication {
/// The `authenticateWithBiometrics` method has been deprecated.
/// Use `authenticate` with `biometricOnly: true` instead
/// Use `authenticate` with `biometricOnly: true` instead.
@Deprecated('Use `authenticate` with `biometricOnly: true` instead')
Future<bool> authenticateWithBiometrics({
required String localizedReason,
Expand All @@ -41,21 +38,21 @@ class LocalAuthentication {
IOSAuthMessages iOSAuthStrings = const IOSAuthMessages(),
bool sensitiveTransaction = true,
}) =>
authenticate(
LocalAuthPlatform.instance.authenticate(
localizedReason: localizedReason,
useErrorDialogs: useErrorDialogs,
stickyAuth: stickyAuth,
androidAuthStrings: androidAuthStrings,
iOSAuthStrings: iOSAuthStrings,
sensitiveTransaction: sensitiveTransaction,
biometricOnly: true,
authMessages: <AuthMessages>[iOSAuthStrings, androidAuthStrings],
options: AuthenticationOptions(
useErrorDialogs: useErrorDialogs,
stickyAuth: stickyAuth,
sensitiveTransaction: sensitiveTransaction,
biometricOnly: true,
),
);

/// Authenticates the user with biometrics available on the device while also
/// allowing the user to use device authentication - pin, pattern, passcode.
///
/// Returns a [Future] holding true, if the user successfully authenticated,
/// false otherwise.
/// Returns true, if the user successfully authenticated, false otherwise.
///
/// [localizedReason] is the message to show to user while prompting them
/// for authentication. This is typically along the lines of: 'Please scan
Expand Down Expand Up @@ -99,29 +96,17 @@ class LocalAuthentication {
IOSAuthMessages iOSAuthStrings = const IOSAuthMessages(),
bool sensitiveTransaction = true,
bool biometricOnly = false,
}) async {
assert(localizedReason.isNotEmpty);

final Map<String, Object> args = <String, Object>{
'localizedReason': localizedReason,
'useErrorDialogs': useErrorDialogs,
'stickyAuth': stickyAuth,
'sensitiveTransaction': sensitiveTransaction,
'biometricOnly': biometricOnly,
};
if (_platform.isIOS) {
args.addAll(iOSAuthStrings.args);
} else if (_platform.isAndroid) {
args.addAll(androidAuthStrings.args);
} else {
throw PlatformException(
code: otherOperatingSystem,
message: 'Local authentication does not support non-Android/iOS '
'operating systems.',
details: 'Your operating system is ${_platform.operatingSystem}',
);
}
return (await _channel.invokeMethod<bool>('authenticate', args)) ?? false;
}) {
return LocalAuthPlatform.instance.authenticate(
localizedReason: localizedReason,
authMessages: <AuthMessages>[iOSAuthStrings, androidAuthStrings],
options: AuthenticationOptions(
useErrorDialogs: useErrorDialogs,
stickyAuth: stickyAuth,
sensitiveTransaction: sensitiveTransaction,
biometricOnly: biometricOnly,
),
);
}

/// Returns true if auth was cancelled successfully.
Expand All @@ -131,52 +116,30 @@ class LocalAuthentication {
/// Returns [Future] bool true or false:
Future<bool> stopAuthentication() async {
if (_platform.isAndroid) {
return await _channel.invokeMethod<bool>('stopAuthentication') ?? false;
return LocalAuthPlatform.instance.stopAuthentication();
}
return true;
}

/// Returns true if device is capable of checking biometrics
///
/// Returns a [Future] bool true or false:
Future<bool> get canCheckBiometrics async =>
(await _channel.invokeListMethod<String>('getAvailableBiometrics'))!
.isNotEmpty;
Future<bool> get canCheckBiometrics =>
LocalAuthPlatform.instance.deviceSupportsBiometrics();

/// Returns true if device is capable of checking biometrics or is able to
/// fail over to device credentials.
///
/// Returns a [Future] bool true or false:
Future<bool> isDeviceSupported() async =>
(await _channel.invokeMethod<bool>('isDeviceSupported')) ?? false;
LocalAuthPlatform.instance.isDeviceSupported();

/// 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.invokeListMethod<String>(
'getAvailableBiometrics',
)) ??
<String>[];
final List<BiometricType> biometrics = <BiometricType>[];
for (final String value in result) {
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;
}
}
return biometrics;
}
Future<List<BiometricType>> getAvailableBiometrics() =>
LocalAuthPlatform.instance.getEnrolledBiometrics();
}
9 changes: 7 additions & 2 deletions packages/local_auth/local_auth/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ repository: https://github.com/flutter/plugins/tree/main/packages/local_auth/loc
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+local_auth%22
version: 1.1.11

# Temporarily disable publishing to allow moving Android and iOS
# implementations.
publish_to: none

environment:
sdk: ">=2.14.0 <3.0.0"
flutter: ">=2.5.0"
flutter: ">=2.8.0"

flutter:
plugin:
Expand All @@ -23,6 +27,7 @@ dependencies:
sdk: flutter
flutter_plugin_android_lifecycle: ^2.0.1
intl: ^0.17.0
local_auth_platform_interface: ^1.0.1
platform: ^3.0.0

dev_dependencies:
Expand All @@ -32,4 +37,4 @@ dev_dependencies:
sdk: flutter
integration_test:
sdk: flutter
pedantic: ^1.10.0
mockito: ^5.1.0
Loading