Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: BAD_DECRYPT Exception on invalid cipher #1730

Merged
merged 7 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment here on the kind of exception it was (SSL related, possibly on old devices ?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment

/// 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 @@ -82,6 +82,7 @@ Future<bool> _init1() async {
system: Platform.operatingSystemVersion,
url: 'https://world.openfoodfacts.org/',
);
await UserManagementProvider.mountCredentials();
_userPreferences = await UserPreferences.getUserPreferences();
_localDatabase = await LocalDatabase.getLocalDatabase();
_productPreferences = ProductPreferences(
Expand Down Expand Up @@ -178,6 +179,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 @@ -227,8 +229,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