ente/lib/ui/settings/security_section_widget.dart

204 lines
6.8 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:typed_data';
2022-11-01 06:13:06 +00:00
import 'package:ente_auth/core/configuration.dart';
import 'package:ente_auth/l10n/l10n.dart';
import 'package:ente_auth/models/user_details.dart';
2022-11-01 06:13:06 +00:00
import 'package:ente_auth/services/local_authentication_service.dart';
import 'package:ente_auth/services/user_service.dart';
2022-11-01 06:13:06 +00:00
import 'package:ente_auth/theme/ente_theme.dart';
2023-08-07 11:15:51 +00:00
import 'package:ente_auth/ui/account/recovery_key_page.dart';
import 'package:ente_auth/ui/account/request_pwd_verification_page.dart';
2022-11-01 06:13:06 +00:00
import 'package:ente_auth/ui/account/sessions_page.dart';
import 'package:ente_auth/ui/components/captioned_text_widget.dart';
import 'package:ente_auth/ui/components/expandable_menu_item_widget.dart';
import 'package:ente_auth/ui/components/menu_item_widget.dart';
import 'package:ente_auth/ui/components/toggle_switch_widget.dart';
import 'package:ente_auth/ui/settings/common_settings.dart';
import 'package:ente_auth/utils/crypto_util.dart';
2023-08-07 11:15:51 +00:00
import 'package:ente_auth/utils/dialog_util.dart';
import 'package:ente_auth/utils/navigation_util.dart';
import 'package:ente_auth/utils/toast_util.dart';
2022-11-01 06:13:06 +00:00
import 'package:flutter/material.dart';
2023-08-07 11:15:51 +00:00
import 'package:flutter_sodium/flutter_sodium.dart';
2022-11-01 06:13:06 +00:00
class SecuritySectionWidget extends StatefulWidget {
2023-04-10 04:17:45 +00:00
const SecuritySectionWidget({Key? key}) : super(key: key);
2022-11-01 06:13:06 +00:00
@override
State<SecuritySectionWidget> createState() => _SecuritySectionWidgetState();
}
class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
final _config = Configuration.instance;
late bool _hasLoggedIn;
2022-11-01 06:13:06 +00:00
@override
void initState() {
_hasLoggedIn = _config.hasConfiguredAccount();
2022-11-01 06:13:06 +00:00
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
2023-04-08 03:55:34 +00:00
final l10n = context.l10n;
2022-11-01 06:13:06 +00:00
return ExpandableMenuItemWidget(
2023-04-08 03:55:34 +00:00
title: l10n.security,
2022-11-01 06:13:06 +00:00
selectionOptionsWidget: _getSectionOptions(context),
leadingIcon: Icons.local_police_outlined,
);
}
Widget _getSectionOptions(BuildContext context) {
final l10n = context.l10n;
2022-11-01 06:13:06 +00:00
final List<Widget> children = [];
if (_hasLoggedIn) {
final bool? canDisableMFA = UserService.instance.canDisableEmailMFA();
if (canDisableMFA == null) {
// We don't know if the user can disable MFA yet, so we fetch the info
UserService.instance.getUserDetailsV2().ignore();
}
children.addAll([
sectionOptionSpacing,
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
title: l10n.recoveryKey,
),
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
onTap: () async {
final hasAuthenticated = await LocalAuthenticationService.instance
.requestLocalAuthentication(
2023-08-07 11:15:51 +00:00
context,
l10n.authToViewYourRecoveryKey,
2023-08-07 11:15:51 +00:00
);
if (hasAuthenticated) {
String recoveryKey;
try {
recoveryKey =
Sodium.bin2hex(Configuration.instance.getRecoveryKey());
} catch (e) {
showGenericErrorDialog(context: context);
return;
}
routeToPage(
context,
RecoveryKeyPage(
recoveryKey,
l10n.ok,
showAppBar: true,
onDone: () {},
),
);
}
},
2022-11-01 06:13:06 +00:00
),
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
title: l10n.emailVerificationToggle,
),
trailingWidget: ToggleSwitchWidget(
value: () => UserService.instance.hasEmailMFAEnabled(),
onChanged: () async {
final hasAuthenticated = await LocalAuthenticationService.instance
.requestLocalAuthentication(
context,
l10n.authToChangeEmailVerificationSetting,
);
final isEmailMFAEnabled =
UserService.instance.hasEmailMFAEnabled();
if (hasAuthenticated) {
await updateEmailMFA(!isEmailMFAEnabled);
if (mounted) {
setState(() {});
}
}
},
),
),
sectionOptionSpacing,
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
title: context.l10n.viewActiveSessions,
),
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
onTap: () async {
2022-11-01 06:13:06 +00:00
final hasAuthenticated = await LocalAuthenticationService.instance
.requestLocalAuthentication(
2022-11-01 06:13:06 +00:00
context,
context.l10n.authToViewYourActiveSessions,
2022-11-01 06:13:06 +00:00
);
if (hasAuthenticated) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return const SessionsPage();
},
),
);
2022-11-01 06:13:06 +00:00
}
},
),
]);
} else {
children.add(sectionOptionSpacing);
}
children.addAll([
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
title: l10n.lockscreen,
),
trailingWidget: ToggleSwitchWidget(
value: () => _config.shouldShowLockScreen(),
onChanged: () async {
final hasAuthenticated = await LocalAuthenticationService.instance
.requestLocalAuthForLockScreen(
context,
!_config.shouldShowLockScreen(),
context.l10n.authToChangeLockscreenSetting,
context.l10n.lockScreenEnablePreSteps,
);
if (hasAuthenticated) {
setState(() {});
}
},
),
),
2022-11-01 06:13:06 +00:00
sectionOptionSpacing,
]);
return Column(
children: children,
);
}
Future<void> updateEmailMFA(bool isEnabled) async {
try {
final UserDetails details =
await UserService.instance.getUserDetailsV2(memoryCount: false);
if (details.profileData?.canDisableEmailMFA == false) {
await routeToPage(
context,
RequestPasswordVerificationPage(
onPasswordVerified: (Uint8List keyEncryptionKey) async {
final Uint8List loginKey =
await CryptoUtil.deriveLoginKey(keyEncryptionKey);
await UserService.instance.registerOrUpdateSrp(loginKey);
},
),
);
}
await UserService.instance.updateEmailMFA(isEnabled);
} catch (e) {
showToast(context, context.l10n.somethingWentWrongMessage);
}
}
2022-11-01 06:13:06 +00:00
}