Skip to content

Commit

Permalink
fix: BAD_DECRYPT Exception on invalid cipher (#1730)
Browse files Browse the repository at this point in the history
* fix: BAD_DECRYPT Exception on invalid cipher

* Update user_management_provider.dart

* More comments + showing SnackBar

* Moving cred movment lower in the tree

* Moved back

* Await user credentials on app start

* Update packages/smooth_app/lib/data_models/user_management_provider.dart

Co-authored-by: monsieurtanuki <fabrice_fontaine@hotmail.com>

Co-authored-by: monsieurtanuki <fabrice_fontaine@hotmail.com>
  • Loading branch information
M123-dev and monsieurtanuki authored May 4, 2022
1 parent f11488e commit 955895d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
29 changes: 17 additions & 12 deletions packages/smooth_app/lib/data_models/user_management_provider.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.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';
Expand All @@ -7,10 +8,6 @@ class UserManagementProvider with ChangeNotifier {
static const String _USER_ID = 'user_id';
static const String _PASSWORD = 'pasword';

bool _finishedLoading = false;

bool get isLoading => !_finishedLoading;

/// Checks credentials and conditionally saves them
Future<bool> login(User user) async {
final bool rightCredentials;
Expand All @@ -22,6 +19,7 @@ class UserManagementProvider with ChangeNotifier {

if (rightCredentials) {
await putUser(user);
notifyListeners();
}

return rightCredentials && await credentialsInStorage();
Expand All @@ -38,20 +36,27 @@ class UserManagementProvider with ChangeNotifier {
}

/// Mounts already stored credentials, called at app startup
Future<void> mountCredentials() async {
final String? userId = await DaoSecuredString.get(_USER_ID);
final String? password = await DaoSecuredString.get(_PASSWORD);
static Future<void> mountCredentials() async {
String? userId;
String? password;

try {
userId = await DaoSecuredString.get(_USER_ID);
password = await DaoSecuredString.get(_PASSWORD);
} on PlatformException {
/// 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);
debugPrint('Credentials query failed, you have been logged out');
}

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
Expand Down
4 changes: 2 additions & 2 deletions packages/smooth_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Future<bool> _init1() async {
system: Platform.operatingSystemVersion,
url: 'https://world.openfoodfacts.org/',
);
await UserManagementProvider.mountCredentials();
_userPreferences = await UserPreferences.getUserPreferences();
_localDatabase = await LocalDatabase.getLocalDatabase();
await _continuousScanModel.load(_localDatabase);
Expand Down Expand Up @@ -182,6 +183,7 @@ class _SmoothAppState extends State<SmoothApp> {
final ThemeProvider themeProvider = context.watch<ThemeProvider>();
final Widget appWidget = OnboardingFlowNavigator(_userPreferences)
.getPageWidget(context, _userPreferences.lastVisitedOnboardingPage);

return MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
Expand Down Expand Up @@ -231,8 +233,6 @@ class SmoothAppGetLanguage extends StatelessWidget {
final LocalDatabase _localDatabase = context.read<LocalDatabase>();
AnalyticsHelper.trackStart(_localDatabase, context);

context.read<UserManagementProvider>().mountCredentials();

return appWidget;
}
}
10 changes: 4 additions & 6 deletions packages/smooth_app/lib/pages/user_preferences_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,17 @@ class _UserPreferencesPageState extends State<UserPreferencesSection> {

@override
Widget build(BuildContext context) {
final UserManagementProvider userManagementProvider =
context.watch<UserManagementProvider>();
// We need to listen to reflect login's from outside of the preferences page
// e.g. question card, ...
context.watch<UserManagementProvider>();

final ThemeData theme = Theme.of(context);
final AppLocalizations appLocalizations = AppLocalizations.of(context)!;
final Size size = MediaQuery.of(context).size;

final List<Widget> result = <Widget>[];

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(
Expand Down

0 comments on commit 955895d

Please sign in to comment.