From ad9873c565bc22f4e66edbc2758d69fb76c56625 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 3 Feb 2021 14:52:58 +0100 Subject: [PATCH] Some cleanup --- .../matrix/android/sdk/api/session/Session.kt | 2 +- .../android/sdk/internal/auth/Constants.kt | 3 + .../sdk/internal/session/DefaultSession.kt | 9 +-- .../app/features/auth/ReAuthViewModel.kt | 2 +- .../crypto/recover/BootstrapReAuthFragment.kt | 3 +- .../recover/BootstrapSharedViewModel.kt | 72 +++++++++---------- 6 files changed, 47 insertions(+), 44 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt index bc6fd3adad..ff7c9f0521 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt @@ -245,7 +245,7 @@ interface Session : val sharedSecretStorageService: SharedSecretStorageService - fun getUIASsoFallbackUrl(authenticationSessionId: String): String + fun getUiaSsoFallbackUrl(authenticationSessionId: String): String /** * Maintenance API, allows to print outs info on DB size to logcat diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/Constants.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/Constants.kt index 642279cc27..e0c52cf9ca 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/Constants.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/Constants.kt @@ -36,3 +36,6 @@ internal const val SSO_REDIRECT_PATH = "/_matrix/client/r0/login/sso/redirect" internal const val MSC2858_SSO_REDIRECT_PATH = "/_matrix/client/unstable/org.matrix.msc2858/login/sso/redirect" internal const val SSO_REDIRECT_URL_PARAM = "redirectUrl" + +// Ref: https://matrix.org/docs/spec/client_server/r0.6.1#single-sign-on +internal const val SSO_UIA_FALLBACK_PATH = "/_matrix/client/r0/auth/m.login.sso/fallback/web" diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt index 92337e6185..e09c051c81 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt @@ -52,6 +52,8 @@ import org.matrix.android.sdk.api.session.terms.TermsService import org.matrix.android.sdk.api.session.typing.TypingUsersTracker import org.matrix.android.sdk.api.session.user.UserService import org.matrix.android.sdk.api.session.widgets.WidgetService +import org.matrix.android.sdk.api.util.appendParamToUrl +import org.matrix.android.sdk.internal.auth.SSO_UIA_FALLBACK_PATH import org.matrix.android.sdk.internal.auth.SessionParamsStore import org.matrix.android.sdk.internal.crypto.DefaultCryptoService import org.matrix.android.sdk.internal.database.tools.RealmDebugTools @@ -273,16 +275,15 @@ internal class DefaultSession @Inject constructor( return "$myUserId - ${sessionParams.deviceId}" } - override fun getUIASsoFallbackUrl(authenticationSessionId: String): String { + override fun getUiaSsoFallbackUrl(authenticationSessionId: String): String { val hsBas = sessionParams.homeServerConnectionConfig .homeServerUri .toString() .trim { it == '/' } return buildString { append(hsBas) - append("/_matrix/client/r0/auth/m.login.sso/fallback/web") - append("?session=") - append(authenticationSessionId) + append(SSO_UIA_FALLBACK_PATH) + appendParamToUrl("session", authenticationSessionId) } } diff --git a/vector/src/main/java/im/vector/app/features/auth/ReAuthViewModel.kt b/vector/src/main/java/im/vector/app/features/auth/ReAuthViewModel.kt index a946a91ced..4204da0d24 100644 --- a/vector/src/main/java/im/vector/app/features/auth/ReAuthViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/auth/ReAuthViewModel.kt @@ -55,7 +55,7 @@ class ReAuthViewModel @AssistedInject constructor( ReAuthActions.StartSSOFallback -> { if (state.flowType == LoginFlowTypes.SSO) { setState { copy(ssoFallbackPageWasShown = true) } - val ssoURL = session.getUIASsoFallbackUrl(initialState.session ?: "") + val ssoURL = session.getUiaSsoFallbackUrl(initialState.session ?: "") _viewEvents.post(ReAuthEvents.OpenSsoURl(ssoURL)) } } diff --git a/vector/src/main/java/im/vector/app/features/crypto/recover/BootstrapReAuthFragment.kt b/vector/src/main/java/im/vector/app/features/crypto/recover/BootstrapReAuthFragment.kt index b23077a6d7..507050c2e8 100644 --- a/vector/src/main/java/im/vector/app/features/crypto/recover/BootstrapReAuthFragment.kt +++ b/vector/src/main/java/im/vector/app/features/crypto/recover/BootstrapReAuthFragment.kt @@ -70,13 +70,12 @@ class BootstrapReAuthFragment @Inject constructor( return@withState } val failure = state.step.failure + views.reAuthFailureText.setTextOrHide(failure) if (failure == null) { - views.reAuthFailureText.setTextOrHide(null) views.waitingProgress.isVisible = true views.bootstrapCancelButton.isVisible = false views.bootstrapRetryButton.isVisible = false } else { - views.reAuthFailureText.setTextOrHide(failure) views.waitingProgress.isVisible = false views.bootstrapCancelButton.isVisible = true views.bootstrapRetryButton.isVisible = true diff --git a/vector/src/main/java/im/vector/app/features/crypto/recover/BootstrapSharedViewModel.kt b/vector/src/main/java/im/vector/app/features/crypto/recover/BootstrapSharedViewModel.kt index 9180c92cbf..42278cd948 100644 --- a/vector/src/main/java/im/vector/app/features/crypto/recover/BootstrapSharedViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/crypto/recover/BootstrapSharedViewModel.kt @@ -86,7 +86,7 @@ class BootstrapSharedViewModel @AssistedInject constructor( when (args.setUpMode) { SetupMode.PASSPHRASE_RESET, SetupMode.PASSPHRASE_AND_NEEDED_SECRETS_RESET, - SetupMode.HARD_RESET -> { + SetupMode.HARD_RESET -> { setState { copy(step = BootstrapStep.FirstForm(keyBackUpExist = false, reset = true)) } @@ -97,7 +97,7 @@ class BootstrapSharedViewModel @AssistedInject constructor( copy(step = BootstrapStep.AccountReAuth()) } } - SetupMode.NORMAL -> { + SetupMode.NORMAL -> { // need to check if user have an existing keybackup setState { copy(step = BootstrapStep.CheckingMigration) @@ -149,8 +149,8 @@ class BootstrapSharedViewModel @AssistedInject constructor( override fun handle(action: BootstrapActions) = withState { state -> when (action) { - is BootstrapActions.GoBack -> queryBack() - BootstrapActions.TogglePasswordVisibility -> { + is BootstrapActions.GoBack -> queryBack() + BootstrapActions.TogglePasswordVisibility -> { when (state.step) { is BootstrapStep.SetupPassphrase -> { setState { @@ -173,13 +173,13 @@ class BootstrapSharedViewModel @AssistedInject constructor( else -> Unit } } - BootstrapActions.StartKeyBackupMigration -> { + BootstrapActions.StartKeyBackupMigration -> { handleStartMigratingKeyBackup() } - is BootstrapActions.Start -> { + is BootstrapActions.Start -> { handleStart(action) } - is BootstrapActions.UpdateCandidatePassphrase -> { + is BootstrapActions.UpdateCandidatePassphrase -> { val strength = zxcvbn.measure(action.pass) setState { copy( @@ -188,7 +188,7 @@ class BootstrapSharedViewModel @AssistedInject constructor( ) } } - is BootstrapActions.GoToConfirmPassphrase -> { + is BootstrapActions.GoToConfirmPassphrase -> { setState { copy( passphrase = action.passphrase, @@ -205,7 +205,7 @@ class BootstrapSharedViewModel @AssistedInject constructor( ) } } - is BootstrapActions.DoInitialize -> { + is BootstrapActions.DoInitialize -> { if (state.passphrase == state.passphraseRepeat) { startInitializeFlow(state) } else { @@ -216,42 +216,42 @@ class BootstrapSharedViewModel @AssistedInject constructor( } } } - is BootstrapActions.DoInitializeGeneratedKey -> { + is BootstrapActions.DoInitializeGeneratedKey -> { startInitializeFlow(state) } - BootstrapActions.RecoveryKeySaved -> { + BootstrapActions.RecoveryKeySaved -> { _viewEvents.post(BootstrapViewEvents.RecoveryKeySaved) setState { copy(step = BootstrapStep.SaveRecoveryKey(true)) } } - BootstrapActions.Completed -> { + BootstrapActions.Completed -> { _viewEvents.post(BootstrapViewEvents.Dismiss(true)) } - BootstrapActions.GoToCompleted -> { + BootstrapActions.GoToCompleted -> { setState { copy(step = BootstrapStep.DoneSuccess) } } - BootstrapActions.SaveReqQueryStarted -> { + BootstrapActions.SaveReqQueryStarted -> { setState { copy(recoverySaveFileProcess = Loading()) } } - is BootstrapActions.SaveKeyToUri -> { + is BootstrapActions.SaveKeyToUri -> { saveRecoveryKeyToUri(action.os) } - BootstrapActions.SaveReqFailed -> { + BootstrapActions.SaveReqFailed -> { setState { copy(recoverySaveFileProcess = Uninitialized) } } - BootstrapActions.GoToEnterAccountPassword -> { + BootstrapActions.GoToEnterAccountPassword -> { setState { copy(step = BootstrapStep.AccountReAuth()) } } - BootstrapActions.HandleForgotBackupPassphrase -> { + BootstrapActions.HandleForgotBackupPassphrase -> { if (state.step is BootstrapStep.GetBackupSecretPassForMigration) { setState { copy(step = BootstrapStep.GetBackupSecretPassForMigration(state.step.isPasswordVisible, true)) @@ -261,16 +261,16 @@ class BootstrapSharedViewModel @AssistedInject constructor( // is BootstrapActions.ReAuth -> { // startInitializeFlow(action.pass) // } - is BootstrapActions.DoMigrateWithPassphrase -> { + is BootstrapActions.DoMigrateWithPassphrase -> { startMigrationFlow(state.step, action.passphrase, null) } - is BootstrapActions.DoMigrateWithRecoveryKey -> { + is BootstrapActions.DoMigrateWithRecoveryKey -> { startMigrationFlow(state.step, null, action.recoveryKey) } - BootstrapActions.SsoAuthDone -> { + BootstrapActions.SsoAuthDone -> { uiaContinuation?.resume(DefaultBaseAuth(session = pendingAuth?.session ?: "")) } - is BootstrapActions.PasswordAuthDone -> { + is BootstrapActions.PasswordAuthDone -> { val decryptedPass = session.loadSecureSecret(action.password.fromBase64().inputStream(), ReAuthActivity.DEFAULT_RESULT_KEYSTORE_ALIAS) uiaContinuation?.resume( UserPasswordAuth( @@ -280,7 +280,7 @@ class BootstrapSharedViewModel @AssistedInject constructor( ) ) } - BootstrapActions.ReAuthCancelled -> { + BootstrapActions.ReAuthCancelled -> { setState { copy(step = BootstrapStep.AccountReAuth(stringProvider.getString(R.string.authentication_error))) } @@ -410,7 +410,7 @@ class BootstrapSharedViewModel @AssistedInject constructor( } _viewEvents.post(BootstrapViewEvents.RequestReAuth(flowResponse, errCode)) } - LoginFlowTypes.SSO -> { + LoginFlowTypes.SSO -> { pendingAuth = DefaultBaseAuth(flowResponse.session) uiaContinuation = promise setState { @@ -441,7 +441,7 @@ class BootstrapSharedViewModel @AssistedInject constructor( is BootstrapResult.SuccessCrossSigningOnly -> { _viewEvents.post(BootstrapViewEvents.Dismiss(true)) } - is BootstrapResult.Success -> { + is BootstrapResult.Success -> { setState { copy( recoveryKeyCreationInfo = bootstrapResult.keyInfo, @@ -452,7 +452,7 @@ class BootstrapSharedViewModel @AssistedInject constructor( ) } } - is BootstrapResult.InvalidPasswordError -> { + is BootstrapResult.InvalidPasswordError -> { // it's a bad password / auth setState { copy( @@ -460,7 +460,7 @@ class BootstrapSharedViewModel @AssistedInject constructor( ) } } - is BootstrapResult.Failure -> { + is BootstrapResult.Failure -> { if (bootstrapResult is BootstrapResult.GenericError && bootstrapResult.failure is Failure.OtherServerError && bootstrapResult.failure.httpCode == 401) { @@ -510,7 +510,7 @@ class BootstrapSharedViewModel @AssistedInject constructor( } } } - is BootstrapStep.SetupPassphrase -> { + is BootstrapStep.SetupPassphrase -> { setState { copy( step = BootstrapStep.FirstForm(keyBackUpExist = doesKeyBackupExist), @@ -520,7 +520,7 @@ class BootstrapSharedViewModel @AssistedInject constructor( ) } } - is BootstrapStep.ConfirmPassphrase -> { + is BootstrapStep.ConfirmPassphrase -> { setState { copy( step = BootstrapStep.SetupPassphrase( @@ -529,19 +529,19 @@ class BootstrapSharedViewModel @AssistedInject constructor( ) } } - is BootstrapStep.AccountReAuth -> { + is BootstrapStep.AccountReAuth -> { _viewEvents.post(BootstrapViewEvents.SkipBootstrap(state.passphrase != null)) } - BootstrapStep.Initializing -> { + BootstrapStep.Initializing -> { // do we let you cancel from here? _viewEvents.post(BootstrapViewEvents.SkipBootstrap(state.passphrase != null)) } is BootstrapStep.SaveRecoveryKey, - BootstrapStep.DoneSuccess -> { + BootstrapStep.DoneSuccess -> { // nop } - BootstrapStep.CheckingMigration -> Unit - is BootstrapStep.FirstForm -> { + BootstrapStep.CheckingMigration -> Unit + is BootstrapStep.FirstForm -> { _viewEvents.post( when (args.setUpMode) { SetupMode.CROSS_SIGNING_ONLY, @@ -550,7 +550,7 @@ class BootstrapSharedViewModel @AssistedInject constructor( } ) } - is BootstrapStep.GetBackupSecretForMigration -> { + is BootstrapStep.GetBackupSecretForMigration -> { setState { copy( step = BootstrapStep.FirstForm(keyBackUpExist = doesKeyBackupExist), @@ -568,7 +568,7 @@ class BootstrapSharedViewModel @AssistedInject constructor( private fun BackupToQuadSMigrationTask.Result.Failure.toHumanReadable(): String { return when (this) { is BackupToQuadSMigrationTask.Result.InvalidRecoverySecret -> stringProvider.getString(R.string.keys_backup_passphrase_error_decrypt) - is BackupToQuadSMigrationTask.Result.ErrorFailure -> errorFormatter.toHumanReadable(throwable) + is BackupToQuadSMigrationTask.Result.ErrorFailure -> errorFormatter.toHumanReadable(throwable) // is BackupToQuadSMigrationTask.Result.NoKeyBackupVersion, // is BackupToQuadSMigrationTask.Result.IllegalParams, else -> stringProvider.getString(R.string.unexpected_error)