stack_wallet/lib/utilities/biometrics.dart

93 lines
2.5 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/local_auth.dart';
2024-09-16 22:09:34 +00:00
import 'package:local_auth_android/local_auth_android.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
// debugPrint("canCheckBiometrics: $canCheckBiometrics");
// debugPrint("isDeviceSupported: $isDeviceSupported");
2022-08-26 08:11:35 +00:00
if (canCheckBiometrics && isDeviceSupported) {
2024-09-20 20:46:49 +00:00
List<BiometricType> availableSystems =
2022-08-26 08:11:35 +00:00
await localAuth.getAvailableBiometrics();
2024-09-20 20:46:49 +00:00
Logging.instance.log(
"Bio availableSystems: $availableSystems",
level: LogLevel.Info,
);
2022-08-26 08:11:35 +00:00
//TODO properly handle caught exceptions
2024-09-20 20:46:49 +00:00
try {
final bool didAuthenticate = await localAuth.authenticate(
localizedReason: localizedReason,
options: const AuthenticationOptions(
stickyAuth: true,
biometricOnly: true,
),
authMessages: <AuthMessages>[
AndroidAuthMessages(
biometricHint: "",
cancelButton: cancelButtonText,
signInTitle: title,
),
2024-09-20 20:46:49 +00:00
],
);
2022-08-26 08:11:35 +00:00
2024-09-20 20:46:49 +00:00
if (didAuthenticate) {
return true;
2022-08-26 08:11:35 +00:00
}
2024-09-20 20:46:49 +00:00
} catch (e) {
Logging.instance.log(
"local_auth exception caught in Biometrics.authenticate(), e: $e",
level: LogLevel.Error,
);
2022-08-26 08:11:35 +00:00
}
}
// authentication failed
return false;
}
}