stack_wallet/lib/utilities/biometrics.dart

130 lines
3.8 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
2022-08-26 08:11:35 +00:00
import 'dart:io';
import 'package:local_auth/auth_strings.dart';
import 'package:local_auth/local_auth.dart';
2024-05-27 23:56:22 +00:00
import 'logger.dart';
2022-08-26 08:11:35 +00:00
class Biometrics {
static const integrationTestFlag =
bool.fromEnvironment("IS_INTEGRATION_TEST");
const Biometrics();
Future<bool> authenticate({
required String cancelButtonText,
required String localizedReason,
required String title,
}) async {
if (!(Platform.isIOS || Platform.isAndroid)) {
Logging.instance.log(
2024-05-27 23:56:22 +00:00
"Tried to use Biometrics.authenticate() on a platform that is not Android or iOS! ...returning false.",
level: LogLevel.Error,
);
2022-08-26 08:11:35 +00:00
return false;
}
if (integrationTestFlag) {
Logging.instance.log(
2024-05-27 23:56:22 +00:00
"Tried to use Biometrics.authenticate() during integration testing. Returning false.",
level: LogLevel.Warning,
);
2022-08-26 08:11:35 +00:00
return false;
}
final LocalAuthentication localAuth = LocalAuthentication();
final canCheckBiometrics = await localAuth.canCheckBiometrics;
final isDeviceSupported = await localAuth.isDeviceSupported();
2022-12-13 00:17:02 +00:00
//todo: check if print needed
// debugPrint("canCheckBiometrics: $canCheckBiometrics");
// debugPrint("isDeviceSupported: $isDeviceSupported");
2022-08-26 08:11:35 +00:00
if (canCheckBiometrics && isDeviceSupported) {
2024-05-27 23:56:22 +00:00
final List<BiometricType> availableSystems =
2022-08-26 08:11:35 +00:00
await localAuth.getAvailableBiometrics();
2022-12-13 00:17:02 +00:00
//todo: check if print needed
// debugPrint("availableSystems: $availableSystems");
2022-08-26 08:11:35 +00:00
//TODO properly handle caught exceptions
if (Platform.isIOS) {
if (availableSystems.contains(BiometricType.face)) {
try {
2024-05-27 23:56:22 +00:00
final bool didAuthenticate = await localAuth.authenticate(
2022-08-26 08:11:35 +00:00
biometricOnly: true,
localizedReason: localizedReason,
stickyAuth: true,
iOSAuthStrings: const IOSAuthMessages(),
);
if (didAuthenticate) {
return true;
}
} catch (e) {
Logging.instance.log(
2024-05-27 23:56:22 +00:00
"local_auth exception caught in Biometrics.authenticate(), e: $e",
level: LogLevel.Error,
);
2022-08-26 08:11:35 +00:00
}
} else if (availableSystems.contains(BiometricType.fingerprint)) {
try {
2024-05-27 23:56:22 +00:00
final bool didAuthenticate = await localAuth.authenticate(
2022-08-26 08:11:35 +00:00
biometricOnly: true,
localizedReason: localizedReason,
stickyAuth: true,
iOSAuthStrings: const IOSAuthMessages(),
);
if (didAuthenticate) {
return true;
}
} catch (e) {
Logging.instance.log(
2024-05-27 23:56:22 +00:00
"local_auth exception caught in Biometrics.authenticate(), e: $e",
level: LogLevel.Error,
);
2022-08-26 08:11:35 +00:00
}
}
} else if (Platform.isAndroid) {
if (availableSystems.contains(BiometricType.fingerprint)) {
try {
2024-05-27 23:56:22 +00:00
final bool didAuthenticate = await localAuth.authenticate(
2022-08-26 08:11:35 +00:00
biometricOnly: true,
localizedReason: localizedReason,
stickyAuth: true,
androidAuthStrings: AndroidAuthMessages(
biometricHint: "",
cancelButton: cancelButtonText,
signInTitle: title,
),
);
if (didAuthenticate) {
return true;
}
} catch (e) {
Logging.instance.log(
2024-05-27 23:56:22 +00:00
"local_auth exception caught in Biometrics.authenticate(), e: $e",
level: LogLevel.Error,
);
2022-08-26 08:11:35 +00:00
}
}
}
}
// authentication failed
return false;
}
}