π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
mobile/photos: sync security toggle state from user details
Neeraj Gupta77aa11b10593
diff --git a/mobile/apps/photos/lib/events/two_factor_status_change_event.dart b/mobile/apps/photos/lib/events/two_factor_status_change_event.dartdeleted file mode 100644index 0419d1787c..0000000000--- a/mobile/apps/photos/lib/events/two_factor_status_change_event.dart+++ /dev/null@@ -1,7 +0,0 @@-import 'package:photos/events/event.dart';--class TwoFactorStatusChangeEvent extends Event {- final bool status;-- TwoFactorStatusChangeEvent(this.status);-}diff --git a/mobile/apps/photos/lib/services/account/user_service.dart b/mobile/apps/photos/lib/services/account/user_service.dartindex c87f07a6da..f47b136ebe 100644--- a/mobile/apps/photos/lib/services/account/user_service.dart+++ b/mobile/apps/photos/lib/services/account/user_service.dart@@ -14,7 +14,6 @@ import 'package:photos/core/configuration.dart';import 'package:photos/core/constants.dart';import 'package:photos/core/event_bus.dart';import "package:photos/events/account_configured_event.dart";-import 'package:photos/events/two_factor_status_change_event.dart';import 'package:photos/events/user_details_changed_event.dart';import 'package:photos/gateways/users/models/delete_account.dart';import 'package:photos/gateways/users/models/key_attributes.dart';@@ -67,8 +66,6 @@ class UserService {final _logger = Logger((UserService).toString());final _config = Configuration.instance;late SharedPreferences _preferences;- StreamSubscription<TwoFactorStatusChangeEvent>?- _twoFactorStatusChangeSubscription;UsersGateway get _gateway => usersGateway;@@ -89,13 +86,6 @@ class UserService {() => {setTwoFactor(fetchTwoFactorStatus: true).ignore()},);}- if (_twoFactorStatusChangeSubscription != null) {- await _twoFactorStatusChangeSubscription!.cancel();- }- _twoFactorStatusChangeSubscription =- Bus.instance.on<TwoFactorStatusChangeEvent>().listen((event) {- setTwoFactor(value: event.status);- });}Future<void> sendOtt(@@ -208,14 +198,31 @@ class UserService {bool shouldCache = true,}) async {_logger.info("Fetching user details");+ final bool previousEmailMFAStatus = hasEmailMFAEnabled();+ final bool previousTwoFactorStatus = hasEnabledTwoFactor();final userDetails = await _gateway.getUserDetails(memoryCount: memoryCount);if (shouldCache) {await _preferences.setString(keyUserDetails, userDetails.toJson());+ bool hasSecurityStatusChanged = false;if (userDetails.profileData != null) {+ final bool currentEmailMFAStatus =+ userDetails.profileData!.isEmailMFAEnabled;await _preferences.setBool(kIsEmailMFAEnabled,- userDetails.profileData!.isEmailMFAEnabled,+ currentEmailMFAStatus,);+ hasSecurityStatusChanged =+ hasSecurityStatusChanged ||+ previousEmailMFAStatus != currentEmailMFAStatus;+ final bool currentTwoFactorStatus =+ userDetails.profileData!.isTwoFactorEnabled;+ await setTwoFactor(value: currentTwoFactorStatus);+ hasSecurityStatusChanged =+ hasSecurityStatusChanged ||+ previousTwoFactorStatus != currentTwoFactorStatus;+ }+ if (hasSecurityStatusChanged) {+ Bus.instance.fire(UserDetailsChangedEvent());}// handle email change from different clientif (userDetails.email != _config.getEmail()) {@@ -1019,9 +1026,15 @@ class UserService {twoFactorSecretDecryptionNonce:CryptoUtil.bin2base64(encryptionResult.nonce!),);+ await setTwoFactor(value: true);+ final UserDetails? profile = getCachedUserDetails();+ if (profile != null && profile.profileData != null) {+ profile.profileData!.isTwoFactorEnabled = true;+ await _preferences.setString(keyUserDetails, profile.toJson());+ }await dialog.hide();Navigator.pop(context);- Bus.instance.fire(TwoFactorStatusChangeEvent(true));+ Bus.instance.fire(UserDetailsChangedEvent());return true;} catch (e, s) {await dialog.hide();@@ -1059,8 +1072,14 @@ class UserService {await dialog.show();try {await _gateway.disableTwoFactor();+ await setTwoFactor(value: false);+ final UserDetails? profile = getCachedUserDetails();+ if (profile != null && profile.profileData != null) {+ profile.profileData!.isTwoFactorEnabled = false;+ await _preferences.setString(keyUserDetails, profile.toJson());+ }await dialog.hide();- Bus.instance.fire(TwoFactorStatusChangeEvent(false));+ Bus.instance.fire(UserDetailsChangedEvent());showShortToast(context,AppLocalizations.of(context).twofactorAuthenticationHasBeenDisabled,@@ -1080,8 +1099,17 @@ class UserService {Future<bool> fetchTwoFactorStatus() async {try {+ final previousStatus = hasEnabledTwoFactor();final status = await _gateway.getTwoFactorStatus();await setTwoFactor(value: status);+ final UserDetails? profile = getCachedUserDetails();+ if (profile != null && profile.profileData != null) {+ profile.profileData!.isTwoFactorEnabled = status;+ await _preferences.setString(keyUserDetails, profile.toJson());+ }+ if (previousStatus != status) {+ Bus.instance.fire(UserDetailsChangedEvent());+ }return status;} catch (e) {_logger.severe("Failed to fetch 2FA status", e);@@ -1185,6 +1213,7 @@ class UserService {profile.profileData!.isEmailMFAEnabled = isEnabled;await _preferences.setString(keyUserDetails, profile.toJson());}+ Bus.instance.fire(UserDetailsChangedEvent());} catch (e) {_logger.severe("Failed to update email mfa", e);rethrow;diff --git a/mobile/apps/photos/lib/ui/settings/security/security_settings_page.dart b/mobile/apps/photos/lib/ui/settings/security/security_settings_page.dartindex 5f6cf63852..8fc00dddc5 100644--- a/mobile/apps/photos/lib/ui/settings/security/security_settings_page.dart+++ b/mobile/apps/photos/lib/ui/settings/security/security_settings_page.dart@@ -9,7 +9,7 @@ import "package:local_auth/local_auth.dart";import "package:logging/logging.dart";import "package:photos/core/configuration.dart";import "package:photos/core/event_bus.dart";-import "package:photos/events/two_factor_status_change_event.dart";+import "package:photos/events/user_details_changed_event.dart";import "package:photos/generated/l10n.dart";import "package:photos/l10n/l10n.dart";import "package:photos/models/user_details.dart";@@ -36,24 +36,24 @@ class SecuritySettingsPage extends StatefulWidget {class _SecuritySettingsPageState extends State<SecuritySettingsPage> {final _config = Configuration.instance;- late StreamSubscription<TwoFactorStatusChangeEvent>- _twoFactorStatusChangeEvent;+ late StreamSubscription<UserDetailsChangedEvent> _userDetailsChangedEvent;final Logger _logger = Logger("SecuritySettingsPage");@overridevoid initState() {super.initState();- _twoFactorStatusChangeEvent =- Bus.instance.on<TwoFactorStatusChangeEvent>().listen((event) async {+ _userDetailsChangedEvent =+ Bus.instance.on<UserDetailsChangedEvent>().listen((event) async {if (mounted) {setState(() {});}});+ _refreshSecurityDetails().ignore();}@overridevoid dispose() {- _twoFactorStatusChangeEvent.cancel();+ _userDetailsChangedEvent.cancel();super.dispose();}@@ -178,6 +178,20 @@ class _SecuritySettingsPageState extends State<SecuritySettingsPage> {);}+ Future<void> _refreshSecurityDetails() async {+ if (!_config.hasConfiguredAccount() || isOfflineMode) {+ return;+ }+ try {+ await UserService.instance.getUserDetailsV2(memoryCount: false);+ if (mounted) {+ setState(() {});+ }+ } catch (e, s) {+ _logger.warning("failed to refresh security details", e, s);+ }+ }+Future<void> _onTwoFactorToggle(BuildContext context) async {final completer = Completer();final hasAuthenticated =diff --git a/mobile/apps/photos/lib/ui/settings/security_section_widget.dart b/mobile/apps/photos/lib/ui/settings/security_section_widget.dartindex 255ea2843f..1a54c801e2 100644--- a/mobile/apps/photos/lib/ui/settings/security_section_widget.dart+++ b/mobile/apps/photos/lib/ui/settings/security_section_widget.dart@@ -9,10 +9,11 @@ import "package:logging/logging.dart";import 'package:photos/core/configuration.dart';import 'package:photos/core/event_bus.dart';import 'package:photos/ente_theme_data.dart';-import 'package:photos/events/two_factor_status_change_event.dart';+import 'package:photos/events/user_details_changed_event.dart';import "package:photos/generated/l10n.dart";import "package:photos/l10n/l10n.dart";import "package:photos/models/user_details.dart";+import "package:photos/service_locator.dart";import "package:photos/services/account/passkey_service.dart";import 'package:photos/services/account/user_service.dart';import 'package:photos/services/local_authentication_service.dart';@@ -38,23 +39,23 @@ class SecuritySectionWidget extends StatefulWidget {class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {final _config = Configuration.instance;- late StreamSubscription<TwoFactorStatusChangeEvent>- _twoFactorStatusChangeEvent;+ late StreamSubscription<UserDetailsChangedEvent> _userDetailsChangedEvent;final Logger _logger = Logger('SecuritySectionWidget');@overridevoid initState() {super.initState();- _twoFactorStatusChangeEvent =- Bus.instance.on<TwoFactorStatusChangeEvent>().listen((event) async {+ _userDetailsChangedEvent =+ Bus.instance.on<UserDetailsChangedEvent>().listen((event) async {if (mounted) {setState(() {});}});+ _refreshSecurityDetails().ignore();}@overridevoid dispose() {- _twoFactorStatusChangeEvent.cancel();+ _userDetailsChangedEvent.cancel();super.dispose();}@@ -216,6 +217,20 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {);}+ Future<void> _refreshSecurityDetails() async {+ if (!_config.hasConfiguredAccount() || isOfflineMode) {+ return;+ }+ try {+ await UserService.instance.getUserDetailsV2(memoryCount: false);+ if (mounted) {+ setState(() {});+ }+ } catch (e, s) {+ _logger.warning("failed to refresh security details", e, s);+ }+ }+Future<void> _disableTwoFactor() async {final AlertDialog alert = AlertDialog(title: Text(AppLocalizations.of(context).disableTwofactor),