security_section_widget.dart 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import 'dart:async';
  2. import "dart:typed_data";
  3. import 'package:flutter/material.dart';
  4. import 'package:photos/core/configuration.dart';
  5. import 'package:photos/core/event_bus.dart';
  6. import 'package:photos/ente_theme_data.dart';
  7. import 'package:photos/events/two_factor_status_change_event.dart';
  8. import "package:photos/generated/l10n.dart";
  9. import "package:photos/l10n/l10n.dart";
  10. import "package:photos/models/user_details.dart";
  11. import "package:photos/services/feature_flag_service.dart";
  12. import 'package:photos/services/local_authentication_service.dart';
  13. import "package:photos/services/passkey_service.dart";
  14. import 'package:photos/services/user_service.dart';
  15. import 'package:photos/theme/ente_theme.dart';
  16. import "package:photos/ui/account/request_pwd_verification_page.dart";
  17. import 'package:photos/ui/account/sessions_page.dart';
  18. import 'package:photos/ui/components/captioned_text_widget.dart';
  19. import 'package:photos/ui/components/expandable_menu_item_widget.dart';
  20. import 'package:photos/ui/components/menu_item_widget/menu_item_widget.dart';
  21. import 'package:photos/ui/components/toggle_switch_widget.dart';
  22. import 'package:photos/ui/settings/common_settings.dart';
  23. import "package:photos/utils/crypto_util.dart";
  24. import "package:photos/utils/navigation_util.dart";
  25. import "package:photos/utils/toast_util.dart";
  26. class SecuritySectionWidget extends StatefulWidget {
  27. const SecuritySectionWidget({Key? key}) : super(key: key);
  28. @override
  29. State<SecuritySectionWidget> createState() => _SecuritySectionWidgetState();
  30. }
  31. class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
  32. final _config = Configuration.instance;
  33. late StreamSubscription<TwoFactorStatusChangeEvent>
  34. _twoFactorStatusChangeEvent;
  35. @override
  36. void initState() {
  37. super.initState();
  38. _twoFactorStatusChangeEvent =
  39. Bus.instance.on<TwoFactorStatusChangeEvent>().listen((event) async {
  40. if (mounted) {
  41. setState(() {});
  42. }
  43. });
  44. }
  45. @override
  46. void dispose() {
  47. _twoFactorStatusChangeEvent.cancel();
  48. super.dispose();
  49. }
  50. @override
  51. Widget build(BuildContext context) {
  52. return ExpandableMenuItemWidget(
  53. title: S.of(context).security,
  54. selectionOptionsWidget: _getSectionOptions(context),
  55. leadingIcon: Icons.local_police_outlined,
  56. );
  57. }
  58. Widget _getSectionOptions(BuildContext context) {
  59. final Completer completer = Completer();
  60. final List<Widget> children = [];
  61. if (_config.hasConfiguredAccount()) {
  62. final bool isInternalUser =
  63. FeatureFlagService.instance.isInternalUserOrDebugBuild();
  64. children.addAll(
  65. [
  66. sectionOptionSpacing,
  67. MenuItemWidget(
  68. captionedTextWidget: CaptionedTextWidget(
  69. title: S.of(context).twofactor,
  70. ),
  71. trailingWidget: ToggleSwitchWidget(
  72. value: () => UserService.instance.hasEnabledTwoFactor(),
  73. onChanged: () async {
  74. final hasAuthenticated = await LocalAuthenticationService
  75. .instance
  76. .requestLocalAuthentication(
  77. context,
  78. S.of(context).authToConfigureTwofactorAuthentication,
  79. );
  80. final isTwoFactorEnabled =
  81. UserService.instance.hasEnabledTwoFactor();
  82. if (hasAuthenticated) {
  83. if (isTwoFactorEnabled) {
  84. await _disableTwoFactor();
  85. completer.isCompleted ? null : completer.complete();
  86. } else {
  87. await UserService.instance
  88. .setupTwoFactor(context, completer);
  89. }
  90. return completer.future;
  91. }
  92. },
  93. ),
  94. ),
  95. if (isInternalUser) sectionOptionSpacing,
  96. if (isInternalUser)
  97. MenuItemWidget(
  98. captionedTextWidget: CaptionedTextWidget(
  99. title: context.l10n.passkey,
  100. ),
  101. pressedColor: getEnteColorScheme(context).fillFaint,
  102. trailingIcon: Icons.chevron_right_outlined,
  103. trailingIconIsMuted: true,
  104. onTap: () => PasskeyService.instance.openPasskeyPage(context),
  105. ),
  106. sectionOptionSpacing,
  107. MenuItemWidget(
  108. captionedTextWidget: CaptionedTextWidget(
  109. title: S.of(context).emailVerificationToggle,
  110. ),
  111. trailingWidget: ToggleSwitchWidget(
  112. value: () => UserService.instance.hasEmailMFAEnabled(),
  113. onChanged: () async {
  114. final hasAuthenticated = await LocalAuthenticationService
  115. .instance
  116. .requestLocalAuthentication(
  117. context,
  118. S.of(context).authToChangeEmailVerificationSetting,
  119. );
  120. final isEmailMFAEnabled =
  121. UserService.instance.hasEmailMFAEnabled();
  122. if (hasAuthenticated) {
  123. await updateEmailMFA(!isEmailMFAEnabled);
  124. }
  125. },
  126. ),
  127. ),
  128. sectionOptionSpacing,
  129. ],
  130. );
  131. }
  132. children.addAll([
  133. MenuItemWidget(
  134. captionedTextWidget: CaptionedTextWidget(
  135. title: S.of(context).lockscreen,
  136. ),
  137. trailingWidget: ToggleSwitchWidget(
  138. value: () => _config.shouldShowLockScreen(),
  139. onChanged: () async {
  140. await LocalAuthenticationService.instance
  141. .requestLocalAuthForLockScreen(
  142. context,
  143. !_config.shouldShowLockScreen(),
  144. S.of(context).authToChangeLockscreenSetting,
  145. S.of(context).lockScreenEnablePreSteps,
  146. );
  147. },
  148. ),
  149. ),
  150. sectionOptionSpacing,
  151. MenuItemWidget(
  152. captionedTextWidget: CaptionedTextWidget(
  153. title: S.of(context).viewActiveSessions,
  154. ),
  155. pressedColor: getEnteColorScheme(context).fillFaint,
  156. trailingIcon: Icons.chevron_right_outlined,
  157. trailingIconIsMuted: true,
  158. showOnlyLoadingState: true,
  159. onTap: () async {
  160. final hasAuthenticated = await LocalAuthenticationService.instance
  161. .requestLocalAuthentication(
  162. context,
  163. S.of(context).authToViewYourActiveSessions,
  164. );
  165. if (hasAuthenticated) {
  166. unawaited(
  167. Navigator.of(context).push(
  168. MaterialPageRoute(
  169. builder: (BuildContext context) {
  170. return const SessionsPage();
  171. },
  172. ),
  173. ),
  174. );
  175. }
  176. },
  177. ),
  178. sectionOptionSpacing,
  179. ]);
  180. return Column(
  181. children: children,
  182. );
  183. }
  184. Future<void> _disableTwoFactor() async {
  185. final AlertDialog alert = AlertDialog(
  186. title: Text(S.of(context).disableTwofactor),
  187. content: Text(
  188. S.of(context).confirm2FADisable,
  189. ),
  190. actions: [
  191. TextButton(
  192. child: Text(
  193. S.of(context).no,
  194. style: TextStyle(
  195. color: Theme.of(context).colorScheme.greenAlternative,
  196. ),
  197. ),
  198. onPressed: () {
  199. Navigator.of(context, rootNavigator: true).pop('dialog');
  200. },
  201. ),
  202. TextButton(
  203. child: Text(
  204. S.of(context).yes,
  205. style: const TextStyle(
  206. color: Colors.red,
  207. ),
  208. ),
  209. onPressed: () async {
  210. await UserService.instance.disableTwoFactor(context);
  211. Navigator.of(context, rootNavigator: true).pop('dialog');
  212. },
  213. ),
  214. ],
  215. );
  216. await showDialog(
  217. context: context,
  218. builder: (BuildContext context) {
  219. return alert;
  220. },
  221. );
  222. }
  223. Future<void> updateEmailMFA(bool isEnabled) async {
  224. try {
  225. final UserDetails details =
  226. await UserService.instance.getUserDetailsV2(memoryCount: false);
  227. if ((details.profileData?.canDisableEmailMFA ?? false) == false) {
  228. await routeToPage(
  229. context,
  230. RequestPasswordVerificationPage(
  231. onPasswordVerified: (Uint8List keyEncryptionKey) async {
  232. final Uint8List loginKey =
  233. await CryptoUtil.deriveLoginKey(keyEncryptionKey);
  234. await UserService.instance.registerOrUpdateSrp(loginKey);
  235. },
  236. ),
  237. );
  238. }
  239. await UserService.instance.updateEmailMFA(isEnabled);
  240. } catch (e) {
  241. showToast(context, S.of(context).somethingWentWrong);
  242. }
  243. }
  244. }