2020-12-10 15:06:02 +01:00
|
|
|
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
|
|
|
import 'package:famedlysdk/encryption.dart';
|
|
|
|
import 'package:famedlysdk/encryption/utils/bootstrap.dart';
|
2021-01-23 11:57:47 +01:00
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
2020-12-10 15:06:02 +01:00
|
|
|
import 'package:fluffychat/components/dialogs/adaptive_flat_button.dart';
|
2021-02-24 12:17:23 +01:00
|
|
|
import 'package:fluffychat/components/matrix.dart';
|
2021-02-06 12:55:27 +01:00
|
|
|
import 'package:flutter/services.dart';
|
2020-12-10 15:06:02 +01:00
|
|
|
import 'package:fluffychat/utils/platform_infos.dart';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
|
|
|
|
2021-02-13 14:26:03 +01:00
|
|
|
import 'key_verification_dialog.dart';
|
|
|
|
|
2020-12-10 15:06:02 +01:00
|
|
|
class BootstrapDialog extends StatefulWidget {
|
2021-02-13 14:26:03 +01:00
|
|
|
final bool wipe;
|
2021-01-23 11:57:47 +01:00
|
|
|
const BootstrapDialog({
|
|
|
|
Key key,
|
2021-02-13 14:26:03 +01:00
|
|
|
this.wipe = false,
|
2021-01-23 11:57:47 +01:00
|
|
|
}) : super(key: key);
|
2021-01-19 15:46:43 +01:00
|
|
|
|
2020-12-10 15:06:02 +01:00
|
|
|
Future<bool> show(BuildContext context) => PlatformInfos.isCupertinoStyle
|
2021-02-24 12:17:23 +01:00
|
|
|
? showCupertinoDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => this,
|
|
|
|
useRootNavigator: false,
|
|
|
|
)
|
|
|
|
: showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => this,
|
|
|
|
useRootNavigator: false,
|
|
|
|
);
|
2021-01-19 15:46:43 +01:00
|
|
|
|
2020-12-10 15:06:02 +01:00
|
|
|
@override
|
|
|
|
_BootstrapDialogState createState() => _BootstrapDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _BootstrapDialogState extends State<BootstrapDialog> {
|
2021-02-06 12:55:27 +01:00
|
|
|
final TextEditingController _recoveryKeyTextEditingController =
|
|
|
|
TextEditingController();
|
|
|
|
|
2020-12-10 15:06:02 +01:00
|
|
|
Bootstrap bootstrap;
|
|
|
|
|
2021-02-06 12:55:27 +01:00
|
|
|
String _recoveryKeyInputError;
|
|
|
|
|
|
|
|
bool _recoveryKeyInputLoading = false;
|
|
|
|
|
|
|
|
String titleText;
|
|
|
|
|
|
|
|
bool _recoveryKeyStored = false;
|
|
|
|
|
2021-02-13 14:26:03 +01:00
|
|
|
bool _wipe;
|
2021-02-06 12:55:27 +01:00
|
|
|
|
|
|
|
void _createBootstrap(bool wipe) {
|
|
|
|
setState(() {
|
|
|
|
_wipe = wipe;
|
|
|
|
titleText = null;
|
|
|
|
_recoveryKeyStored = false;
|
2021-02-24 12:17:23 +01:00
|
|
|
bootstrap = Matrix.of(context)
|
|
|
|
.client
|
|
|
|
.encryption
|
2021-02-06 12:55:27 +01:00
|
|
|
.bootstrap(onUpdate: () => setState(() => null));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-10 15:06:02 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-02-13 14:26:03 +01:00
|
|
|
_wipe ??= widget.wipe;
|
2020-12-10 15:06:02 +01:00
|
|
|
final buttons = <AdaptiveFlatButton>[];
|
2021-02-06 12:55:27 +01:00
|
|
|
Widget body = LinearProgressIndicator();
|
2021-02-24 12:17:23 +01:00
|
|
|
titleText = L10n.of(context).loadingPleaseWait;
|
2020-12-10 15:06:02 +01:00
|
|
|
|
2021-02-06 12:55:27 +01:00
|
|
|
if (bootstrap == null) {
|
2021-02-24 12:17:23 +01:00
|
|
|
titleText = L10n.of(context).chatBackup;
|
|
|
|
body = Text(L10n.of(context).chatBackupDescription);
|
2021-02-06 12:55:27 +01:00
|
|
|
buttons.add(AdaptiveFlatButton(
|
2021-02-27 07:53:34 +01:00
|
|
|
label: L10n.of(context).next,
|
2021-02-06 12:55:27 +01:00
|
|
|
onPressed: () => _createBootstrap(false),
|
|
|
|
));
|
|
|
|
} else if (bootstrap.newSsssKey?.recoveryKey != null &&
|
|
|
|
_recoveryKeyStored == false) {
|
|
|
|
final key = bootstrap.newSsssKey.recoveryKey;
|
2021-02-24 12:17:23 +01:00
|
|
|
titleText = L10n.of(context).securityKey;
|
2021-02-06 12:55:27 +01:00
|
|
|
body = Container(
|
|
|
|
alignment: Alignment.center,
|
|
|
|
width: 200,
|
|
|
|
height: 128,
|
|
|
|
child: Text(
|
|
|
|
key,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 18,
|
|
|
|
wordSpacing: 38,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
buttons.add(AdaptiveFlatButton(
|
2021-02-27 07:53:34 +01:00
|
|
|
label: L10n.of(context).copyToClipboard,
|
2021-02-06 12:55:27 +01:00
|
|
|
onPressed: () => Clipboard.setData(ClipboardData(text: key)),
|
|
|
|
));
|
|
|
|
buttons.add(AdaptiveFlatButton(
|
2021-02-27 07:53:34 +01:00
|
|
|
label: L10n.of(context).next,
|
2021-02-06 12:55:27 +01:00
|
|
|
onPressed: () => setState(() => _recoveryKeyStored = true),
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
switch (bootstrap.state) {
|
|
|
|
case BootstrapState.loading:
|
|
|
|
break;
|
|
|
|
case BootstrapState.askWipeSsss:
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback(
|
|
|
|
(_) => bootstrap.wipeSsss(_wipe),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case BootstrapState.askUseExistingSsss:
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback(
|
|
|
|
(_) => bootstrap.useExistingSsss(!_wipe),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case BootstrapState.askUnlockSsss:
|
|
|
|
throw Exception('This state is not supposed to be implemented');
|
|
|
|
case BootstrapState.askNewSsss:
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback(
|
|
|
|
(_) => bootstrap.newSsss(),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case BootstrapState.openExistingSsss:
|
|
|
|
_recoveryKeyStored = true;
|
|
|
|
titleText =
|
2021-02-24 12:17:23 +01:00
|
|
|
_recoveryKeyInputError ?? L10n.of(context).pleaseEnterSecurityKey;
|
2021-02-06 12:55:27 +01:00
|
|
|
body = PlatformInfos.isCupertinoStyle
|
|
|
|
? CupertinoTextField(
|
|
|
|
minLines: 2,
|
|
|
|
maxLines: 2,
|
|
|
|
autofocus: true,
|
|
|
|
autocorrect: false,
|
|
|
|
autofillHints: _recoveryKeyInputLoading
|
|
|
|
? null
|
|
|
|
: [AutofillHints.password],
|
|
|
|
controller: _recoveryKeyTextEditingController,
|
2020-12-10 15:06:02 +01:00
|
|
|
)
|
2021-02-06 12:55:27 +01:00
|
|
|
: TextField(
|
|
|
|
minLines: 2,
|
|
|
|
maxLines: 2,
|
|
|
|
autofocus: true,
|
|
|
|
autocorrect: false,
|
|
|
|
autofillHints: _recoveryKeyInputLoading
|
|
|
|
? null
|
|
|
|
: [AutofillHints.password],
|
|
|
|
controller: _recoveryKeyTextEditingController,
|
|
|
|
);
|
|
|
|
buttons.add(AdaptiveFlatButton(
|
|
|
|
textColor: Colors.red,
|
2021-02-27 07:53:34 +01:00
|
|
|
label: 'Lost security key',
|
2020-12-10 15:06:02 +01:00
|
|
|
onPressed: () async {
|
2021-02-06 12:55:27 +01:00
|
|
|
if (OkCancelResult.ok ==
|
|
|
|
await showOkCancelAlertDialog(
|
|
|
|
context: context,
|
2021-02-24 12:17:23 +01:00
|
|
|
useRootNavigator: false,
|
|
|
|
title: L10n.of(context).securityKeyLost,
|
|
|
|
message: L10n.of(context).wipeChatBackup,
|
|
|
|
okLabel: L10n.of(context).ok,
|
|
|
|
cancelLabel: L10n.of(context).cancel,
|
2021-02-06 12:55:27 +01:00
|
|
|
isDestructiveAction: true,
|
|
|
|
)) {
|
|
|
|
_createBootstrap(true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
));
|
2021-02-13 14:26:03 +01:00
|
|
|
buttons.add(AdaptiveFlatButton(
|
2021-02-27 07:53:34 +01:00
|
|
|
label: L10n.of(context).transferFromAnotherDevice,
|
2021-02-13 14:26:03 +01:00
|
|
|
onPressed: () async {
|
2021-02-24 12:17:23 +01:00
|
|
|
final req = await Matrix.of(context)
|
|
|
|
.client
|
|
|
|
.userDeviceKeys[Matrix.of(context).client.userID]
|
2021-02-13 14:26:03 +01:00
|
|
|
.startVerification();
|
2021-02-24 12:17:23 +01:00
|
|
|
await KeyVerificationDialog(request: req).show(context);
|
|
|
|
Navigator.of(context, rootNavigator: false).pop();
|
2021-02-13 14:26:03 +01:00
|
|
|
},
|
|
|
|
));
|
2021-02-06 12:55:27 +01:00
|
|
|
buttons.add(AdaptiveFlatButton(
|
2021-02-27 07:53:34 +01:00
|
|
|
label: L10n.of(context).next,
|
2021-02-06 12:55:27 +01:00
|
|
|
onPressed: () async {
|
|
|
|
setState(() {
|
|
|
|
_recoveryKeyInputError = null;
|
|
|
|
_recoveryKeyInputLoading = true;
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
await bootstrap.newSsssKey.unlock(
|
2021-02-13 15:07:30 +01:00
|
|
|
keyOrPassphrase: _recoveryKeyTextEditingController.text,
|
2021-02-06 12:55:27 +01:00
|
|
|
);
|
|
|
|
await bootstrap.openExistingSsss();
|
|
|
|
} catch (e, s) {
|
|
|
|
Logs().w('Unable to unlock SSSS', e, s);
|
|
|
|
setState(() => _recoveryKeyInputError =
|
2021-02-24 12:17:23 +01:00
|
|
|
L10n.of(context).oopsSomethingWentWrong);
|
2021-02-06 12:55:27 +01:00
|
|
|
} finally {
|
|
|
|
setState(() => _recoveryKeyInputLoading = false);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
break;
|
|
|
|
case BootstrapState.askWipeCrossSigning:
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback(
|
|
|
|
(_) => bootstrap.wipeCrossSigning(_wipe),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case BootstrapState.askSetupCrossSigning:
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback(
|
|
|
|
(_) => bootstrap.askSetupCrossSigning(
|
|
|
|
setupMasterKey: true,
|
|
|
|
setupSelfSigningKey: true,
|
|
|
|
setupUserSigningKey: true,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case BootstrapState.askWipeOnlineKeyBackup:
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback(
|
|
|
|
(_) => bootstrap.wipeOnlineKeyBackup(_wipe),
|
|
|
|
);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case BootstrapState.askSetupOnlineKeyBackup:
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback(
|
|
|
|
(_) => bootstrap.askSetupOnlineKeyBackup(true),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case BootstrapState.askBadSsss:
|
|
|
|
case BootstrapState.error:
|
2021-02-24 12:17:23 +01:00
|
|
|
titleText = L10n.of(context).oopsSomethingWentWrong;
|
2021-02-06 12:55:27 +01:00
|
|
|
body = ListTile(
|
|
|
|
contentPadding: EdgeInsets.zero,
|
|
|
|
leading: Icon(Icons.error_outline, color: Colors.red),
|
2021-02-24 12:17:23 +01:00
|
|
|
title: Text(L10n.of(context).oopsSomethingWentWrong),
|
2021-02-06 12:55:27 +01:00
|
|
|
);
|
|
|
|
buttons.add(AdaptiveFlatButton(
|
2021-02-27 07:53:34 +01:00
|
|
|
label: L10n.of(context).close,
|
2021-02-24 12:17:23 +01:00
|
|
|
onPressed: () =>
|
|
|
|
Navigator.of(context, rootNavigator: false).pop<bool>(false),
|
2021-02-06 12:55:27 +01:00
|
|
|
));
|
|
|
|
break;
|
|
|
|
case BootstrapState.done:
|
2021-02-24 12:17:23 +01:00
|
|
|
titleText = L10n.of(context).everythingReady;
|
2021-02-06 12:55:27 +01:00
|
|
|
body = ListTile(
|
|
|
|
contentPadding: EdgeInsets.zero,
|
|
|
|
leading: Icon(Icons.check_circle, color: Colors.green),
|
2021-02-24 12:17:23 +01:00
|
|
|
title: Text(L10n.of(context).keysCached),
|
2021-02-06 12:55:27 +01:00
|
|
|
);
|
|
|
|
buttons.add(AdaptiveFlatButton(
|
2021-02-27 07:53:34 +01:00
|
|
|
label: L10n.of(context).close,
|
2021-02-24 12:17:23 +01:00
|
|
|
onPressed: () =>
|
|
|
|
Navigator.of(context, rootNavigator: false).pop<bool>(false),
|
2021-02-06 12:55:27 +01:00
|
|
|
));
|
|
|
|
break;
|
|
|
|
}
|
2020-12-10 15:06:02 +01:00
|
|
|
}
|
|
|
|
|
2021-01-23 11:57:47 +01:00
|
|
|
final title = Text(titleText);
|
2020-12-10 15:06:02 +01:00
|
|
|
if (PlatformInfos.isCupertinoStyle) {
|
|
|
|
return CupertinoAlertDialog(
|
|
|
|
title: title,
|
|
|
|
content: body,
|
|
|
|
actions: buttons,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return AlertDialog(
|
|
|
|
title: title,
|
|
|
|
content: body,
|
|
|
|
actions: buttons,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|