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 4 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
26 changes: 22 additions & 4 deletions packages/smooth_app/lib/data_models/user_management_provider.dart
Original file line number Diff line number Diff line change
@@ -1,4 +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';
Expand Down Expand Up @@ -38,9 +40,25 @@ 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);
Future<void> mountCredentials(BuildContext context) 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);
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) {
_finishedLoading = true;
Expand Down
4 changes: 4 additions & 0 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 0 additions & 2 deletions packages/smooth_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,6 @@ class SmoothAppGetLanguage extends StatelessWidget {
final LocalDatabase _localDatabase = context.read<LocalDatabase>();
AnalyticsHelper.trackStart(_localDatabase, context);

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

return appWidget;
}
}
7 changes: 7 additions & 0 deletions packages/smooth_app/lib/pages/page_manager.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -58,6 +60,11 @@ class PageManagerState extends State<PageManager> {
_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<UserManagementProvider>().mountCredentials(context));

final ThemeData themeData = Theme.of(context);
final bool brightnessCheck = themeData.brightness == Brightness.light;
return WillPopScope(
Expand Down