From 83fbbcb2b32ef4f1401ed8004859a9964cd11ec8 Mon Sep 17 00:00:00 2001 From: Marvin M <39344769+M123-dev@users.noreply.github.com> Date: Mon, 2 May 2022 12:19:48 +0200 Subject: [PATCH 1/7] fix: BAD_DECRYPT Exception on invalid cipher --- .../lib/data_models/user_management_provider.dart | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/smooth_app/lib/data_models/user_management_provider.dart b/packages/smooth_app/lib/data_models/user_management_provider.dart index cf22d3d60f2..c289c878125 100644 --- a/packages/smooth_app/lib/data_models/user_management_provider.dart +++ b/packages/smooth_app/lib/data_models/user_management_provider.dart @@ -1,4 +1,5 @@ import 'package:flutter/cupertino.dart'; +import 'package:flutter/services.dart'; import 'package:openfoodfacts/openfoodfacts.dart'; import 'package:openfoodfacts/utils/OpenFoodAPIConfiguration.dart'; import 'package:smooth_app/database/dao_secured_string.dart'; @@ -39,8 +40,14 @@ class UserManagementProvider with ChangeNotifier { /// Mounts already stored credentials, called at app startup Future mountCredentials() async { - final String? userId = await DaoSecuredString.get(_USER_ID); - final String? password = await DaoSecuredString.get(_PASSWORD); + final String? userId; + final String? password; + try { + userId = await DaoSecuredString.get(_USER_ID); + password = await DaoSecuredString.get(_PASSWORD); + } on PlatformException { + return; + } if (userId == null || password == null) { _finishedLoading = true; From 4dbd53ded1375fa3d9cfab4fd3ae41aa5733d24f Mon Sep 17 00:00:00 2001 From: Marvin M <39344769+M123-dev@users.noreply.github.com> Date: Mon, 2 May 2022 12:23:06 +0200 Subject: [PATCH 2/7] Update user_management_provider.dart --- .../smooth_app/lib/data_models/user_management_provider.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/smooth_app/lib/data_models/user_management_provider.dart b/packages/smooth_app/lib/data_models/user_management_provider.dart index c289c878125..ad5648aa7f2 100644 --- a/packages/smooth_app/lib/data_models/user_management_provider.dart +++ b/packages/smooth_app/lib/data_models/user_management_provider.dart @@ -43,8 +43,8 @@ class UserManagementProvider with ChangeNotifier { final String? userId; final String? password; try { - userId = await DaoSecuredString.get(_USER_ID); - password = await DaoSecuredString.get(_PASSWORD); + userId = await DaoSecuredString.get(_USER_ID); + password = await DaoSecuredString.get(_PASSWORD); } on PlatformException { return; } From bc844604ce2b3a1c91ea2cd12270208896a29a98 Mon Sep 17 00:00:00 2001 From: Marvin M <39344769+M123-dev@users.noreply.github.com> Date: Tue, 3 May 2022 17:04:04 +0200 Subject: [PATCH 3/7] More comments + showing SnackBar --- .../data_models/user_management_provider.dart | 21 ++++++++++++++----- packages/smooth_app/lib/l10n/app_en.arb | 4 ++++ packages/smooth_app/lib/main.dart | 5 +++-- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/packages/smooth_app/lib/data_models/user_management_provider.dart b/packages/smooth_app/lib/data_models/user_management_provider.dart index ad5648aa7f2..4e97c1becd9 100644 --- a/packages/smooth_app/lib/data_models/user_management_provider.dart +++ b/packages/smooth_app/lib/data_models/user_management_provider.dart @@ -1,5 +1,6 @@ -import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:openfoodfacts/openfoodfacts.dart'; import 'package:openfoodfacts/utils/OpenFoodAPIConfiguration.dart'; import 'package:smooth_app/database/dao_secured_string.dart'; @@ -39,14 +40,24 @@ class UserManagementProvider with ChangeNotifier { } /// Mounts already stored credentials, called at app startup - Future mountCredentials() async { - final String? userId; - final String? password; + Future mountCredentials(BuildContext context) async { + String? userId; + String? password; + try { userId = await DaoSecuredString.get(_USER_ID); password = await DaoSecuredString.get(_PASSWORD); } on PlatformException { - return; + /// Decrypting the values can go wrong if, for example, the app was + /// manually overwritten from an external apk. + DaoSecuredString.remove(key: _USER_ID); + DaoSecuredString.remove(key: _PASSWORD); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text(AppLocalizations.of(context)!.you_have_been_logged_out), + duration: const Duration(seconds: 2), + ), + ); } if (userId == null || password == null) { diff --git a/packages/smooth_app/lib/l10n/app_en.arb b/packages/smooth_app/lib/l10n/app_en.arb index 30202d0790e..ff4ce3656e0 100644 --- a/packages/smooth_app/lib/l10n/app_en.arb +++ b/packages/smooth_app/lib/l10n/app_en.arb @@ -888,5 +888,9 @@ "user_list_name_error_same": "That is the same name", "@user_list_name_error_same": { "description": "Validation error about the renamed name that is the same as the initial list name" + }, + "you_have_been_logged_out": "You have been logged out", + "@you_have_been_logged_out": { + "description": "Hint when the user got automatically logged out for example for security reasons" } } \ No newline at end of file diff --git a/packages/smooth_app/lib/main.dart b/packages/smooth_app/lib/main.dart index f2131260796..58bdc673c4a 100644 --- a/packages/smooth_app/lib/main.dart +++ b/packages/smooth_app/lib/main.dart @@ -194,7 +194,8 @@ class _SmoothAppState extends State { themeProvider.colorTag, ), themeMode: themeProvider.currentThemeMode, - home: SmoothAppGetLanguage(appWidget), + // We need to wrap it inside a Scaffold to show error Snackbar's + home: Scaffold(body: SmoothAppGetLanguage(appWidget)), ); } @@ -227,7 +228,7 @@ class SmoothAppGetLanguage extends StatelessWidget { final LocalDatabase _localDatabase = context.read(); AnalyticsHelper.trackStart(_localDatabase, context); - context.read().mountCredentials(); + context.read().mountCredentials(context); return appWidget; } From d6d63f3bdd3c42ab54891757aea9d7d4b80e357b Mon Sep 17 00:00:00 2001 From: Marvin M <39344769+M123-dev@users.noreply.github.com> Date: Tue, 3 May 2022 17:57:23 +0200 Subject: [PATCH 4/7] Moving cred movment lower in the tree --- packages/smooth_app/lib/main.dart | 5 +---- packages/smooth_app/lib/pages/page_manager.dart | 7 +++++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/smooth_app/lib/main.dart b/packages/smooth_app/lib/main.dart index 58bdc673c4a..6a06b69cc5c 100644 --- a/packages/smooth_app/lib/main.dart +++ b/packages/smooth_app/lib/main.dart @@ -194,8 +194,7 @@ class _SmoothAppState extends State { themeProvider.colorTag, ), themeMode: themeProvider.currentThemeMode, - // We need to wrap it inside a Scaffold to show error Snackbar's - home: Scaffold(body: SmoothAppGetLanguage(appWidget)), + home: SmoothAppGetLanguage(appWidget), ); } @@ -228,8 +227,6 @@ class SmoothAppGetLanguage extends StatelessWidget { final LocalDatabase _localDatabase = context.read(); AnalyticsHelper.trackStart(_localDatabase, context); - context.read().mountCredentials(context); - return appWidget; } } diff --git a/packages/smooth_app/lib/pages/page_manager.dart b/packages/smooth_app/lib/pages/page_manager.dart index 7889c1dcc65..7e85ec10540 100644 --- a/packages/smooth_app/lib/pages/page_manager.dart +++ b/packages/smooth_app/lib/pages/page_manager.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; +import 'package:provider/provider.dart'; +import 'package:smooth_app/data_models/user_management_provider.dart'; import 'package:smooth_app/pages/scan/inherited_data_manager.dart'; import 'package:smooth_app/widgets/tab_navigator.dart'; @@ -58,6 +60,11 @@ class PageManagerState extends State { _buildOffstageNavigator(BottomNavigationTab.History), ]; + // Mounting the user credentials, needs to be done after build of the first + // Scaffold to be able to show a optional error Snackbar + WidgetsBinding.instance?.addPostFrameCallback((_) => + context.read().mountCredentials(context)); + final ThemeData themeData = Theme.of(context); final bool brightnessCheck = themeData.brightness == Brightness.light; return WillPopScope( From af0d5bd4b6f759c62963221699b8d5d3899a0d48 Mon Sep 17 00:00:00 2001 From: Marvin M <39344769+M123-dev@users.noreply.github.com> Date: Tue, 3 May 2022 18:37:23 +0200 Subject: [PATCH 5/7] Moved back --- .../lib/data_models/user_management_provider.dart | 10 ++-------- packages/smooth_app/lib/l10n/app_en.arb | 4 ---- packages/smooth_app/lib/main.dart | 2 ++ packages/smooth_app/lib/pages/page_manager.dart | 7 ------- 4 files changed, 4 insertions(+), 19 deletions(-) diff --git a/packages/smooth_app/lib/data_models/user_management_provider.dart b/packages/smooth_app/lib/data_models/user_management_provider.dart index 4e97c1becd9..3b0c317cdaa 100644 --- a/packages/smooth_app/lib/data_models/user_management_provider.dart +++ b/packages/smooth_app/lib/data_models/user_management_provider.dart @@ -1,6 +1,5 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; -import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:openfoodfacts/openfoodfacts.dart'; import 'package:openfoodfacts/utils/OpenFoodAPIConfiguration.dart'; import 'package:smooth_app/database/dao_secured_string.dart'; @@ -40,7 +39,7 @@ class UserManagementProvider with ChangeNotifier { } /// Mounts already stored credentials, called at app startup - Future mountCredentials(BuildContext context) async { + Future mountCredentials() async { String? userId; String? password; @@ -52,12 +51,7 @@ class UserManagementProvider with ChangeNotifier { /// manually overwritten from an external apk. DaoSecuredString.remove(key: _USER_ID); DaoSecuredString.remove(key: _PASSWORD); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text(AppLocalizations.of(context)!.you_have_been_logged_out), - duration: const Duration(seconds: 2), - ), - ); + debugPrint('Credentials query failed, you have been logged out'); } if (userId == null || password == null) { diff --git a/packages/smooth_app/lib/l10n/app_en.arb b/packages/smooth_app/lib/l10n/app_en.arb index ff4ce3656e0..30202d0790e 100644 --- a/packages/smooth_app/lib/l10n/app_en.arb +++ b/packages/smooth_app/lib/l10n/app_en.arb @@ -888,9 +888,5 @@ "user_list_name_error_same": "That is the same name", "@user_list_name_error_same": { "description": "Validation error about the renamed name that is the same as the initial list name" - }, - "you_have_been_logged_out": "You have been logged out", - "@you_have_been_logged_out": { - "description": "Hint when the user got automatically logged out for example for security reasons" } } \ No newline at end of file diff --git a/packages/smooth_app/lib/main.dart b/packages/smooth_app/lib/main.dart index 6a06b69cc5c..f2131260796 100644 --- a/packages/smooth_app/lib/main.dart +++ b/packages/smooth_app/lib/main.dart @@ -227,6 +227,8 @@ class SmoothAppGetLanguage extends StatelessWidget { final LocalDatabase _localDatabase = context.read(); AnalyticsHelper.trackStart(_localDatabase, context); + context.read().mountCredentials(); + return appWidget; } } diff --git a/packages/smooth_app/lib/pages/page_manager.dart b/packages/smooth_app/lib/pages/page_manager.dart index 7e85ec10540..7889c1dcc65 100644 --- a/packages/smooth_app/lib/pages/page_manager.dart +++ b/packages/smooth_app/lib/pages/page_manager.dart @@ -1,7 +1,5 @@ import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; -import 'package:provider/provider.dart'; -import 'package:smooth_app/data_models/user_management_provider.dart'; import 'package:smooth_app/pages/scan/inherited_data_manager.dart'; import 'package:smooth_app/widgets/tab_navigator.dart'; @@ -60,11 +58,6 @@ class PageManagerState extends State { _buildOffstageNavigator(BottomNavigationTab.History), ]; - // Mounting the user credentials, needs to be done after build of the first - // Scaffold to be able to show a optional error Snackbar - WidgetsBinding.instance?.addPostFrameCallback((_) => - context.read().mountCredentials(context)); - final ThemeData themeData = Theme.of(context); final bool brightnessCheck = themeData.brightness == Brightness.light; return WillPopScope( From 6eaf3ecd7ad7476ae9a058f9c1ce235d3f2c5453 Mon Sep 17 00:00:00 2001 From: Marvin M <39344769+M123-dev@users.noreply.github.com> Date: Wed, 4 May 2022 12:49:36 +0200 Subject: [PATCH 6/7] Await user credentials on app start --- .../lib/data_models/user_management_provider.dart | 13 +++---------- packages/smooth_app/lib/main.dart | 4 ++-- .../lib/pages/user_preferences_profile.dart | 10 ++++------ 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/packages/smooth_app/lib/data_models/user_management_provider.dart b/packages/smooth_app/lib/data_models/user_management_provider.dart index 3b0c317cdaa..0fd253c3044 100644 --- a/packages/smooth_app/lib/data_models/user_management_provider.dart +++ b/packages/smooth_app/lib/data_models/user_management_provider.dart @@ -6,11 +6,7 @@ import 'package:smooth_app/database/dao_secured_string.dart'; class UserManagementProvider with ChangeNotifier { static const String _USER_ID = 'user_id'; - static const String _PASSWORD = 'pasword'; - - bool _finishedLoading = false; - - bool get isLoading => !_finishedLoading; + static const String _PASSWORD = 'password'; /// Checks credentials and conditionally saves them Future login(User user) async { @@ -23,6 +19,7 @@ class UserManagementProvider with ChangeNotifier { if (rightCredentials) { await putUser(user); + notifyListeners(); } return rightCredentials && await credentialsInStorage(); @@ -39,7 +36,7 @@ class UserManagementProvider with ChangeNotifier { } /// Mounts already stored credentials, called at app startup - Future mountCredentials() async { + static Future mountCredentials() async { String? userId; String? password; @@ -55,15 +52,11 @@ class UserManagementProvider with ChangeNotifier { } if (userId == null || password == null) { - _finishedLoading = true; - notifyListeners(); return; } final User user = User(userId: userId, password: password); OpenFoodAPIConfiguration.globalUser = user; - _finishedLoading = true; - notifyListeners(); } /// Checks if any credentials exist in storage diff --git a/packages/smooth_app/lib/main.dart b/packages/smooth_app/lib/main.dart index f2131260796..5815cb0af87 100644 --- a/packages/smooth_app/lib/main.dart +++ b/packages/smooth_app/lib/main.dart @@ -82,6 +82,7 @@ Future _init1() async { system: Platform.operatingSystemVersion, url: 'https://world.openfoodfacts.org/', ); + await UserManagementProvider.mountCredentials(); _userPreferences = await UserPreferences.getUserPreferences(); _localDatabase = await LocalDatabase.getLocalDatabase(); _productPreferences = ProductPreferences( @@ -178,6 +179,7 @@ class _SmoothAppState extends State { final ThemeProvider themeProvider = context.watch(); final Widget appWidget = OnboardingFlowNavigator(_userPreferences) .getPageWidget(context, _userPreferences.lastVisitedOnboardingPage); + return MaterialApp( localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, @@ -227,8 +229,6 @@ class SmoothAppGetLanguage extends StatelessWidget { final LocalDatabase _localDatabase = context.read(); AnalyticsHelper.trackStart(_localDatabase, context); - context.read().mountCredentials(); - return appWidget; } } diff --git a/packages/smooth_app/lib/pages/user_preferences_profile.dart b/packages/smooth_app/lib/pages/user_preferences_profile.dart index 77c056279d4..9063b1b97e1 100644 --- a/packages/smooth_app/lib/pages/user_preferences_profile.dart +++ b/packages/smooth_app/lib/pages/user_preferences_profile.dart @@ -109,8 +109,9 @@ class _UserPreferencesPageState extends State { @override Widget build(BuildContext context) { - final UserManagementProvider userManagementProvider = - context.watch(); + // We need to listen to reflect login's from outside of the preferences page + // e.g. question card, ... + context.watch(); final ThemeData theme = Theme.of(context); final AppLocalizations appLocalizations = AppLocalizations.of(context)!; @@ -118,10 +119,7 @@ class _UserPreferencesPageState extends State { final List result = []; - if (userManagementProvider.isLoading) { - //Loading - result.add(const Center(child: CircularProgressIndicator())); - } else if (OpenFoodAPIConfiguration.globalUser != null) { + if (OpenFoodAPIConfiguration.globalUser != null) { //Credentials final String userId = OpenFoodAPIConfiguration.globalUser!.userId; result.add( From 1a27153520a516b8b2602ef23f64b7de9792b6a2 Mon Sep 17 00:00:00 2001 From: Marvin M <39344769+M123-dev@users.noreply.github.com> Date: Wed, 4 May 2022 13:24:30 +0200 Subject: [PATCH 7/7] Update packages/smooth_app/lib/data_models/user_management_provider.dart Co-authored-by: monsieurtanuki --- .../smooth_app/lib/data_models/user_management_provider.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/smooth_app/lib/data_models/user_management_provider.dart b/packages/smooth_app/lib/data_models/user_management_provider.dart index 0fd253c3044..b98bef334e7 100644 --- a/packages/smooth_app/lib/data_models/user_management_provider.dart +++ b/packages/smooth_app/lib/data_models/user_management_provider.dart @@ -6,7 +6,7 @@ import 'package:smooth_app/database/dao_secured_string.dart'; class UserManagementProvider with ChangeNotifier { static const String _USER_ID = 'user_id'; - static const String _PASSWORD = 'password'; + static const String _PASSWORD = 'pasword'; /// Checks credentials and conditionally saves them Future login(User user) async {