Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TW-1766: fix 500,404 error in POST request when login #1946

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions lib/pages/bootstrap/bootstrap_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import 'package:matrix/encryption/utils/bootstrap.dart';
import 'package:matrix/matrix.dart';
import 'package:share_plus/share_plus.dart';

import '../key_verification/key_verification_dialog.dart';

class BootstrapDialog extends StatefulWidget {
final bool wipe;
final Client client;
Expand Down Expand Up @@ -333,25 +331,26 @@ class BootstrapDialogState extends State<BootstrapDialog> {
const Expanded(child: Divider()),
],
),
const SizedBox(height: 16),
ElevatedButton.icon(
icon: const Icon(Icons.cast_connected_outlined),
label: Text(L10n.of(context)!.transferFromAnotherDevice),
onPressed: _recoveryKeyInputLoading
? null
: () async {
final req = await TwakeDialog
.showFutureLoadingDialogFullScreen(
future: () => widget.client
.userDeviceKeys[widget.client.userID!]!
.startVerification(),
);
if (req.error != null) return;
await KeyVerificationDialog(request: req.result!)
.show(context);
Navigator.of(context, rootNavigator: false).pop();
},
),
// TODO: TW-1766: temporary disable right now, because not supported yet
// const SizedBox(height: 16),
// ElevatedButton.icon(
// icon: const Icon(Icons.cast_connected_outlined),
// label: Text(L10n.of(context)!.transferFromAnotherDevice),
// onPressed: _recoveryKeyInputLoading
// ? null
// : () async {
// final req = await TwakeDialog
// .showFutureLoadingDialogFullScreen(
// future: () => widget.client
// .userDeviceKeys[widget.client.userID!]!
// .startVerification(),
// );
// if (req.error != null) return;
// await KeyVerificationDialog(request: req.result!)
// .show(context);
// Navigator.of(context, rootNavigator: false).pop();
// },
// ),
const SizedBox(height: 16),
ElevatedButton.icon(
style: ElevatedButton.styleFrom(
Expand Down
104 changes: 62 additions & 42 deletions lib/pages/bootstrap/tom_bootstrap_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:linagora_design_flutter/colors/linagora_sys_colors.dart';
import 'package:lottie/lottie.dart';
import 'package:matrix/encryption.dart';
import 'package:matrix/encryption/utils/bootstrap.dart';
import 'package:matrix/matrix.dart';

class TomBootstrapDialog extends StatefulWidget {
Expand Down Expand Up @@ -57,19 +56,17 @@ class TomBootstrapDialogState extends State<TomBootstrapDialog>
@override
void initState() {
super.initState();
bootstrap =
widget.client.encryption!.bootstrap(onUpdate: (_) => setState(() {}));
_createBootstrap();
_initData();
}

void _createBootstrap() async {
void _initData() async {
WidgetsBinding.instance.addPostFrameCallback((_) async {
await _loadingData();
});
}

Future<void> setupAdditionalDioCacheOption(String userId) async {
Logs().d('TomBootstrapDialog::setupAdditionalDioCacheOption: $userId');
Logs().i('TomBootstrapDialog::setupAdditionalDioCacheOption: $userId');
nqhhdev marked this conversation as resolved.
Show resolved Hide resolved
DioCacheInterceptorForClient(userId).setup(getIt);
}

Expand All @@ -83,8 +80,11 @@ class TomBootstrapDialogState extends State<TomBootstrapDialog>

Future<void> _loadingData() async {
_uploadRecoveryKeyState = UploadRecoveryKeyState.dataLoading;
Logs().i('_loadingData: $_uploadRecoveryKeyState');
await widget.client.roomsLoading;
await widget.client.accountDataLoading;

Logs().i('_loadingData: roomDataLoaded & accountDataLoaded');
if (widget.client.userID != null) {
await setupAdditionalDioCacheOption(widget.client.userID!);
}
Expand All @@ -107,30 +107,32 @@ class TomBootstrapDialogState extends State<TomBootstrapDialog>

// Display first login bootstrap if enabled
if (widget.client.encryption?.keyManager.enabled == true) {
Logs().d(
Logs().i(
'TomBootstrapDialog::_initializeRecoveryKeyState: Showing bootstrap dialog when encryption is enabled',
);
if (await widget.client.encryption?.keyManager.isCached() == false ||
await widget.client.encryption?.crossSigning.isCached() == false ||
widget.client.isUnknownSession && mounted) {
final recoveryWords = await _getRecoveryWords();
_createBootstrap();
if (recoveryWords != null) {
_recoveryWords = recoveryWords;
_uploadRecoveryKeyState = UploadRecoveryKeyState.useExisting;
setState(() {});
return;
} else {
Logs().d(
Logs().i(
'TomBootstrapDialog::_initializeRecoveryKeyState(): no recovery existed then call bootstrap',
);
Navigator.of(context, rootNavigator: false).pop<bool>(false);
}
}
} else {
Logs().d(
Logs().i(
'TomBootstrapDialog::_initializeRecoveryKeyState(): encryption is not enabled',
);
final recoveryWords = await _getRecoveryWords();
_createBootstrap();
_wipe = recoveryWords != null;
if (recoveryWords != null) {
_uploadRecoveryKeyState = UploadRecoveryKeyState.wipeRecovery;
Expand All @@ -142,6 +144,11 @@ class TomBootstrapDialogState extends State<TomBootstrapDialog>
}
}

void _createBootstrap() {
bootstrap =
widget.client.encryption!.bootstrap(onUpdate: (_) => setState(() {}));
}

bool get isDataLoadingState =>
_uploadRecoveryKeyState == UploadRecoveryKeyState.dataLoading;

Expand All @@ -160,11 +167,11 @@ class TomBootstrapDialogState extends State<TomBootstrapDialog>

@override
Widget build(BuildContext context) {
Logs().d(
Logs().i(
'TomBootstrapDialogState::build(): BootstrapState = ${bootstrap?.state}',
);

Logs().d(
Logs().i(
'TomBootstrapDialogState::build(): RecoveryKeyState = $_uploadRecoveryKeyState',
);

Expand All @@ -186,12 +193,12 @@ class TomBootstrapDialogState extends State<TomBootstrapDialog>
break;
case UploadRecoveryKeyState.created:
if (_createNewRecoveryKeySuccess()) {
Logs().d(
Logs().i(
'TomBootstrapDialogState::build(): start backup process with key ${bootstrap?.newSsssKey!.recoveryKey}',
);
final key = bootstrap?.newSsssKey!.recoveryKey;
WidgetsBinding.instance.addPostFrameCallback((_) {
Logs().d(
Logs().i(
'TomBootstrapDialogState::build(): check if key is already in TOM = ${_existedRecoveryWordsInTom(
key,
)} - ${_recoveryWords?.words}',
Expand All @@ -216,7 +223,7 @@ class TomBootstrapDialogState extends State<TomBootstrapDialog>
});
break;
case UploadRecoveryKeyState.uploadError:
Logs().e('TomBootstrapDialogState::build(): upload recovery key error');
Logs().i('TomBootstrapDialogState::build(): upload recovery key error');
WidgetsBinding.instance.addPostFrameCallback((_) {
Navigator.of(context, rootNavigator: false).pop<bool>();
});
Expand Down Expand Up @@ -269,6 +276,9 @@ class TomBootstrapDialogState extends State<TomBootstrapDialog>
}

bool _existedRecoveryWordsInTom(String? key) {
Logs().i(
'TomBootstrapDialogState::_existedRecoveryWordsInTom(): $key, $_recoveryWords',
);
if (key == null && _recoveryWords != null) {
return true;
}
Expand All @@ -285,6 +295,9 @@ class TomBootstrapDialogState extends State<TomBootstrapDialog>
_uploadRecoveryKeyState != UploadRecoveryKeyState.checkingRecoveryWork;

void _handleBootstrapState() {
Logs().i(
'TomBootstrapDialogState::_handleBootstrapState(): ${bootstrap?.state}',
);
if (bootstrap != null && _setUpSuccess) {
switch (bootstrap!.state) {
case BootstrapState.loading:
Expand Down Expand Up @@ -364,35 +377,35 @@ class TomBootstrapDialogState extends State<TomBootstrapDialog>
}

Future<void> _wipeRecoveryWord() async {
await _deleteRecoveryWordsInteractor.execute().then(
(either) => either.fold(
(failure) {
Logs().e(
'TomBootstrapDialogState::_wipeRecoveryWord(): wipe recoveryWords failed',
);
if (Matrix.of(context).twakeSupported) {
setState(
() => _uploadRecoveryKeyState =
UploadRecoveryKeyState.wipeRecoveryFailed,
);
} else {
setState(
() =>
_uploadRecoveryKeyState = UploadRecoveryKeyState.initial,
);
}
},
(success) => setState(
await _deleteRecoveryWordsInteractor.execute().then((either) {
_createBootstrap();
either.fold(
(failure) {
nqhhdev marked this conversation as resolved.
Show resolved Hide resolved
Logs().i(
'TomBootstrapDialogState::_wipeRecoveryWord(): wipe recoveryWords failed',
);
if (Matrix.of(context).twakeSupported) {
setState(
() => _uploadRecoveryKeyState =
UploadRecoveryKeyState.wipeRecoveryFailed,
);
} else {
setState(
() => _uploadRecoveryKeyState = UploadRecoveryKeyState.initial,
),
),
);
);
}
},
(success) => setState(() {
_uploadRecoveryKeyState = UploadRecoveryKeyState.initial;
}),
);
});
}

Future<void> _backUpInRecoveryVault(String? key) async {
if (key == null) {
setState(() {
Logs().d(
Logs().i(
'TomBootstrapDialogState::_backUpInRecoveryVault(): key null, upload failed',
);
_uploadRecoveryKeyState = UploadRecoveryKeyState.uploadError;
Expand All @@ -401,7 +414,7 @@ class TomBootstrapDialogState extends State<TomBootstrapDialog>
await _saveRecoveryWordsInteractor.execute(key!).then(
(either) => either.fold(
(failure) {
Logs().d(
Logs().i(
'TomBootstrapDialogState::_backUpInRecoveryVault(): upload recoveryWords failed',
);
setState(
Expand All @@ -419,29 +432,36 @@ class TomBootstrapDialogState extends State<TomBootstrapDialog>
Future<void> _unlockBackUp() async {
final recoveryWords = _recoveryWords;
if (recoveryWords == null) {
Logs().e('TomBootstrapDialogState::_unlockBackUp(): recoveryWords null');
Logs().i('TomBootstrapDialogState::_unlockBackUp(): recoveryWords null');
setState(() {
_uploadRecoveryKeyState = UploadRecoveryKeyState.unlockError;
});
return;
}
try {
Logs().d('TomBootstrapDialogState::_unlockBackUp() unlocking');
Logs().i(
'TomBootstrapDialogState::_unlockBackUp() unlocking: ${recoveryWords.words}',
);
await bootstrap?.newSsssKey!.unlock(
keyOrPassphrase: recoveryWords.words,
);
Logs().d('TomBootstrapDialogState::_unlockBackUp() self Signing');
Logs().i('TomBootstrapDialogState::_unlockBackUp() self Signing');
await bootstrap?.client.encryption!.crossSigning.selfSign(
keyOrPassphrase: recoveryWords.words,
);
Logs().d('TomBootstrapDialogState::_unlockBackUp() open existing SSSS');
Logs().i('TomBootstrapDialogState::_unlockBackUp() open existing SSSS');
await bootstrap?.openExistingSsss();
} catch (e, s) {
Logs().w(
'TomBootstrapDialogState::_unlockBackUp() Unable to unlock SSSS',
e,
s,
);
if (e is InvalidPassphraseException) {
Logs().i(
'TomBootstrapDialogState::_unlockBackUp(): InvalidPassphraseException: ${e.cause}',
);
}
setState(() {
_uploadRecoveryKeyState = UploadRecoveryKeyState.unlockError;
});
Expand Down