2021-06-29 09:48:01 +00:00
|
|
|
import 'dart:async';
|
2023-08-14 08:35:11 +00:00
|
|
|
import "dart:typed_data";
|
2021-04-19 09:35:22 +00:00
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2024-03-07 12:49:38 +00:00
|
|
|
import "package:logging/logging.dart";
|
2021-04-19 09:35:22 +00:00
|
|
|
import 'package:photos/core/configuration.dart';
|
2021-06-29 09:48:01 +00:00
|
|
|
import 'package:photos/core/event_bus.dart';
|
2022-06-07 08:28:34 +00:00
|
|
|
import 'package:photos/ente_theme_data.dart';
|
2021-06-29 09:48:01 +00:00
|
|
|
import 'package:photos/events/two_factor_status_change_event.dart';
|
2023-04-06 05:26:50 +00:00
|
|
|
import "package:photos/generated/l10n.dart";
|
2024-03-06 08:56:00 +00:00
|
|
|
import "package:photos/l10n/l10n.dart";
|
2023-08-14 08:35:11 +00:00
|
|
|
import "package:photos/models/user_details.dart";
|
2024-04-23 06:24:24 +00:00
|
|
|
import 'package:photos/service_locator.dart';
|
2022-09-05 10:32:04 +00:00
|
|
|
import 'package:photos/services/local_authentication_service.dart';
|
2024-03-06 08:56:00 +00:00
|
|
|
import "package:photos/services/passkey_service.dart";
|
2021-04-19 09:35:22 +00:00
|
|
|
import 'package:photos/services/user_service.dart';
|
2022-10-21 13:37:57 +00:00
|
|
|
import 'package:photos/theme/ente_theme.dart';
|
2023-08-14 08:35:11 +00:00
|
|
|
import "package:photos/ui/account/request_pwd_verification_page.dart";
|
2022-07-01 14:18:05 +00:00
|
|
|
import 'package:photos/ui/account/sessions_page.dart';
|
2022-10-04 04:16:40 +00:00
|
|
|
import 'package:photos/ui/components/captioned_text_widget.dart';
|
2022-10-05 11:48:56 +00:00
|
|
|
import 'package:photos/ui/components/expandable_menu_item_widget.dart';
|
2023-01-31 12:21:57 +00:00
|
|
|
import 'package:photos/ui/components/menu_item_widget/menu_item_widget.dart';
|
2022-10-05 15:57:41 +00:00
|
|
|
import 'package:photos/ui/components/toggle_switch_widget.dart';
|
2022-03-08 19:43:51 +00:00
|
|
|
import 'package:photos/ui/settings/common_settings.dart';
|
2023-03-25 05:52:38 +00:00
|
|
|
import "package:photos/utils/crypto_util.dart";
|
2024-03-07 12:49:38 +00:00
|
|
|
import "package:photos/utils/dialog_util.dart";
|
2023-03-25 05:52:38 +00:00
|
|
|
import "package:photos/utils/navigation_util.dart";
|
2023-08-04 04:24:54 +00:00
|
|
|
import "package:photos/utils/toast_util.dart";
|
2021-04-19 09:35:22 +00:00
|
|
|
|
|
|
|
class SecuritySectionWidget extends StatefulWidget {
|
2022-12-27 18:03:40 +00:00
|
|
|
const SecuritySectionWidget({Key? key}) : super(key: key);
|
2021-04-19 09:35:22 +00:00
|
|
|
|
|
|
|
@override
|
2022-07-03 09:45:00 +00:00
|
|
|
State<SecuritySectionWidget> createState() => _SecuritySectionWidgetState();
|
2021-04-19 09:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
|
|
|
|
final _config = Configuration.instance;
|
|
|
|
|
2022-12-27 18:03:40 +00:00
|
|
|
late StreamSubscription<TwoFactorStatusChangeEvent>
|
|
|
|
_twoFactorStatusChangeEvent;
|
2024-03-07 12:49:38 +00:00
|
|
|
final Logger _logger = Logger('SecuritySectionWidget');
|
2021-04-19 09:35:22 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2021-06-29 09:48:01 +00:00
|
|
|
_twoFactorStatusChangeEvent =
|
|
|
|
Bus.instance.on<TwoFactorStatusChangeEvent>().listen((event) async {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_twoFactorStatusChangeEvent.cancel();
|
|
|
|
super.dispose();
|
2021-04-19 09:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-10-05 11:48:56 +00:00
|
|
|
return ExpandableMenuItemWidget(
|
2023-04-06 05:26:50 +00:00
|
|
|
title: S.of(context).security,
|
2022-10-05 11:48:56 +00:00
|
|
|
selectionOptionsWidget: _getSectionOptions(context),
|
|
|
|
leadingIcon: Icons.local_police_outlined,
|
2022-03-08 19:43:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _getSectionOptions(BuildContext context) {
|
2022-11-15 13:42:26 +00:00
|
|
|
final Completer completer = Completer();
|
2021-04-19 09:35:22 +00:00
|
|
|
final List<Widget> children = [];
|
|
|
|
if (_config.hasConfiguredAccount()) {
|
|
|
|
children.addAll(
|
|
|
|
[
|
2022-10-05 07:52:48 +00:00
|
|
|
sectionOptionSpacing,
|
2022-11-15 09:11:35 +00:00
|
|
|
MenuItemWidget(
|
2023-04-06 05:26:50 +00:00
|
|
|
captionedTextWidget: CaptionedTextWidget(
|
|
|
|
title: S.of(context).twofactor,
|
2022-11-15 09:11:35 +00:00
|
|
|
),
|
2022-12-06 18:03:39 +00:00
|
|
|
trailingWidget: ToggleSwitchWidget(
|
2022-11-16 12:20:02 +00:00
|
|
|
value: () => UserService.instance.hasEnabledTwoFactor(),
|
2022-11-15 09:11:35 +00:00
|
|
|
onChanged: () async {
|
|
|
|
final hasAuthenticated = await LocalAuthenticationService
|
|
|
|
.instance
|
|
|
|
.requestLocalAuthentication(
|
|
|
|
context,
|
2023-04-06 05:26:50 +00:00
|
|
|
S.of(context).authToConfigureTwofactorAuthentication,
|
2022-11-15 09:11:35 +00:00
|
|
|
);
|
2022-11-16 12:20:02 +00:00
|
|
|
final isTwoFactorEnabled =
|
|
|
|
UserService.instance.hasEnabledTwoFactor();
|
2022-11-15 09:11:35 +00:00
|
|
|
if (hasAuthenticated) {
|
|
|
|
if (isTwoFactorEnabled) {
|
2022-11-16 12:00:44 +00:00
|
|
|
await _disableTwoFactor();
|
2022-11-16 13:01:29 +00:00
|
|
|
completer.isCompleted ? null : completer.complete();
|
2022-11-15 09:11:35 +00:00
|
|
|
} else {
|
2022-11-15 13:42:26 +00:00
|
|
|
await UserService.instance
|
|
|
|
.setupTwoFactor(context, completer);
|
2022-11-15 09:11:35 +00:00
|
|
|
}
|
2022-11-15 13:42:26 +00:00
|
|
|
return completer.future;
|
2022-11-15 09:11:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2022-10-05 07:52:48 +00:00
|
|
|
),
|
2024-04-23 08:08:55 +00:00
|
|
|
if (flagService.passKeyEnabled) sectionOptionSpacing,
|
|
|
|
if (flagService.passKeyEnabled)
|
2024-03-06 08:56:00 +00:00
|
|
|
MenuItemWidget(
|
|
|
|
captionedTextWidget: CaptionedTextWidget(
|
|
|
|
title: context.l10n.passkey,
|
|
|
|
),
|
|
|
|
pressedColor: getEnteColorScheme(context).fillFaint,
|
|
|
|
trailingIcon: Icons.chevron_right_outlined,
|
|
|
|
trailingIconIsMuted: true,
|
2024-03-08 03:33:43 +00:00
|
|
|
onTap: () async => await onPasskeyClick(context),
|
2024-03-06 08:56:00 +00:00
|
|
|
),
|
2023-08-14 08:35:11 +00:00
|
|
|
sectionOptionSpacing,
|
|
|
|
MenuItemWidget(
|
|
|
|
captionedTextWidget: CaptionedTextWidget(
|
|
|
|
title: S.of(context).emailVerificationToggle,
|
|
|
|
),
|
|
|
|
trailingWidget: ToggleSwitchWidget(
|
|
|
|
value: () => UserService.instance.hasEmailMFAEnabled(),
|
|
|
|
onChanged: () async {
|
|
|
|
final hasAuthenticated = await LocalAuthenticationService
|
|
|
|
.instance
|
|
|
|
.requestLocalAuthentication(
|
|
|
|
context,
|
|
|
|
S.of(context).authToChangeEmailVerificationSetting,
|
|
|
|
);
|
|
|
|
final isEmailMFAEnabled =
|
|
|
|
UserService.instance.hasEmailMFAEnabled();
|
|
|
|
if (hasAuthenticated) {
|
|
|
|
await updateEmailMFA(!isEmailMFAEnabled);
|
|
|
|
}
|
|
|
|
},
|
2023-08-04 04:24:54 +00:00
|
|
|
),
|
2023-08-14 08:35:11 +00:00
|
|
|
),
|
2022-10-05 07:52:48 +00:00
|
|
|
sectionOptionSpacing,
|
2021-04-19 09:35:22 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
children.addAll([
|
2022-10-04 15:52:51 +00:00
|
|
|
MenuItemWidget(
|
2023-04-06 05:26:50 +00:00
|
|
|
captionedTextWidget: CaptionedTextWidget(
|
|
|
|
title: S.of(context).lockscreen,
|
2022-10-04 15:52:51 +00:00
|
|
|
),
|
2022-12-06 18:03:39 +00:00
|
|
|
trailingWidget: ToggleSwitchWidget(
|
2022-11-16 11:06:45 +00:00
|
|
|
value: () => _config.shouldShowLockScreen(),
|
2022-10-28 14:31:12 +00:00
|
|
|
onChanged: () async {
|
|
|
|
await LocalAuthenticationService.instance
|
2022-10-04 15:52:51 +00:00
|
|
|
.requestLocalAuthForLockScreen(
|
|
|
|
context,
|
2022-10-28 14:31:12 +00:00
|
|
|
!_config.shouldShowLockScreen(),
|
2023-04-06 05:26:50 +00:00
|
|
|
S.of(context).authToChangeLockscreenSetting,
|
|
|
|
S.of(context).lockScreenEnablePreSteps,
|
2022-10-04 15:52:51 +00:00
|
|
|
);
|
|
|
|
},
|
2021-04-19 09:35:22 +00:00
|
|
|
),
|
|
|
|
),
|
2022-10-05 07:52:48 +00:00
|
|
|
sectionOptionSpacing,
|
2022-10-04 15:52:51 +00:00
|
|
|
MenuItemWidget(
|
2023-04-06 05:26:50 +00:00
|
|
|
captionedTextWidget: CaptionedTextWidget(
|
|
|
|
title: S.of(context).viewActiveSessions,
|
2022-10-04 15:52:51 +00:00
|
|
|
),
|
2022-10-21 13:37:57 +00:00
|
|
|
pressedColor: getEnteColorScheme(context).fillFaint,
|
2022-10-04 15:52:51 +00:00
|
|
|
trailingIcon: Icons.chevron_right_outlined,
|
|
|
|
trailingIconIsMuted: true,
|
2023-02-01 03:44:54 +00:00
|
|
|
showOnlyLoadingState: true,
|
2021-11-27 05:39:50 +00:00
|
|
|
onTap: () async {
|
2022-09-06 08:35:34 +00:00
|
|
|
final hasAuthenticated = await LocalAuthenticationService.instance
|
2022-09-05 10:32:04 +00:00
|
|
|
.requestLocalAuthentication(
|
2022-09-03 03:02:42 +00:00
|
|
|
context,
|
2023-04-06 05:26:50 +00:00
|
|
|
S.of(context).authToViewYourActiveSessions,
|
2021-11-27 05:39:50 +00:00
|
|
|
);
|
2022-09-06 08:35:34 +00:00
|
|
|
if (hasAuthenticated) {
|
2022-11-06 10:36:33 +00:00
|
|
|
unawaited(
|
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return const SessionsPage();
|
|
|
|
},
|
|
|
|
),
|
2022-09-03 03:02:42 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-11-27 05:39:50 +00:00
|
|
|
},
|
|
|
|
),
|
2022-10-05 07:52:48 +00:00
|
|
|
sectionOptionSpacing,
|
2021-11-27 05:39:50 +00:00
|
|
|
]);
|
2021-07-28 15:41:45 +00:00
|
|
|
return Column(
|
|
|
|
children: children,
|
2021-04-19 09:35:22 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-16 12:00:44 +00:00
|
|
|
Future<void> _disableTwoFactor() async {
|
2022-08-29 14:43:31 +00:00
|
|
|
final AlertDialog alert = AlertDialog(
|
2023-04-06 05:26:50 +00:00
|
|
|
title: Text(S.of(context).disableTwofactor),
|
|
|
|
content: Text(
|
|
|
|
S.of(context).confirm2FADisable,
|
2022-07-04 06:02:17 +00:00
|
|
|
),
|
2021-06-29 09:58:04 +00:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
child: Text(
|
2023-04-06 05:26:50 +00:00
|
|
|
S.of(context).no,
|
2021-06-29 09:58:04 +00:00
|
|
|
style: TextStyle(
|
2022-07-12 06:30:02 +00:00
|
|
|
color: Theme.of(context).colorScheme.greenAlternative,
|
2021-06-29 09:58:04 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context, rootNavigator: true).pop('dialog');
|
|
|
|
},
|
|
|
|
),
|
|
|
|
TextButton(
|
2023-04-06 05:26:50 +00:00
|
|
|
child: Text(
|
|
|
|
S.of(context).yes,
|
|
|
|
style: const TextStyle(
|
2021-06-29 09:58:04 +00:00
|
|
|
color: Colors.red,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onPressed: () async {
|
|
|
|
await UserService.instance.disableTwoFactor(context);
|
|
|
|
Navigator.of(context, rootNavigator: true).pop('dialog');
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2022-11-16 11:54:48 +00:00
|
|
|
await showDialog(
|
2021-06-29 09:58:04 +00:00
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return alert;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2023-03-25 05:52:38 +00:00
|
|
|
|
2024-03-08 03:33:43 +00:00
|
|
|
Future<void> onPasskeyClick(BuildContext buildContext) async {
|
2024-03-07 12:49:38 +00:00
|
|
|
try {
|
|
|
|
final isPassKeyResetEnabled =
|
2024-03-08 03:33:43 +00:00
|
|
|
await PasskeyService.instance.isPasskeyRecoveryEnabled();
|
2024-03-07 12:49:38 +00:00
|
|
|
if (!isPassKeyResetEnabled) {
|
|
|
|
final Uint8List recoveryKey =
|
|
|
|
await UserService.instance.getOrCreateRecoveryKey(context);
|
2024-03-11 17:03:47 +00:00
|
|
|
final resetKey = CryptoUtil.generateKey();
|
|
|
|
final resetKeyBase64 = CryptoUtil.bin2base64(resetKey);
|
2024-03-07 12:49:38 +00:00
|
|
|
final encryptionResult = CryptoUtil.encryptSync(
|
2024-03-11 17:03:47 +00:00
|
|
|
resetKey,
|
2024-03-07 12:49:38 +00:00
|
|
|
recoveryKey,
|
|
|
|
);
|
|
|
|
await PasskeyService.instance.configurePasskeyRecovery(
|
2024-03-11 17:03:47 +00:00
|
|
|
resetKeyBase64,
|
2024-03-07 12:49:38 +00:00
|
|
|
CryptoUtil.bin2base64(encryptionResult.encryptedData!),
|
|
|
|
CryptoUtil.bin2base64(encryptionResult.nonce!),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
PasskeyService.instance.openPasskeyPage(buildContext).ignore();
|
|
|
|
} catch (e, s) {
|
|
|
|
_logger.severe("failed to open passkey page", e, s);
|
|
|
|
await showGenericErrorDialog(context: context, error: e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-04 04:24:54 +00:00
|
|
|
Future<void> updateEmailMFA(bool isEnabled) async {
|
|
|
|
try {
|
2023-11-30 05:19:35 +00:00
|
|
|
final UserDetails details =
|
|
|
|
await UserService.instance.getUserDetailsV2(memoryCount: false);
|
|
|
|
if ((details.profileData?.canDisableEmailMFA ?? false) == false) {
|
|
|
|
await routeToPage(
|
|
|
|
context,
|
|
|
|
RequestPasswordVerificationPage(
|
|
|
|
onPasswordVerified: (Uint8List keyEncryptionKey) async {
|
|
|
|
final Uint8List loginKey =
|
|
|
|
await CryptoUtil.deriveLoginKey(keyEncryptionKey);
|
|
|
|
await UserService.instance.registerOrUpdateSrp(loginKey);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2023-08-14 08:35:11 +00:00
|
|
|
}
|
2023-08-04 04:24:54 +00:00
|
|
|
await UserService.instance.updateEmailMFA(isEnabled);
|
|
|
|
} catch (e) {
|
2023-12-16 21:04:26 +00:00
|
|
|
showToast(context, S.of(context).somethingWentWrong);
|
2023-08-04 04:24:54 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-19 09:35:22 +00:00
|
|
|
}
|