From 45c562626787e41e8483e532b9927f06b8d0c1d9 Mon Sep 17 00:00:00 2001 From: Valere Date: Thu, 2 Apr 2020 18:30:43 +0200 Subject: [PATCH] Add generate key option --- .../im/vector/riotx/features/MainActivity.kt | 3 ++- .../crypto/recover/BootstrapActions.kt | 1 + .../crypto/recover/BootstrapBottomSheet.kt | 25 ++++++++++------- .../recover/BootstrapCrossSigningTask.kt | 27 ++++++++++++------- .../recover/BootstrapSharedViewModel.kt | 22 ++++++++++++--- .../crypto/recover/BootstrapViewEvents.kt | 2 +- vector/src/main/res/values/strings_riotX.xml | 2 ++ 7 files changed, 57 insertions(+), 25 deletions(-) diff --git a/vector/src/main/java/im/vector/riotx/features/MainActivity.kt b/vector/src/main/java/im/vector/riotx/features/MainActivity.kt index bc5a1aff95..c894e0739c 100644 --- a/vector/src/main/java/im/vector/riotx/features/MainActivity.kt +++ b/vector/src/main/java/im/vector/riotx/features/MainActivity.kt @@ -195,7 +195,8 @@ class MainActivity : VectorBaseActivity() { // We have a session. // Check it can be opened if (sessionHolder.getActiveSession().isOpenable) { - HomeActivity.newIntent(this) + // DO NOT COMMIT + HomeActivity.newIntent(this, accountCreation = true) } else { // The token is still invalid SoftLogoutActivity.newIntent(this) diff --git a/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapActions.kt b/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapActions.kt index 4286c9e571..7c0f2c1c46 100644 --- a/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapActions.kt +++ b/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapActions.kt @@ -30,6 +30,7 @@ sealed class BootstrapActions : VectorViewModelAction { object GoToEnterAccountPassword : BootstrapActions() data class DoInitialize(val passphrase: String, val auth: UserPasswordAuth? = null) : BootstrapActions() + data class DoInitializeGeneratedKey(val auth: UserPasswordAuth? = null) : BootstrapActions() object TogglePasswordVisibility : BootstrapActions() data class UpdateCandidatePassphrase(val pass: String) : BootstrapActions() data class UpdateConfirmCandidatePassphrase(val pass: String) : BootstrapActions() diff --git a/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapBottomSheet.kt b/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapBottomSheet.kt index 6812997ffa..6305f161e3 100644 --- a/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapBottomSheet.kt +++ b/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapBottomSheet.kt @@ -55,30 +55,35 @@ class BootstrapBottomSheet : VectorBaseBottomSheetDialogFragment() { super.onViewCreated(view, savedInstanceState) viewModel.observeViewEvents { event -> when (event) { - is BootstrapViewEvents.Dismiss -> dismiss() - is BootstrapViewEvents.ModalError -> { + is BootstrapViewEvents.Dismiss -> dismiss() + is BootstrapViewEvents.ModalError -> { AlertDialog.Builder(requireActivity()) .setTitle(R.string.dialog_title_error) .setMessage(event.error) .setPositiveButton(R.string.ok, null) .show() } - BootstrapViewEvents.RecoveryKeySaved -> { + BootstrapViewEvents.RecoveryKeySaved -> { KeepItSafeDialog().show(requireActivity()) } - BootstrapViewEvents.SkipBootstrap -> { - promptSkip() + is BootstrapViewEvents.SkipBootstrap -> { + promptSkip(event.genKeyOption) } } } } - private fun promptSkip() { - AlertDialog.Builder(requireActivity()) + private fun promptSkip(genKeyOption: Boolean) { + AlertDialog.Builder(requireContext()) .setTitle(R.string.are_you_sure) - .setMessage(R.string.bootstrap_skip_text) + .setMessage(if (genKeyOption) R.string.bootstrap_skip_text else R.string.bootstrap_skip_text_no_gen_key) .setPositiveButton(R.string._continue, null) - .setNeutralButton(R.string.generate_message_key) { _, _ -> + .apply { + if (genKeyOption) { + setNeutralButton(R.string.generate_message_key) { _, _ -> + viewModel.handle(BootstrapActions.DoInitializeGeneratedKey()) + } + } } .setNegativeButton(R.string.skip) { _, _ -> dismiss() @@ -118,7 +123,7 @@ class BootstrapBottomSheet : VectorBaseBottomSheetDialogFragment() { bootstrapTitleText.text = getString(R.string.confirm_recovery_passphrase, getString(R.string.recovery_passphrase)) showFragment(BootstrapConfirmPassphraseFragment::class, Bundle()) } - is BootstrapStep.AccountPassword -> { + is BootstrapStep.AccountPassword -> { bootstrapIcon.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.ic_user)) bootstrapTitleText.text = getString(R.string.account_password) showFragment(BootstrapAccountPasswordFragment::class, Bundle()) diff --git a/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapCrossSigningTask.kt b/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapCrossSigningTask.kt index 7c579d8d74..9e9a579bde 100644 --- a/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapCrossSigningTask.kt +++ b/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapCrossSigningTask.kt @@ -64,7 +64,7 @@ interface BootstrapProgressListener { data class Params( val userPasswordAuth: UserPasswordAuth? = null, val progressListener: BootstrapProgressListener? = null, - val passphrase: String + val passphrase: String? ) class BootstrapCrossSigningTask @Inject constructor( @@ -100,14 +100,23 @@ class BootstrapCrossSigningTask @Inject constructor( params.progressListener?.onProgress(WaitingViewData(stringProvider.getString(R.string.bootstrap_crosssigning_progress_pbkdf2), isIndeterminate = true)) try { keyInfo = awaitCallback { - ssssService.generateKeyWithPassphrase( - UUID.randomUUID().toString(), - "ssss_key", - params.passphrase, - EmptyKeySigner(), - null, - it - ) + params.passphrase?.let { passphrase -> + ssssService.generateKeyWithPassphrase( + UUID.randomUUID().toString(), + "ssss_key", + passphrase, + EmptyKeySigner(), + null, + it + ) + } ?: kotlin.run { + ssssService.generateKey( + UUID.randomUUID().toString(), + "ssss_key", + EmptyKeySigner(), + it + ) + } } } catch (failure: Failure) { return BootstrapResult.FailedToCreateSSSSKey(failure) diff --git a/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapSharedViewModel.kt b/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapSharedViewModel.kt index 0e9395468f..998899374c 100644 --- a/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapSharedViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapSharedViewModel.kt @@ -151,6 +151,20 @@ class BootstrapSharedViewModel @AssistedInject constructor( } } } + is BootstrapActions.DoInitializeGeneratedKey -> { + val auth = action.auth ?: reAuthHelper.rememberedAuth() + if (auth == null) { + setState { + copy( + passphrase = null, + passphraseRepeat = null, + step = BootstrapStep.AccountPassword(false) + ) + } + } else { + startInitializeFlow(action.auth) + } + } BootstrapActions.RecoveryKeySaved -> { _viewEvents.post(BootstrapViewEvents.RecoveryKeySaved) setState { @@ -237,7 +251,7 @@ class BootstrapSharedViewModel @AssistedInject constructor( bootstrapTask.invoke(this, Params( userPasswordAuth = auth ?: reAuthHelper.rememberedAuth(), progressListener = progressListener, - passphrase = state.passphrase!! + passphrase = state.passphrase )) { when (it) { is BootstrapResult.Success -> { @@ -297,7 +311,7 @@ class BootstrapSharedViewModel @AssistedInject constructor( when (state.step) { is BootstrapStep.SetupPassphrase -> { // do we let you cancel from here? - _viewEvents.post(BootstrapViewEvents.SkipBootstrap) + _viewEvents.post(BootstrapViewEvents.SkipBootstrap()) } is BootstrapStep.ConfirmPassphrase -> { setState { @@ -309,11 +323,11 @@ class BootstrapSharedViewModel @AssistedInject constructor( } } is BootstrapStep.AccountPassword -> { - _viewEvents.post(BootstrapViewEvents.SkipBootstrap) + _viewEvents.post(BootstrapViewEvents.SkipBootstrap(state.passphrase != null)) } BootstrapStep.Initializing -> { // do we let you cancel from here? - _viewEvents.post(BootstrapViewEvents.SkipBootstrap) + _viewEvents.post(BootstrapViewEvents.SkipBootstrap(state.passphrase != null)) } is BootstrapStep.SaveRecoveryKey, BootstrapStep.DoneSuccess -> { diff --git a/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapViewEvents.kt b/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapViewEvents.kt index efac9c9e34..679eabd561 100644 --- a/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapViewEvents.kt +++ b/vector/src/main/java/im/vector/riotx/features/crypto/recover/BootstrapViewEvents.kt @@ -22,5 +22,5 @@ sealed class BootstrapViewEvents : VectorViewEvents { object Dismiss : BootstrapViewEvents() data class ModalError(val error: String) : BootstrapViewEvents() object RecoveryKeySaved: BootstrapViewEvents() - object SkipBootstrap: BootstrapViewEvents() + data class SkipBootstrap(val genKeyOption: Boolean = true): BootstrapViewEvents() } diff --git a/vector/src/main/res/values/strings_riotX.xml b/vector/src/main/res/values/strings_riotX.xml index c875cb9ea9..c850316912 100644 --- a/vector/src/main/res/values/strings_riotX.xml +++ b/vector/src/main/res/values/strings_riotX.xml @@ -84,6 +84,8 @@ You cannot do that from mobile Setting a Message Password lets you secure & unlock encrypted messages and trust.\n\nIf you don’t want to set a Message Password, generate a Message Key instead. + Setting a Message Password lets you secure & unlock encrypted messages and trust. +