From 35163f77ba1335b0893a10d9807a2393eeca60d7 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Fri, 27 May 2022 15:38:33 +0100 Subject: [PATCH] allow passing the new password when resetting passwords either upfront or as part of the confirmation step --- changelog.d/6169.sdk | 1 + .../android/sdk/api/auth/login/LoginWizard.kt | 8 +++-- .../internal/auth/login/DefaultLoginWizard.kt | 30 ++++++++++++------- .../internal/auth/login/ResetPasswordData.kt | 2 +- 4 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 changelog.d/6169.sdk diff --git a/changelog.d/6169.sdk b/changelog.d/6169.sdk new file mode 100644 index 0000000000..dfee158c3f --- /dev/null +++ b/changelog.d/6169.sdk @@ -0,0 +1 @@ +Allows new passwords to be passed at the point of confirmation when resetting a password diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/login/LoginWizard.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/login/LoginWizard.kt index f5670875c2..cf810de733 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/login/LoginWizard.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/login/LoginWizard.kt @@ -65,16 +65,18 @@ interface LoginWizard { * [resetPasswordMailConfirmed] is successfully called. * * @param email an email previously associated to the account the user wants the password to be reset. - * @param newPassword the desired new password + * @param newPassword the desired new password, can be optionally set here or as part of [resetPasswordMailConfirmed] */ suspend fun resetPassword( email: String, - newPassword: String + newPassword: String? = null ) /** * Confirm the new password, once the user has checked their email * When this method succeed, tha account password will be effectively modified. + * + * @param newPassword the desired new password, required if a password was not supplied to [resetPassword] */ - suspend fun resetPasswordMailConfirmed() + suspend fun resetPasswordMailConfirmed(newPassword: String? = null) } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt index 0a189f86e6..f949e793df 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt @@ -30,6 +30,7 @@ import org.matrix.android.sdk.internal.auth.data.ThreePidMedium import org.matrix.android.sdk.internal.auth.data.TokenLoginParams import org.matrix.android.sdk.internal.auth.db.PendingSessionData import org.matrix.android.sdk.internal.auth.registration.AddThreePidRegistrationParams +import org.matrix.android.sdk.internal.auth.registration.AddThreePidRegistrationResponse import org.matrix.android.sdk.internal.auth.registration.RegisterAddThreePidTask import org.matrix.android.sdk.internal.network.executeRequest import org.matrix.android.sdk.internal.session.content.DefaultContentUrlResolver @@ -103,7 +104,7 @@ internal class DefaultLoginWizard( return sessionCreator.createSession(credentials, pendingSessionData.homeServerConnectionConfig) } - override suspend fun resetPassword(email: String, newPassword: String) { + override suspend fun resetPassword(email: String, newPassword: String?) { val param = RegisterAddThreePidTask.Params( RegisterThreePid.Email(email), pendingSessionData.clientSecret, @@ -121,15 +122,14 @@ internal class DefaultLoginWizard( .also { pendingSessionStore.savePendingSessionData(it) } } - override suspend fun resetPasswordMailConfirmed() { - val safeResetPasswordData = pendingSessionData.resetPasswordData - ?: throw IllegalStateException("developer error, no reset password in progress") - - val param = ResetPasswordMailConfirmed.create( - pendingSessionData.clientSecret, - safeResetPasswordData.addThreePidRegistrationResponse.sid, - safeResetPasswordData.newPassword - ) + override suspend fun resetPasswordMailConfirmed(newPassword: String?) { + val param = pendingSessionData.readResetPasswordDataOrThrow(newPassword).let { (response, password) -> + ResetPasswordMailConfirmed.create( + pendingSessionData.clientSecret, + response.sid, + password + ) + } executeRequest(null) { authAPI.resetPasswordMailConfirmed(param) @@ -138,4 +138,14 @@ internal class DefaultLoginWizard( // Set to null? // resetPasswordData = null } + + private fun PendingSessionData.readResetPasswordDataOrThrow(newPassword: String?): Pair { + return when (resetPasswordData) { + null -> throw IllegalStateException("developer error, no reset password in progress") + else -> { + val password = newPassword ?: resetPasswordData.newPassword ?: throw IllegalStateException("developer error, no new password set") + resetPasswordData.addThreePidRegistrationResponse to password + } + } + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/ResetPasswordData.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/ResetPasswordData.kt index a65ec38d6d..0e31d2bf13 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/ResetPasswordData.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/ResetPasswordData.kt @@ -24,6 +24,6 @@ import org.matrix.android.sdk.internal.auth.registration.AddThreePidRegistration */ @JsonClass(generateAdapter = true) internal data class ResetPasswordData( - val newPassword: String, + val newPassword: String?, val addThreePidRegistrationResponse: AddThreePidRegistrationResponse )