fluffychat/lib/pages/bootstrap/bootstrap_dialog.dart

474 lines
18 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2021-10-26 16:50:34 +00:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
2021-05-15 10:06:13 +00:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2021-10-26 16:50:34 +00:00
import 'package:matrix/encryption.dart';
import 'package:matrix/matrix.dart';
2021-10-26 16:50:34 +00:00
import 'package:fluffychat/config/themes.dart';
2023-11-24 14:38:35 +00:00
import 'package:fluffychat/utils/error_reporter.dart';
import 'package:fluffychat/utils/fluffy_share.dart';
2023-11-24 14:38:35 +00:00
import 'package:fluffychat/utils/localized_exception_extension.dart';
2021-10-26 16:50:34 +00:00
import 'package:fluffychat/utils/platform_infos.dart';
import '../../utils/adaptive_bottom_sheet.dart';
2021-11-09 20:32:16 +00:00
import '../key_verification/key_verification_dialog.dart';
2021-02-13 13:26:03 +00:00
class BootstrapDialog extends StatefulWidget {
2021-02-13 13:26:03 +00:00
final bool wipe;
2021-03-06 09:03:01 +00:00
final Client client;
2021-01-23 10:57:47 +00:00
const BootstrapDialog({
super.key,
2021-02-13 13:26:03 +00:00
this.wipe = false,
2022-01-29 11:35:03 +00:00
required this.client,
});
2021-01-19 14:46:43 +00:00
Future<bool?> show(BuildContext context) => showAdaptiveBottomSheet(
context: context,
builder: (context) => this,
maxHeight: 600,
);
2021-01-19 14:46:43 +00:00
@override
2022-08-14 14:59:21 +00:00
BootstrapDialogState createState() => BootstrapDialogState();
}
2022-08-14 14:59:21 +00:00
class BootstrapDialogState extends State<BootstrapDialog> {
2021-02-06 11:55:27 +00:00
final TextEditingController _recoveryKeyTextEditingController =
TextEditingController();
2022-01-29 11:35:03 +00:00
late Bootstrap bootstrap;
2022-01-29 11:35:03 +00:00
String? _recoveryKeyInputError;
2021-02-06 11:55:27 +00:00
bool _recoveryKeyInputLoading = false;
2022-01-29 11:35:03 +00:00
String? titleText;
2021-02-06 11:55:27 +00:00
bool _recoveryKeyStored = false;
bool _recoveryKeyCopied = false;
2021-02-06 11:55:27 +00:00
bool? _storeInSecureStorage = false;
2022-01-29 11:35:03 +00:00
bool? _wipe;
2021-02-06 11:55:27 +00:00
String get _secureStorageKey =>
'ssss_recovery_key_${bootstrap.client.userID}';
bool get _supportsSecureStorage =>
PlatformInfos.isMobile || PlatformInfos.isDesktop;
String _getSecureStorageLocalizedName() {
if (PlatformInfos.isAndroid) {
return L10n.of(context)!.storeInAndroidKeystore;
}
if (PlatformInfos.isIOS || PlatformInfos.isMacOS) {
return L10n.of(context)!.storeInAppleKeyChain;
}
return L10n.of(context)!.storeSecurlyOnThisDevice;
}
2021-11-19 08:12:47 +00:00
@override
void initState() {
_createBootstrap(widget.wipe);
super.initState();
}
2022-08-05 18:37:36 +00:00
void _createBootstrap(bool wipe) async {
2021-11-19 08:12:47 +00:00
_wipe = wipe;
titleText = null;
_recoveryKeyStored = false;
2022-01-29 11:35:03 +00:00
bootstrap =
2022-09-05 15:44:16 +00:00
widget.client.encryption!.bootstrap(onUpdate: (_) => setState(() {}));
2022-08-05 18:37:36 +00:00
final key = await const FlutterSecureStorage().read(key: _secureStorageKey);
if (key == null) return;
_recoveryKeyTextEditingController.text = key;
2021-02-06 11:55:27 +00:00
}
@override
Widget build(BuildContext context) {
2021-02-13 13:26:03 +00:00
_wipe ??= widget.wipe;
2023-08-13 11:17:53 +00:00
final buttons = <Widget>[];
Widget body = const CircularProgressIndicator.adaptive();
2022-01-29 11:35:03 +00:00
titleText = L10n.of(context)!.loadingPleaseWait;
2021-11-19 08:12:47 +00:00
if (bootstrap.newSsssKey?.recoveryKey != null &&
2021-02-06 11:55:27 +00:00
_recoveryKeyStored == false) {
2022-01-29 11:35:03 +00:00
final key = bootstrap.newSsssKey!.recoveryKey;
titleText = L10n.of(context)!.recoveryKey;
2021-10-14 14:57:01 +00:00
return Scaffold(
appBar: AppBar(
2021-10-14 14:56:08 +00:00
centerTitle: true,
leading: IconButton(
2021-10-14 16:09:30 +00:00
icon: const Icon(Icons.close),
2021-10-14 14:56:08 +00:00
onPressed: Navigator.of(context).pop,
2021-10-10 11:35:16 +00:00
),
title: Text(L10n.of(context)!.recoveryKey),
2021-10-14 14:56:08 +00:00
),
2021-10-14 14:57:01 +00:00
body: Center(
2021-10-14 14:56:08 +00:00
child: ConstrainedBox(
constraints:
2021-10-14 16:09:30 +00:00
const BoxConstraints(maxWidth: FluffyThemes.columnWidth * 1.5),
2021-10-14 14:56:08 +00:00
child: ListView(
padding: const EdgeInsets.all(16.0),
children: [
ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 8.0),
2023-02-17 08:34:23 +00:00
trailing: CircleAvatar(
backgroundColor: Colors.transparent,
child: Icon(
Icons.info_outlined,
color: Theme.of(context).colorScheme.primary,
),
),
subtitle: Text(L10n.of(context)!.chatBackupDescription),
),
const Divider(
height: 32,
thickness: 1,
),
2021-10-14 14:56:08 +00:00
TextField(
2023-02-17 08:34:23 +00:00
minLines: 2,
2021-10-14 14:56:08 +00:00
maxLines: 4,
readOnly: true,
2023-02-17 08:34:23 +00:00
style: const TextStyle(fontFamily: 'RobotoMono'),
2021-10-14 14:56:08 +00:00
controller: TextEditingController(text: key),
2023-02-17 08:34:23 +00:00
decoration: const InputDecoration(
contentPadding: EdgeInsets.all(16),
suffixIcon: Icon(Icons.key_outlined),
),
2021-10-10 11:35:16 +00:00
),
2021-10-14 14:56:08 +00:00
const SizedBox(height: 16),
if (_supportsSecureStorage)
CheckboxListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 8.0),
value: _storeInSecureStorage,
activeColor: Theme.of(context).colorScheme.primary,
onChanged: (b) {
setState(() {
_storeInSecureStorage = b;
});
},
title: Text(_getSecureStorageLocalizedName()),
subtitle:
Text(L10n.of(context)!.storeInSecureStorageDescription),
),
const SizedBox(height: 16),
CheckboxListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 8.0),
value: _recoveryKeyCopied,
activeColor: Theme.of(context).colorScheme.primary,
onChanged: (b) {
FluffyShare.share(key!, context);
setState(() => _recoveryKeyCopied = true);
},
title: Text(L10n.of(context)!.copyToClipboard),
subtitle: Text(L10n.of(context)!.saveKeyManuallyDescription),
2021-10-14 14:56:08 +00:00
),
const SizedBox(height: 16),
ElevatedButton.icon(
2021-10-14 16:09:30 +00:00
icon: const Icon(Icons.check_outlined),
2022-01-29 11:35:03 +00:00
label: Text(L10n.of(context)!.next),
onPressed:
(_recoveryKeyCopied || _storeInSecureStorage == true)
? () {
if (_storeInSecureStorage == true) {
2022-08-05 18:37:36 +00:00
const FlutterSecureStorage().write(
key: _secureStorageKey,
value: key,
);
}
setState(() => _recoveryKeyStored = true);
}
: null,
2021-10-14 14:56:08 +00:00
),
],
),
2021-10-10 11:35:16 +00:00
),
2021-02-06 11:55:27 +00:00
),
);
} else {
switch (bootstrap.state) {
case BootstrapState.loading:
break;
case BootstrapState.askWipeSsss:
2022-05-12 09:25:34 +00:00
WidgetsBinding.instance.addPostFrameCallback(
2022-01-29 11:35:03 +00:00
(_) => bootstrap.wipeSsss(_wipe!),
2021-02-06 11:55:27 +00:00
);
break;
2021-07-24 09:45:27 +00:00
case BootstrapState.askBadSsss:
2022-05-12 09:25:34 +00:00
WidgetsBinding.instance.addPostFrameCallback(
2021-07-24 09:45:27 +00:00
(_) => bootstrap.ignoreBadSecrets(true),
);
break;
2021-02-06 11:55:27 +00:00
case BootstrapState.askUseExistingSsss:
2022-05-12 09:25:34 +00:00
WidgetsBinding.instance.addPostFrameCallback(
2022-01-29 11:35:03 +00:00
(_) => bootstrap.useExistingSsss(!_wipe!),
2021-02-06 11:55:27 +00:00
);
break;
case BootstrapState.askUnlockSsss:
2022-05-12 09:25:34 +00:00
WidgetsBinding.instance.addPostFrameCallback(
2021-07-24 09:45:27 +00:00
(_) => bootstrap.unlockedSsss(),
);
break;
2021-02-06 11:55:27 +00:00
case BootstrapState.askNewSsss:
2022-05-12 09:25:34 +00:00
WidgetsBinding.instance.addPostFrameCallback(
2021-02-06 11:55:27 +00:00
(_) => bootstrap.newSsss(),
);
break;
case BootstrapState.openExistingSsss:
_recoveryKeyStored = true;
2021-10-14 14:56:08 +00:00
return Scaffold(
appBar: AppBar(
centerTitle: true,
leading: IconButton(
2021-10-14 16:09:30 +00:00
icon: const Icon(Icons.close),
2021-10-14 14:56:08 +00:00
onPressed: Navigator.of(context).pop,
2021-10-10 09:40:08 +00:00
),
2023-02-17 08:34:23 +00:00
title: Text(L10n.of(context)!.chatBackup),
2021-10-14 14:56:08 +00:00
),
body: Center(
child: ConstrainedBox(
2021-10-17 12:15:29 +00:00
constraints: const BoxConstraints(
maxWidth: FluffyThemes.columnWidth * 1.5,
),
2021-10-14 14:56:08 +00:00
child: ListView(
padding: const EdgeInsets.all(16.0),
children: [
ListTile(
contentPadding:
const EdgeInsets.symmetric(horizontal: 8.0),
trailing: Icon(
Icons.info_outlined,
color: Theme.of(context).colorScheme.primary,
),
subtitle: Text(
L10n.of(context)!.pleaseEnterRecoveryKeyDescription,
),
2021-11-19 08:12:47 +00:00
),
const Divider(height: 32),
2021-10-14 14:56:08 +00:00
TextField(
2023-02-17 08:34:23 +00:00
minLines: 1,
2022-12-30 07:39:58 +00:00
maxLines: 2,
2021-10-14 14:56:08 +00:00
autocorrect: false,
2021-11-19 13:42:34 +00:00
readOnly: _recoveryKeyInputLoading,
2021-10-14 14:56:08 +00:00
autofillHints: _recoveryKeyInputLoading
? null
: [AutofillHints.password],
controller: _recoveryKeyTextEditingController,
2023-02-17 08:34:23 +00:00
style: const TextStyle(fontFamily: 'RobotoMono'),
2021-10-14 14:56:08 +00:00
decoration: InputDecoration(
2023-02-17 08:34:23 +00:00
contentPadding: const EdgeInsets.all(16),
hintStyle: TextStyle(
fontFamily:
Theme.of(context).textTheme.bodyLarge?.fontFamily,
),
2023-02-17 08:34:23 +00:00
hintText: L10n.of(context)!.recoveryKey,
2021-10-14 14:56:08 +00:00
errorText: _recoveryKeyInputError,
),
2021-10-10 11:35:16 +00:00
),
2021-10-14 14:56:08 +00:00
const SizedBox(height: 16),
ElevatedButton.icon(
style: ElevatedButton.styleFrom(
foregroundColor:
Theme.of(context).colorScheme.onPrimary,
backgroundColor: Theme.of(context).colorScheme.primary,
2021-10-14 14:56:08 +00:00
),
icon: _recoveryKeyInputLoading
? const CircularProgressIndicator.adaptive()
: const Icon(Icons.lock_open_outlined),
label: Text(L10n.of(context)!.unlockOldMessages),
onPressed: _recoveryKeyInputLoading
? null
: () async {
setState(() {
_recoveryKeyInputError = null;
_recoveryKeyInputLoading = true;
});
try {
final key = _recoveryKeyTextEditingController
.text
.trim();
await bootstrap.newSsssKey!.unlock(
keyOrPassphrase: key,
);
await bootstrap.openExistingSsss();
Logs().d('SSSS unlocked');
if (bootstrap.encryption.crossSigning.enabled) {
Logs().v(
'Cross signing is already enabled. Try to self-sign',
);
try {
await bootstrap
.client.encryption!.crossSigning
.selfSign(recoveryKey: key);
Logs().d('Successful selfsigned');
} catch (e, s) {
Logs().e(
'Unable to self sign with recovery key after successfully open existing SSSS',
e,
s,
);
}
}
} on InvalidPassphraseException catch (e) {
setState(
() => _recoveryKeyInputError =
e.toLocalizedString(context),
);
} catch (e, s) {
ErrorReporter(
context,
'Unable to open SSSS with recovery key',
).onErrorCallback(e, s);
setState(
() => _recoveryKeyInputError =
e.toLocalizedString(context),
);
} finally {
setState(
() => _recoveryKeyInputLoading = false,
);
}
},
),
const SizedBox(height: 16),
Row(
children: [
const Expanded(child: Divider()),
Padding(
padding: const EdgeInsets.all(12.0),
child: Text(L10n.of(context)!.or),
),
const Expanded(child: Divider()),
],
),
const SizedBox(height: 16),
2021-10-14 14:56:08 +00:00
ElevatedButton.icon(
icon: const Icon(Icons.cast_connected_outlined),
2022-01-29 11:35:03 +00:00
label: Text(L10n.of(context)!.transferFromAnotherDevice),
2021-11-19 13:42:34 +00:00
onPressed: _recoveryKeyInputLoading
? null
: () async {
final req = await showFutureLoadingDialog(
context: context,
2022-01-29 11:35:03 +00:00
future: () => widget.client
.userDeviceKeys[widget.client.userID!]!
2021-11-19 13:42:34 +00:00
.startVerification(),
);
if (req.error != null) return;
2022-01-29 11:35:03 +00:00
await KeyVerificationDialog(request: req.result!)
2021-11-19 13:42:34 +00:00
.show(context);
Navigator.of(context, rootNavigator: false).pop();
},
2021-10-10 11:35:16 +00:00
),
2021-10-14 14:56:08 +00:00
const SizedBox(height: 16),
ElevatedButton.icon(
style: ElevatedButton.styleFrom(
2022-08-31 17:54:22 +00:00
foregroundColor: Colors.red,
2021-10-14 14:56:08 +00:00
),
2021-10-14 16:09:30 +00:00
icon: const Icon(Icons.delete_outlined),
label: Text(L10n.of(context)!.recoveryKeyLost),
2021-11-19 13:42:34 +00:00
onPressed: _recoveryKeyInputLoading
? null
: () async {
if (OkCancelResult.ok ==
await showOkCancelAlertDialog(
useRootNavigator: false,
context: context,
title: L10n.of(context)!.recoveryKeyLost,
2022-01-29 11:35:03 +00:00
message: L10n.of(context)!.wipeChatBackup,
okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context)!.cancel,
2021-11-19 13:42:34 +00:00
isDestructiveAction: true,
)) {
setState(() => _createBootstrap(true));
}
},
2023-08-18 05:24:31 +00:00
),
2021-10-14 14:56:08 +00:00
],
),
2021-10-10 11:35:16 +00:00
),
2021-10-10 09:40:08 +00:00
),
);
2021-02-06 11:55:27 +00:00
case BootstrapState.askWipeCrossSigning:
2022-05-12 09:25:34 +00:00
WidgetsBinding.instance.addPostFrameCallback(
2022-01-29 11:35:03 +00:00
(_) => bootstrap.wipeCrossSigning(_wipe!),
2021-02-06 11:55:27 +00:00
);
break;
case BootstrapState.askSetupCrossSigning:
2022-05-12 09:25:34 +00:00
WidgetsBinding.instance.addPostFrameCallback(
2021-02-06 11:55:27 +00:00
(_) => bootstrap.askSetupCrossSigning(
setupMasterKey: true,
setupSelfSigningKey: true,
setupUserSigningKey: true,
),
);
break;
case BootstrapState.askWipeOnlineKeyBackup:
2022-05-12 09:25:34 +00:00
WidgetsBinding.instance.addPostFrameCallback(
2022-01-29 11:35:03 +00:00
(_) => bootstrap.wipeOnlineKeyBackup(_wipe!),
2021-02-06 11:55:27 +00:00
);
break;
case BootstrapState.askSetupOnlineKeyBackup:
2022-05-12 09:25:34 +00:00
WidgetsBinding.instance.addPostFrameCallback(
2021-02-06 11:55:27 +00:00
(_) => bootstrap.askSetupOnlineKeyBackup(true),
);
break;
case BootstrapState.error:
2022-01-29 11:35:03 +00:00
titleText = L10n.of(context)!.oopsSomethingWentWrong;
2023-08-13 11:17:53 +00:00
body = const Icon(Icons.error_outline, color: Colors.red, size: 80);
buttons.add(
2023-08-13 11:17:53 +00:00
OutlinedButton(
onPressed: () =>
Navigator.of(context, rootNavigator: false).pop<bool>(false),
2023-08-13 11:17:53 +00:00
child: Text(L10n.of(context)!.close),
),
);
2021-02-06 11:55:27 +00:00
break;
case BootstrapState.done:
2022-01-29 11:35:03 +00:00
titleText = L10n.of(context)!.everythingReady;
2021-06-22 15:56:15 +00:00
body = Column(
mainAxisSize: MainAxisSize.min,
children: [
2021-10-10 09:40:08 +00:00
Image.asset('assets/backup.png', fit: BoxFit.contain),
2022-01-29 11:35:03 +00:00
Text(L10n.of(context)!.yourChatBackupHasBeenSetUp),
2021-06-22 15:56:15 +00:00
],
2021-02-06 11:55:27 +00:00
);
buttons.add(
2023-08-13 11:17:53 +00:00
OutlinedButton(
onPressed: () =>
Navigator.of(context, rootNavigator: false).pop<bool>(false),
2023-08-13 11:17:53 +00:00
child: Text(L10n.of(context)!.close),
),
);
2021-02-06 11:55:27 +00:00
break;
}
}
return Scaffold(
appBar: AppBar(
2023-08-13 11:17:53 +00:00
leading: Center(
child: CloseButton(
onPressed: () =>
Navigator.of(context, rootNavigator: false).pop<bool>(true),
),
),
title: Text(titleText ?? L10n.of(context)!.loadingPleaseWait),
),
2023-08-13 11:17:53 +00:00
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
body,
const SizedBox(height: 8),
...buttons,
],
),
),
);
}
}