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

Commit dd2cd96

Browse files
authored
[local_auth] Update app facing package to use default method channel implementation in new platform interface package. (#5195)
1 parent 700fba5 commit dd2cd96

File tree

5 files changed

+168
-304
lines changed

5 files changed

+168
-304
lines changed

packages/local_auth/local_auth/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## NEXT
22

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

56
## 1.1.11
67

packages/local_auth/local_auth/lib/auth_strings.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
// ignore_for_file: public_member_api_docs
1010

1111
import 'package:intl/intl.dart';
12+
import 'package:local_auth_platform_interface/types/auth_messages.dart';
1213

1314
/// Android side authentication messages.
1415
///
1516
/// Provides default values for all messages.
16-
class AndroidAuthMessages {
17+
class AndroidAuthMessages extends AuthMessages {
1718
const AndroidAuthMessages({
1819
this.biometricHint,
1920
this.biometricNotRecognized,
@@ -38,6 +39,7 @@ class AndroidAuthMessages {
3839
final String? goToSettingsDescription;
3940
final String? signInTitle;
4041

42+
@override
4143
Map<String, String> get args {
4244
return <String, String>{
4345
'biometricHint': biometricHint ?? androidBiometricHint,
@@ -62,7 +64,7 @@ class AndroidAuthMessages {
6264
/// iOS side authentication messages.
6365
///
6466
/// Provides default values for all messages.
65-
class IOSAuthMessages {
67+
class IOSAuthMessages extends AuthMessages {
6668
const IOSAuthMessages({
6769
this.lockOut,
6870
this.goToSettingsButton,
@@ -77,6 +79,7 @@ class IOSAuthMessages {
7779
final String? cancelButton;
7880
final String? localizedFallbackTitle;
7981

82+
@override
8083
Map<String, String> get args {
8184
return <String, String>{
8285
'lockOut': lockOut ?? iOSLockOut,

packages/local_auth/local_auth/lib/local_auth.dart

Lines changed: 29 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@ import 'dart:async';
1212

1313
import 'package:flutter/foundation.dart' show visibleForTesting;
1414
import 'package:flutter/services.dart';
15+
import 'package:local_auth_platform_interface/local_auth_platform_interface.dart';
1516
import 'package:platform/platform.dart';
16-
1717
import 'auth_strings.dart';
18-
import 'error_codes.dart';
19-
20-
enum BiometricType { face, fingerprint, iris }
2118

22-
const MethodChannel _channel = MethodChannel('plugins.flutter.io/local_auth');
19+
export 'package:local_auth_platform_interface/types/biometric_type.dart';
2320

2421
Platform _platform = const LocalPlatform();
2522

@@ -31,7 +28,7 @@ void setMockPathProviderPlatform(Platform platform) {
3128
/// A Flutter plugin for authenticating the user identity locally.
3229
class LocalAuthentication {
3330
/// The `authenticateWithBiometrics` method has been deprecated.
34-
/// Use `authenticate` with `biometricOnly: true` instead
31+
/// Use `authenticate` with `biometricOnly: true` instead.
3532
@Deprecated('Use `authenticate` with `biometricOnly: true` instead')
3633
Future<bool> authenticateWithBiometrics({
3734
required String localizedReason,
@@ -41,21 +38,21 @@ class LocalAuthentication {
4138
IOSAuthMessages iOSAuthStrings = const IOSAuthMessages(),
4239
bool sensitiveTransaction = true,
4340
}) =>
44-
authenticate(
41+
LocalAuthPlatform.instance.authenticate(
4542
localizedReason: localizedReason,
46-
useErrorDialogs: useErrorDialogs,
47-
stickyAuth: stickyAuth,
48-
androidAuthStrings: androidAuthStrings,
49-
iOSAuthStrings: iOSAuthStrings,
50-
sensitiveTransaction: sensitiveTransaction,
51-
biometricOnly: true,
43+
authMessages: <AuthMessages>[iOSAuthStrings, androidAuthStrings],
44+
options: AuthenticationOptions(
45+
useErrorDialogs: useErrorDialogs,
46+
stickyAuth: stickyAuth,
47+
sensitiveTransaction: sensitiveTransaction,
48+
biometricOnly: true,
49+
),
5250
);
5351

5452
/// Authenticates the user with biometrics available on the device while also
5553
/// allowing the user to use device authentication - pin, pattern, passcode.
5654
///
57-
/// Returns a [Future] holding true, if the user successfully authenticated,
58-
/// false otherwise.
55+
/// Returns true, if the user successfully authenticated, false otherwise.
5956
///
6057
/// [localizedReason] is the message to show to user while prompting them
6158
/// for authentication. This is typically along the lines of: 'Please scan
@@ -99,29 +96,17 @@ class LocalAuthentication {
9996
IOSAuthMessages iOSAuthStrings = const IOSAuthMessages(),
10097
bool sensitiveTransaction = true,
10198
bool biometricOnly = false,
102-
}) async {
103-
assert(localizedReason.isNotEmpty);
104-
105-
final Map<String, Object> args = <String, Object>{
106-
'localizedReason': localizedReason,
107-
'useErrorDialogs': useErrorDialogs,
108-
'stickyAuth': stickyAuth,
109-
'sensitiveTransaction': sensitiveTransaction,
110-
'biometricOnly': biometricOnly,
111-
};
112-
if (_platform.isIOS) {
113-
args.addAll(iOSAuthStrings.args);
114-
} else if (_platform.isAndroid) {
115-
args.addAll(androidAuthStrings.args);
116-
} else {
117-
throw PlatformException(
118-
code: otherOperatingSystem,
119-
message: 'Local authentication does not support non-Android/iOS '
120-
'operating systems.',
121-
details: 'Your operating system is ${_platform.operatingSystem}',
122-
);
123-
}
124-
return (await _channel.invokeMethod<bool>('authenticate', args)) ?? false;
99+
}) {
100+
return LocalAuthPlatform.instance.authenticate(
101+
localizedReason: localizedReason,
102+
authMessages: <AuthMessages>[iOSAuthStrings, androidAuthStrings],
103+
options: AuthenticationOptions(
104+
useErrorDialogs: useErrorDialogs,
105+
stickyAuth: stickyAuth,
106+
sensitiveTransaction: sensitiveTransaction,
107+
biometricOnly: biometricOnly,
108+
),
109+
);
125110
}
126111

127112
/// Returns true if auth was cancelled successfully.
@@ -131,52 +116,30 @@ class LocalAuthentication {
131116
/// Returns [Future] bool true or false:
132117
Future<bool> stopAuthentication() async {
133118
if (_platform.isAndroid) {
134-
return await _channel.invokeMethod<bool>('stopAuthentication') ?? false;
119+
return LocalAuthPlatform.instance.stopAuthentication();
135120
}
136121
return true;
137122
}
138123

139124
/// Returns true if device is capable of checking biometrics
140125
///
141126
/// Returns a [Future] bool true or false:
142-
Future<bool> get canCheckBiometrics async =>
143-
(await _channel.invokeListMethod<String>('getAvailableBiometrics'))!
144-
.isNotEmpty;
127+
Future<bool> get canCheckBiometrics =>
128+
LocalAuthPlatform.instance.deviceSupportsBiometrics();
145129

146130
/// Returns true if device is capable of checking biometrics or is able to
147131
/// fail over to device credentials.
148132
///
149133
/// Returns a [Future] bool true or false:
150134
Future<bool> isDeviceSupported() async =>
151-
(await _channel.invokeMethod<bool>('isDeviceSupported')) ?? false;
135+
LocalAuthPlatform.instance.isDeviceSupported();
152136

153137
/// Returns a list of enrolled biometrics
154138
///
155139
/// Returns a [Future] List<BiometricType> with the following possibilities:
156140
/// - BiometricType.face
157141
/// - BiometricType.fingerprint
158142
/// - BiometricType.iris (not yet implemented)
159-
Future<List<BiometricType>> getAvailableBiometrics() async {
160-
final List<String> result = (await _channel.invokeListMethod<String>(
161-
'getAvailableBiometrics',
162-
)) ??
163-
<String>[];
164-
final List<BiometricType> biometrics = <BiometricType>[];
165-
for (final String value in result) {
166-
switch (value) {
167-
case 'face':
168-
biometrics.add(BiometricType.face);
169-
break;
170-
case 'fingerprint':
171-
biometrics.add(BiometricType.fingerprint);
172-
break;
173-
case 'iris':
174-
biometrics.add(BiometricType.iris);
175-
break;
176-
case 'undefined':
177-
break;
178-
}
179-
}
180-
return biometrics;
181-
}
143+
Future<List<BiometricType>> getAvailableBiometrics() =>
144+
LocalAuthPlatform.instance.getEnrolledBiometrics();
182145
}

packages/local_auth/local_auth/pubspec.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ repository: https://github.com/flutter/plugins/tree/main/packages/local_auth/loc
55
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+local_auth%22
66
version: 1.1.11
77

8+
# Temporarily disable publishing to allow moving Android and iOS
9+
# implementations.
10+
publish_to: none
11+
812
environment:
913
sdk: ">=2.14.0 <3.0.0"
10-
flutter: ">=2.5.0"
14+
flutter: ">=2.8.0"
1115

1216
flutter:
1317
plugin:
@@ -23,6 +27,7 @@ dependencies:
2327
sdk: flutter
2428
flutter_plugin_android_lifecycle: ^2.0.1
2529
intl: ^0.17.0
30+
local_auth_platform_interface: ^1.0.1
2631
platform: ^3.0.0
2732

2833
dev_dependencies:
@@ -32,4 +37,4 @@ dev_dependencies:
3237
sdk: flutter
3338
integration_test:
3439
sdk: flutter
35-
pedantic: ^1.10.0
40+
mockito: ^5.1.0

0 commit comments

Comments
 (0)