mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
**Category:** new-feature **User Impact:** Mobile users must confirm with Face ID, biometrics, or their device passcode before sending their Buzz identity to Desktop. **Problem:** A signed-in phone could send its full identity, including the `nsec`, to a desktop without fresh local verification. **Solution:** Require OS device authentication before opening the identity-recovery scanner, retain that authorization only for the active pairing session and short pairing window, and require fresh authentication again if it expires before the identity payload is sent. Normal app opening, identity import, and community removal remain unchanged. ## Screencasts | Enable Face ID | Use Face ID | | --- | --- | |  |  | <details> <summary>File changes</summary> **Android and iOS integration** - `mobile/android/app/build.gradle.kts` declares the AppCompat dependency required by the biometric activity theme. - `mobile/android/app/src/main/kotlin/xyz/block/buzz/mobile/MainActivity.kt` uses the activity type required by the system authentication prompt. - `mobile/android/app/src/main/res/values/styles.xml` and `mobile/android/app/src/main/res/values-night/styles.xml` use the compatible launch theme. - `mobile/ios/Podfile.lock` records the native local-authentication dependency. - `mobile/ios/Runner/Info.plist` explains why Buzz requests Face ID access. **Identity policy and pairing flow** - `mobile/lib/shared/security/sensitive_action_authorizer.dart` wraps OS authentication and maps platform errors to stable app-level outcomes. - `mobile/lib/shared/community/community.dart` and `mobile/lib/shared/community/community_storage.dart` persist the sensitive-action policy. - `mobile/lib/features/invites/invite_join_provider.dart` assigns the explicit policy for invite-created communities. - `mobile/lib/features/pairing/pairing_provider.dart` gates export, binds grants to the active community/session, reauthenticates expired grants, and clears grants on every terminal path. - `mobile/lib/features/pairing/pairing_page.dart` lets users choose biometric protection while importing an identity. - `mobile/lib/features/settings/settings_page.dart` wires pairing into settings. - `mobile/lib/features/settings/settings_page/connection_section.dart` authenticates before opening export recovery and bounds the foreground-resume wait. - `mobile/pubspec.yaml` and `mobile/pubspec.lock` add and lock `local_auth`. **Coverage** - `mobile/test/shared/security/sensitive_action_authorizer_test.dart` covers native result mapping, unsupported devices, and single-flight behavior. - `mobile/test/shared/community/community_test.dart` and `mobile/test/shared/community/community_storage_test.dart` cover policy defaults and persistence. - `mobile/test/features/invites/invite_join_provider_test.dart` covers the invite policy. - `mobile/test/features/pairing/pairing_page_test.dart` covers import protection controls. - `mobile/test/features/pairing/pairing_provider_test.dart` covers export/import authorization, stale/reset/concurrent guards, malformed payload cleanup, and no-export failure paths. - `mobile/test/features/settings/connection_section_test.dart` covers the tap gate, lifecycle resume, and timeout behavior. </details> ## Reproduction steps 1. Pair an identity into the mobile app. 2. Open Settings and choose “Send identity to desktop.” 3. Verify Face ID, biometrics, or the device passcode is required before the recovery scanner opens. 4. Cancel device authentication and verify the scanner does not open and no identity transfer begins. 5. Authenticate, scan a Desktop recovery code, confirm the SAS, and verify the identity transfer completes. ## Validation At `be5620f5f10aa6cc16e86a4f01f102f3d9aeef9b`: - `cd mobile && ../bin/flutter analyze` — no issues - `cd mobile && ../bin/flutter test` — 1,368 tests passed - `cd mobile/android && JAVA_HOME=$(/usr/libexec/java_home -v 21) ./gradlew app:assembleDebug` — debug APK assembled successfully --------- Signed-off-by: Taylor Ho <taylorkmho@gmail.com>
159 lines
5.2 KiB
Dart
159 lines
5.2 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:local_auth/local_auth.dart';
|
|
|
|
/// Coarse outcomes safe to use for control flow without retaining OS details.
|
|
enum DeviceAuthResult { success, cancelled, unavailable, lockedOut, failed }
|
|
|
|
abstract interface class SensitiveActionAuthorizer {
|
|
Future<DeviceAuthResult> authorizeIdentityAction({
|
|
required bool biometricOnly,
|
|
});
|
|
|
|
Future<DeviceAuthResult> authorizeBiometricProtection();
|
|
|
|
Future<List<BiometricType>> enrolledBiometrics();
|
|
}
|
|
|
|
class LocalSensitiveActionAuthorizer implements SensitiveActionAuthorizer {
|
|
LocalSensitiveActionAuthorizer([LocalAuthentication? authentication])
|
|
: _authentication = authentication ?? LocalAuthentication();
|
|
|
|
final LocalAuthentication _authentication;
|
|
|
|
@override
|
|
Future<DeviceAuthResult> authorizeIdentityAction({
|
|
required bool biometricOnly,
|
|
}) => _authorize(
|
|
localizedReason: 'Confirm sending your Buzz identity to desktop',
|
|
biometricOnly: biometricOnly,
|
|
);
|
|
|
|
@override
|
|
Future<List<BiometricType>> enrolledBiometrics() async {
|
|
try {
|
|
return await _authentication.getAvailableBiometrics();
|
|
} catch (_) {
|
|
return const [];
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<DeviceAuthResult> authorizeBiometricProtection() async {
|
|
try {
|
|
final availableBiometrics = await _authentication
|
|
.getAvailableBiometrics();
|
|
if (availableBiometrics.isEmpty) return DeviceAuthResult.unavailable;
|
|
return _authorize(
|
|
localizedReason: 'Enable biometrics for secure actions',
|
|
biometricOnly: true,
|
|
);
|
|
} on LocalAuthException catch (error) {
|
|
return _resultFor(error);
|
|
} catch (_) {
|
|
return DeviceAuthResult.failed;
|
|
}
|
|
}
|
|
|
|
Future<DeviceAuthResult> _authorize({
|
|
required String localizedReason,
|
|
required bool biometricOnly,
|
|
}) async {
|
|
try {
|
|
final supported = await _authentication.isDeviceSupported();
|
|
if (!supported) return DeviceAuthResult.unavailable;
|
|
final authenticated = await _authentication.authenticate(
|
|
localizedReason: localizedReason,
|
|
biometricOnly: biometricOnly,
|
|
sensitiveTransaction: true,
|
|
// iOS may briefly background the app while presenting Face ID. Keep
|
|
// this authorization alive across that system transition instead of
|
|
// returning a cancellation that forces the user to tap and retry.
|
|
persistAcrossBackgrounding: true,
|
|
);
|
|
return authenticated ? DeviceAuthResult.success : DeviceAuthResult.failed;
|
|
} on LocalAuthException catch (error) {
|
|
return _resultFor(error);
|
|
} catch (_) {
|
|
return DeviceAuthResult.failed;
|
|
}
|
|
}
|
|
|
|
static DeviceAuthResult _resultFor(LocalAuthException error) =>
|
|
switch (error.code) {
|
|
LocalAuthExceptionCode.userCanceled ||
|
|
LocalAuthExceptionCode.systemCanceled ||
|
|
LocalAuthExceptionCode.timeout => DeviceAuthResult.cancelled,
|
|
LocalAuthExceptionCode.temporaryLockout ||
|
|
LocalAuthExceptionCode.biometricLockout => DeviceAuthResult.lockedOut,
|
|
LocalAuthExceptionCode.noCredentialsSet ||
|
|
LocalAuthExceptionCode.noBiometricsEnrolled ||
|
|
LocalAuthExceptionCode.noBiometricHardware ||
|
|
LocalAuthExceptionCode.biometricHardwareTemporarilyUnavailable ||
|
|
LocalAuthExceptionCode.uiUnavailable => DeviceAuthResult.unavailable,
|
|
_ => DeviceAuthResult.failed,
|
|
};
|
|
}
|
|
|
|
final sensitiveActionAuthorizerProvider = Provider<SensitiveActionAuthorizer>((
|
|
ref,
|
|
) {
|
|
return LocalSensitiveActionAuthorizer();
|
|
});
|
|
|
|
final enrolledBiometricsProvider = FutureProvider<List<BiometricType>>((ref) {
|
|
return ref.watch(sensitiveActionAuthorizerProvider).enrolledBiometrics();
|
|
});
|
|
|
|
String biometricProtectionLabel(
|
|
TargetPlatform platform,
|
|
Iterable<BiometricType> enrolledBiometrics,
|
|
) {
|
|
if (platform == TargetPlatform.iOS) {
|
|
if (enrolledBiometrics.contains(BiometricType.face)) return 'Use Face ID';
|
|
if (enrolledBiometrics.contains(BiometricType.fingerprint)) {
|
|
return 'Use Touch ID';
|
|
}
|
|
}
|
|
return 'Use biometrics';
|
|
}
|
|
|
|
class SensitiveActionAuthorizationSession {
|
|
SensitiveActionAuthorizationSession(this._authorizer);
|
|
|
|
final SensitiveActionAuthorizer _authorizer;
|
|
Future<DeviceAuthResult>? _authorizationInFlight;
|
|
bool? _inFlightBiometricOnly;
|
|
|
|
Future<DeviceAuthResult> authorize({required bool biometricOnly}) async {
|
|
final inFlight = _authorizationInFlight;
|
|
if (inFlight != null) {
|
|
if (_inFlightBiometricOnly == biometricOnly) return inFlight;
|
|
await inFlight;
|
|
return authorize(biometricOnly: biometricOnly);
|
|
}
|
|
|
|
final authorization = _authorizer.authorizeIdentityAction(
|
|
biometricOnly: biometricOnly,
|
|
);
|
|
_authorizationInFlight = authorization;
|
|
_inFlightBiometricOnly = biometricOnly;
|
|
try {
|
|
final result = await authorization;
|
|
return result;
|
|
} finally {
|
|
if (identical(_authorizationInFlight, authorization)) {
|
|
_authorizationInFlight = null;
|
|
_inFlightBiometricOnly = null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
final sensitiveActionAuthorizationSessionProvider =
|
|
Provider<SensitiveActionAuthorizationSession>((ref) {
|
|
return SensitiveActionAuthorizationSession(
|
|
ref.watch(sensitiveActionAuthorizerProvider),
|
|
);
|
|
});
|