feat: Simplified bootstrap

This commit is contained in:
Christian Pauly 2021-02-06 12:55:27 +01:00
parent d2340c6eac
commit d9984da76d

View File

@ -3,7 +3,8 @@ import 'package:famedlysdk/encryption.dart';
import 'package:famedlysdk/encryption/utils/bootstrap.dart'; import 'package:famedlysdk/encryption/utils/bootstrap.dart';
import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/dialogs/adaptive_flat_button.dart'; import 'package:fluffychat/components/dialogs/adaptive_flat_button.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart'; import 'package:fluffychat/utils/sentry_controller.dart';
import 'package:flutter/services.dart';
import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/utils/platform_infos.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -14,7 +15,6 @@ class BootstrapDialog extends StatefulWidget {
Key key, Key key,
@required this.l10n, @required this.l10n,
@required this.client, @required this.client,
this.easyMode = false,
}) : super(key: key); }) : super(key: key);
Future<bool> show(BuildContext context) => PlatformInfos.isCupertinoStyle Future<bool> show(BuildContext context) => PlatformInfos.isCupertinoStyle
@ -23,195 +23,224 @@ class BootstrapDialog extends StatefulWidget {
final L10n l10n; final L10n l10n;
final Client client; final Client client;
final bool easyMode;
@override @override
_BootstrapDialogState createState() => _BootstrapDialogState(); _BootstrapDialogState createState() => _BootstrapDialogState();
} }
class _BootstrapDialogState extends State<BootstrapDialog> { class _BootstrapDialogState extends State<BootstrapDialog> {
final TextEditingController _recoveryKeyTextEditingController =
TextEditingController();
Bootstrap bootstrap; Bootstrap bootstrap;
String _recoveryKeyInputError;
bool _recoveryKeyInputLoading = false;
String titleText;
bool _recoveryKeyStored = false;
bool _wipe = false;
void _createBootstrap(bool wipe) {
setState(() {
_wipe = wipe;
titleText = null;
_recoveryKeyStored = false;
bootstrap = widget.client.encryption
.bootstrap(onUpdate: () => setState(() => null));
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
bootstrap ??= widget.client.encryption
.bootstrap(onUpdate: () => setState(() => null));
final buttons = <AdaptiveFlatButton>[]; final buttons = <AdaptiveFlatButton>[];
Widget body; Widget body = LinearProgressIndicator();
var titleText = widget.l10n.cachedKeys; titleText = widget.l10n.loadingPleaseWait;
switch (bootstrap.state) { if (bootstrap == null) {
case BootstrapState.loading: titleText = 'Chat backup';
body = LinearProgressIndicator(); body = Text(
titleText = widget.l10n.loadingPleaseWait; 'To make sure that only you have access to your encrypted messages, we have generated a security key for you.');
break; buttons.add(AdaptiveFlatButton(
case BootstrapState.askWipeSsss: child: Text(widget.l10n.next),
body = Text('Wipe chat backup?'); onPressed: () => _createBootstrap(false),
buttons.add(AdaptiveFlatButton( ));
child: Text(widget.l10n.yes), } else if (bootstrap.newSsssKey?.recoveryKey != null &&
onPressed: () => bootstrap.wipeSsss(true), _recoveryKeyStored == false) {
)); final key = bootstrap.newSsssKey.recoveryKey;
buttons.add(AdaptiveFlatButton( titleText = 'Security key';
textColor: Theme.of(context).textTheme.bodyText1.color, body = Container(
child: Text(widget.l10n.no), alignment: Alignment.center,
onPressed: () => bootstrap.wipeSsss(false), width: 200,
)); height: 128,
break; child: Text(
case BootstrapState.askUseExistingSsss: key,
body = Text('Use existing chat backup?'); textAlign: TextAlign.center,
buttons.add(AdaptiveFlatButton( style: TextStyle(
child: Text(widget.l10n.yes), fontSize: 18,
onPressed: () => bootstrap.useExistingSsss(true), wordSpacing: 38,
));
buttons.add(AdaptiveFlatButton(
textColor: Theme.of(context).textTheme.bodyText1.color,
child: Text(widget.l10n.no),
onPressed: () => bootstrap.useExistingSsss(false),
));
break;
case BootstrapState.askBadSsss:
body = Text('SSSS bad - continue nevertheless? DATALOSS!!!');
buttons.add(AdaptiveFlatButton(
child: Text(widget.l10n.yes),
onPressed: () => bootstrap.ignoreBadSecrets(true),
));
buttons.add(AdaptiveFlatButton(
textColor: Theme.of(context).textTheme.bodyText1.color,
child: Text(widget.l10n.no),
onPressed: () => bootstrap.ignoreBadSecrets(false),
));
break;
case BootstrapState.askUnlockSsss:
final widgets = <Widget>[Text('Unlock old SSSS')];
for (final entry in bootstrap.oldSsssKeys.entries) {
final keyId = entry.key;
final key = entry.value;
widgets
.add(Flexible(child: _AskUnlockOldSsss(keyId, key, widget.l10n)));
}
body = Column(
children: widgets,
mainAxisSize: MainAxisSize.min,
);
buttons.add(AdaptiveFlatButton(
child: Text(widget.l10n.confirm),
onPressed: () => bootstrap.unlockedSsss(),
));
break;
case BootstrapState.askNewSsss:
body = Text('Please set a long passphrase to secure your backup.');
buttons.add(AdaptiveFlatButton(
child: Text('Enter a new passphrase'),
onPressed: () async {
final input =
await showTextInputDialog(context: context, textFields: [
DialogTextField(
minLines: 1,
maxLines: 1,
obscureText: true,
)
]);
if (input?.isEmpty ?? true) return;
await bootstrap.newSsss(input.single);
}));
break;
case BootstrapState.openExistingSsss:
body = Text('Please enter your passphrase!');
buttons.add(AdaptiveFlatButton(
child: Text('Enter passphrase'),
onPressed: () async {
final input =
await showTextInputDialog(context: context, textFields: [
DialogTextField(
minLines: 1,
maxLines: 1,
obscureText: true,
)
]);
if (input?.isEmpty ?? true) return;
final valid = await showFutureLoadingDialog(
context: context,
future: () =>
bootstrap.newSsssKey.unlock(keyOrPassphrase: input.single),
);
if (valid.error == null) await bootstrap.openExistingSsss();
}));
break;
case BootstrapState.askWipeCrossSigning:
body = Text('Wipe cross-signing?');
buttons.add(AdaptiveFlatButton(
child: Text(widget.l10n.yes),
onPressed: () => bootstrap.wipeCrossSigning(true),
));
buttons.add(AdaptiveFlatButton(
textColor: Theme.of(context).textTheme.bodyText1.color,
child: Text(widget.l10n.no),
onPressed: () => bootstrap.wipeCrossSigning(false),
));
break;
case BootstrapState.askSetupCrossSigning:
body = Text('Set up cross-signing?');
buttons.add(AdaptiveFlatButton(
child: Text(widget.l10n.yes),
onPressed: () => bootstrap.askSetupCrossSigning(
setupMasterKey: true,
setupSelfSigningKey: true,
setupUserSigningKey: true,
), ),
)); ),
buttons.add(AdaptiveFlatButton( );
textColor: Theme.of(context).textTheme.bodyText1.color, buttons.add(AdaptiveFlatButton(
child: Text(widget.l10n.no), child: Text('Copy to clipboard'),
onPressed: () => bootstrap.askSetupCrossSigning(), onPressed: () => Clipboard.setData(ClipboardData(text: key)),
)); ));
break; buttons.add(AdaptiveFlatButton(
case BootstrapState.askWipeOnlineKeyBackup: child: Text(widget.l10n.next),
body = Text('Wipe chat backup?'); onPressed: () => setState(() => _recoveryKeyStored = true),
buttons.add(AdaptiveFlatButton( ));
child: Text(widget.l10n.yes), } else {
onPressed: () => bootstrap.wipeOnlineKeyBackup(true), switch (bootstrap.state) {
)); case BootstrapState.loading:
buttons.add(AdaptiveFlatButton( break;
textColor: Theme.of(context).textTheme.bodyText1.color, case BootstrapState.askWipeSsss:
child: Text(widget.l10n.no), WidgetsBinding.instance.addPostFrameCallback(
onPressed: () => bootstrap.wipeOnlineKeyBackup(false), (_) => bootstrap.wipeSsss(_wipe),
)); );
break; break;
case BootstrapState.askSetupOnlineKeyBackup: case BootstrapState.askUseExistingSsss:
body = Text('Set up chat backup?'); WidgetsBinding.instance.addPostFrameCallback(
buttons.add(AdaptiveFlatButton( (_) => bootstrap.useExistingSsss(!_wipe),
child: Text(widget.l10n.yes), );
onPressed: () => bootstrap.askSetupOnlineKeyBackup(true), break;
)); case BootstrapState.askUnlockSsss:
buttons.add(AdaptiveFlatButton( throw Exception('This state is not supposed to be implemented');
textColor: Theme.of(context).textTheme.bodyText1.color, case BootstrapState.askNewSsss:
child: Text(widget.l10n.no), WidgetsBinding.instance.addPostFrameCallback(
onPressed: () => bootstrap.askSetupOnlineKeyBackup(false), (_) => bootstrap.newSsss(),
)); );
break; break;
case BootstrapState.error: case BootstrapState.openExistingSsss:
body = ListTile( _recoveryKeyStored = true;
contentPadding: EdgeInsets.zero, titleText =
leading: Icon(Icons.error_outline, color: Colors.red), _recoveryKeyInputError ?? 'Please enter your security key!';
title: Text(widget.l10n.oopsSomethingWentWrong), body = PlatformInfos.isCupertinoStyle
); ? CupertinoTextField(
buttons.add(AdaptiveFlatButton( minLines: 2,
child: Text(widget.l10n.close), maxLines: 2,
onPressed: () => Navigator.of(context).pop<bool>(false), autofocus: true,
)); autocorrect: false,
break; autofillHints: _recoveryKeyInputLoading
case BootstrapState.done: ? null
body = ListTile( : [AutofillHints.password],
contentPadding: EdgeInsets.zero, controller: _recoveryKeyTextEditingController,
leading: Icon(Icons.check_circle, color: Colors.green), )
title: Text('Chat backup has been initialized!'), : TextField(
); minLines: 2,
buttons.add(AdaptiveFlatButton( maxLines: 2,
child: Text(widget.l10n.close), autofocus: true,
onPressed: () => Navigator.of(context).pop<bool>(false), autocorrect: false,
)); autofillHints: _recoveryKeyInputLoading
break; ? null
: [AutofillHints.password],
controller: _recoveryKeyTextEditingController,
);
buttons.add(AdaptiveFlatButton(
textColor: Colors.red,
child: Text('Lost security key'),
onPressed: () async {
if (OkCancelResult.ok ==
await showOkCancelAlertDialog(
context: context,
title: 'Lost security key',
message:
'Wipe your chat backup to create a new security key?',
isDestructiveAction: true,
)) {
_createBootstrap(true);
}
},
));
buttons.add(AdaptiveFlatButton(
child: Text(widget.l10n.next),
onPressed: () async {
setState(() {
_recoveryKeyInputError = null;
_recoveryKeyInputLoading = true;
});
try {
final input = _recoveryKeyTextEditingController.text.trim();
await bootstrap.newSsssKey.unlock(
keyOrPassphrase: input,
);
await bootstrap.openExistingSsss();
if (widget.client.encryption.crossSigning.enabled) {
Logs().v(
'Cross signing is already enabled. Try to self-sign');
try {
await widget.client.encryption.crossSigning
.selfSign(recoveryKey: input);
} catch (e, s) {
// ignore: unawaited_futures
SentryController.captureException(
'Unable to self sign with recovery key after successfully open existing SSSS: ${e.toString()}',
s);
}
}
} catch (e, s) {
Logs().w('Unable to unlock SSSS', e, s);
setState(() => _recoveryKeyInputError =
L10n.of(context).oopsSomethingWentWrong);
} 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:
titleText = widget.l10n.oopsSomethingWentWrong;
body = ListTile(
contentPadding: EdgeInsets.zero,
leading: Icon(Icons.error_outline, color: Colors.red),
title: Text(widget.l10n.oopsSomethingWentWrong),
);
buttons.add(AdaptiveFlatButton(
child: Text(widget.l10n.close),
onPressed: () => Navigator.of(context).pop<bool>(false),
));
break;
case BootstrapState.done:
titleText = 'Process completed';
body = ListTile(
contentPadding: EdgeInsets.zero,
leading: Icon(Icons.check_circle, color: Colors.green),
title: Text('Chat backup has been initialized!'),
);
buttons.add(AdaptiveFlatButton(
child: Text(widget.l10n.close),
onPressed: () => Navigator.of(context).pop<bool>(false),
));
break;
}
} }
final title = Text(titleText); final title = Text(titleText);
@ -229,82 +258,3 @@ class _BootstrapDialogState extends State<BootstrapDialog> {
); );
} }
} }
class _AskUnlockOldSsss extends StatefulWidget {
final String keyId;
final OpenSSSS ssssKey;
final L10n l10n;
_AskUnlockOldSsss(this.keyId, this.ssssKey, this.l10n);
@override
_AskUnlockOldSsssState createState() => _AskUnlockOldSsssState();
}
class _AskUnlockOldSsssState extends State<_AskUnlockOldSsss> {
bool valid = false;
TextEditingController textEditingController = TextEditingController();
String input;
void checkInput(BuildContext context) async {
if (input == null) {
return;
}
valid = (await showFutureLoadingDialog(
context: context,
future: () => widget.ssssKey.unlock(keyOrPassphrase: input),
))
.error ==
null;
setState(() => null);
}
@override
Widget build(BuildContext build) {
if (valid) {
return Row(
children: <Widget>[
Text(widget.keyId),
Text('unlocked'),
],
mainAxisSize: MainAxisSize.min,
);
}
return Row(
children: <Widget>[
Text(widget.keyId),
Flexible(
child: TextField(
controller: textEditingController,
autofocus: false,
autocorrect: false,
onSubmitted: (s) {
input = s;
checkInput(context);
},
minLines: 1,
maxLines: 1,
obscureText: true,
decoration: InputDecoration(
hintText: widget.l10n.passphraseOrKey,
prefixStyle: TextStyle(color: Theme.of(context).primaryColor),
suffixStyle: TextStyle(color: Theme.of(context).primaryColor),
border: OutlineInputBorder(),
),
),
),
RaisedButton(
color: Theme.of(context).primaryColor,
elevation: 5,
textColor: Colors.white,
child: Text(widget.l10n.submit),
onPressed: () {
input = textEditingController.text;
checkInput(context);
},
),
],
mainAxisSize: MainAxisSize.min,
);
}
}