allow passing the new password when resetting passwords either upfront or as part of the confirmation step

This commit is contained in:
Adam Brown 2022-05-27 15:38:33 +01:00
parent e3d46cfd15
commit 35163f77ba
4 changed files with 27 additions and 14 deletions

1
changelog.d/6169.sdk Normal file
View File

@ -0,0 +1 @@
Allows new passwords to be passed at the point of confirmation when resetting a password

View File

@ -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)
}

View File

@ -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<AddThreePidRegistrationResponse, String> {
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
}
}
}
}

View File

@ -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
)