flutter build apk warned that file_picker and shared_preferences_android apply the Kotlin Gradle Plugin themselves, and that a future Flutter will refuse to build an app whose plugins do. Both have versions that let Flutter's built-in Kotlin do it instead; neither of them is a version bump on its own. shared_preferences_android was free — 2.4.27 is inside the constraint that was already there and only pub.lock was holding it back. file_picker is not: 10 and 11 both apply KGP, so 12 is the floor, and 12 split into federated packages whose windows one wants win32 ^6, which flutter_secure_storage 9 forbids. So the fix reaches flutter_secure_storage, and that is the part worth reading twice. v11 satisfies win32 but its changelog is explicit: data written by a version before v10 is unusable after it, because v10 is what migrates the Jetpack Security (EncryptedSharedPreferences) backend Google deprecated to the package's own ciphers. Going 9 to 11 in one step would leave the stored credentials unreadable and quietly switch biometric login off for anyone who had it on. v10 satisfies win32 ^6 just as well, so the constraint is pinned below 11 with the reason written down: once a build carrying v10 has run on every device that had biometric login enabled, the ceiling can go. encryptedSharedPreferences: true goes with it — v10 ignores the parameter and migrates on first access, and v11 has removed it. file_picker 12's API is smaller and the call sites got smaller with it. FilePicker.platform.pickFiles returning a result whose files list had to be checked for emptiness becomes FilePicker.pickFile returning one nullable file, which is what both callers wanted. PlatformFile.bytes (populated only when withData was asked for) becomes readAsBytes(), so the "bytes, or read the path, or give up" ladder both callers carried is one await — and the give-up branch that raised errors.noFile and the import's notJson is gone, because a file that was picked can now always be read. Verified: flutter analyze is clean and flutter test still passes 32. flutter build apk --debug succeeds and prints no KGP warning, where the build before this named both plugins. Not verified: nothing was exercised on a device — the phone came off USB before the reinstall, so this APK has not run. The two things to try first are the ones that changed under the picker: attach a PDF to a service record, and Settings, data, import a previously exported JSON. Biometric login is the third — it should survive, since v10 migrates rather than resets, but a device that had it on is the only place that claim can be checked, and if the migration does fail the app treats it as stale credentials and asks for the password. Android is the only target built; the win32 bump underneath is untested because this app has no windows/ folder to build. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
120 lines
4.5 KiB
Dart
120 lines
4.5 KiB
Dart
import "package:flutter/services.dart";
|
|
import "package:flutter_secure_storage/flutter_secure_storage.dart";
|
|
import "package:local_auth/local_auth.dart";
|
|
import "package:local_auth_android/local_auth_android.dart";
|
|
|
|
/// Biometric ("Face recognition" / "Fingerprint") sign-in.
|
|
///
|
|
/// The app already persists a JWT in SharedPreferences, so a valid session
|
|
/// auto-restores on boot and the login screen is only shown after logout or
|
|
/// token expiry. Biometric login covers that case: the user's credentials are
|
|
/// kept in Android Keystore-backed secure storage and released only after the
|
|
/// system BiometricPrompt succeeds, then replayed against the normal login API
|
|
/// (so each biometric sign-in mints a fresh session/token).
|
|
class BiometricAuth {
|
|
final LocalAuthentication _auth = LocalAuthentication();
|
|
// Default options: the Jetpack Security backend this used to ask for by name
|
|
// is deprecated by Google and gone in flutter_secure_storage v11, and v10
|
|
// ignores the parameter and migrates what is already stored to its own ciphers
|
|
// on first access. Which is why this app is pinned below v11 — going straight
|
|
// there from v9 would skip that migration and leave the saved credentials
|
|
// unreadable, quietly switching biometric login off for anyone who had it on.
|
|
// Once a build carrying v10 has run on a device, v11 is a free bump.
|
|
final FlutterSecureStorage _store = const FlutterSecureStorage();
|
|
|
|
static const _emailKey = "cc_bio_email";
|
|
static const _passwordKey = "cc_bio_password";
|
|
|
|
/// Synchronous mirror of [isEnabled], kept fresh by the async calls below so
|
|
/// the app-lifecycle handler (which can't await) can decide whether to lock.
|
|
bool enabledCached = false;
|
|
|
|
/// Whether the device has usable biometric hardware with something enrolled.
|
|
Future<bool> isAvailable() async {
|
|
try {
|
|
if (!await _auth.isDeviceSupported()) return false;
|
|
return await _auth.canCheckBiometrics;
|
|
} on PlatformException {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// Which biometric kinds are enrolled (used to label the sign-in buttons).
|
|
Future<List<BiometricType>> availableTypes() async {
|
|
try {
|
|
return await _auth.getAvailableBiometrics();
|
|
} on PlatformException {
|
|
return const [];
|
|
}
|
|
}
|
|
|
|
Future<bool> hasFace() async =>
|
|
(await availableTypes()).contains(BiometricType.face);
|
|
|
|
Future<bool> hasFingerprint() async =>
|
|
(await availableTypes()).contains(BiometricType.fingerprint);
|
|
|
|
/// The email biometric login was enabled for, or null if it's off.
|
|
Future<String?> enabledEmail() async {
|
|
final v = await _store.read(key: _emailKey);
|
|
enabledCached = v != null;
|
|
return v;
|
|
}
|
|
|
|
Future<bool> isEnabled() async => (await enabledEmail()) != null;
|
|
|
|
/// Remember credentials for biometric sign-in. Caller should only invoke this
|
|
/// after a successful password login so the stored creds are known-good.
|
|
Future<void> enable(String email, String password) async {
|
|
await _store.write(key: _emailKey, value: email);
|
|
await _store.write(key: _passwordKey, value: password);
|
|
enabledCached = true;
|
|
}
|
|
|
|
Future<void> disable() async {
|
|
await _store.delete(key: _emailKey);
|
|
await _store.delete(key: _passwordKey);
|
|
enabledCached = false;
|
|
}
|
|
|
|
/// Prompt for biometrics and, on success, return the stored (email, password).
|
|
/// Returns null if the user cancels or nothing is stored; throws
|
|
/// [BiometricException] with a friendly message on a hard error.
|
|
Future<(String, String)?> unlock({String? reason}) async {
|
|
bool ok;
|
|
try {
|
|
ok = await _auth.authenticate(
|
|
localizedReason: reason ?? "Sign in to Car Control",
|
|
options: const AuthenticationOptions(
|
|
biometricOnly: true,
|
|
stickyAuth: true,
|
|
),
|
|
authMessages: const [
|
|
AndroidAuthMessages(
|
|
signInTitle: "Car Control sign-in",
|
|
biometricHint: "",
|
|
cancelButton: "Use password",
|
|
),
|
|
],
|
|
);
|
|
} on PlatformException catch (e) {
|
|
throw BiometricException(e.message ?? "Biometric authentication failed.");
|
|
}
|
|
if (!ok) return null;
|
|
final email = await _store.read(key: _emailKey);
|
|
final password = await _store.read(key: _passwordKey);
|
|
if (email == null || password == null) return null;
|
|
return (email, password);
|
|
}
|
|
}
|
|
|
|
class BiometricException implements Exception {
|
|
final String message;
|
|
BiometricException(this.message);
|
|
@override
|
|
String toString() => message;
|
|
}
|
|
|
|
/// App-wide singleton (mirrors the other services in main.dart).
|
|
final biometricAuth = BiometricAuth();
|