Skip to content
Snippets Groups Projects
Commit cbb78566 authored by Taras's avatar Taras
Browse files

Add forgot password api

parent 9847a4f4
No related branches found
No related tags found
No related merge requests found
......@@ -53,6 +53,9 @@ interface AccountService {
userInteractiveAuthInterceptor: UserInteractiveAuthInterceptor
)
//Added for password UIA stages
//Added for change password UIA stages
suspend fun changePasswordStages(userInteractiveAuthInterceptor: UserInteractiveAuthInterceptor, logoutAllDevices: Boolean = true)
//Added for forgot password UIA stages
suspend fun forgotPasswordStages(userInteractiveAuthInterceptor: UserInteractiveAuthInterceptor, logoutAllDevices: Boolean = true)
}
......@@ -37,7 +37,11 @@ internal interface AccountAPI {
@POST(NetworkConstants.URI_API_PREFIX_PATH_R0 + "account/deactivate")
suspend fun deactivate(@Body params: DeactivateAccountParams)
//Added to handle reAuth uia stages
//Added to handle change password uia stages
@POST(NetworkConstants.URI_API_PREFIX_PATH_R0 + "account/auth/password")
suspend fun changePasswordUIA(@Body params: ChangePasswordUIAParams)
suspend fun changePasswordUIA(@Body params: AuthUIAParams)
//Added to handle forgot password uia stages
@POST(NetworkConstants.URI_API_PREFIX_PATH_R0 + "account/auth/recovery")
suspend fun recoverPasswordUIA(@Body params: AuthUIAParams)
}
......@@ -47,4 +47,7 @@ internal abstract class AccountModule {
@Binds
abstract fun bindChangePasswordUIATask(task: DefaultChangePasswordUIATask): ChangePasswordUIATask
@Binds
abstract fun bindForgotPasswordUIATask(task: DefaultForgotPasswordUIATask): ForgotPasswordUIATask
}
......@@ -22,7 +22,7 @@ import org.matrix.android.sdk.api.auth.UIABaseAuth
//Created for Circles
@JsonClass(generateAdapter = true)
internal data class ChangePasswordUIAParams(
internal data class AuthUIAParams(
@Json(name = "logout_devices")
val logoutDevices: Boolean = true,
......@@ -31,8 +31,8 @@ internal data class ChangePasswordUIAParams(
val auth: Map<String, *>? = null
) {
companion object {
fun create(auth: UIABaseAuth?, logoutDevices: Boolean): ChangePasswordUIAParams {
return ChangePasswordUIAParams(
fun create(auth: UIABaseAuth?, logoutDevices: Boolean): AuthUIAParams {
return AuthUIAParams(
auth = auth?.asMap(),
logoutDevices = logoutDevices
)
......
......@@ -44,7 +44,7 @@ internal class DefaultChangePasswordUIATask @Inject constructor(
) : ChangePasswordUIATask {
override suspend fun execute(params: ChangePasswordUIATask.Params) {
val changePasswordParams = ChangePasswordUIAParams.create(params.userAuthParam, params.logoutAllDevices)
val changePasswordParams = AuthUIAParams.create(params.userAuthParam, params.logoutAllDevices)
try {
executeRequest(globalErrorReceiver) {
accountAPI.changePasswordUIA(changePasswordParams)
......
......@@ -23,7 +23,8 @@ import javax.inject.Inject
internal class DefaultAccountService @Inject constructor(
private val changePasswordTask: ChangePasswordTask,
private val deactivateAccountTask: DeactivateAccountTask,
private val changePasswordUIATask: ChangePasswordUIATask
private val changePasswordUIATask: ChangePasswordUIATask,
private val forgotPasswordUIATask: DefaultForgotPasswordUIATask
) : AccountService {
override suspend fun changePassword(password: String, newPassword: String, logoutAllDevices: Boolean) {
......@@ -38,4 +39,9 @@ internal class DefaultAccountService @Inject constructor(
override suspend fun changePasswordStages(userInteractiveAuthInterceptor: UserInteractiveAuthInterceptor, logoutAllDevices: Boolean) {
changePasswordUIATask.execute(ChangePasswordUIATask.Params(logoutAllDevices, userInteractiveAuthInterceptor))
}
//Added for password UIA stages
override suspend fun forgotPasswordStages(userInteractiveAuthInterceptor: UserInteractiveAuthInterceptor, logoutAllDevices: Boolean) {
forgotPasswordUIATask.execute(ForgotPasswordUIATask.Params(logoutAllDevices, userInteractiveAuthInterceptor))
}
}
/*
* Copyright 2020 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.internal.session.account
import org.matrix.android.sdk.api.auth.UIABaseAuth
import org.matrix.android.sdk.api.auth.UserInteractiveAuthInterceptor
import org.matrix.android.sdk.api.failure.Failure
import org.matrix.android.sdk.api.failure.toRegistrationFlowResponse
import org.matrix.android.sdk.api.session.uia.UiaResult
import org.matrix.android.sdk.internal.auth.registration.handleUIA
import org.matrix.android.sdk.internal.network.GlobalErrorReceiver
import org.matrix.android.sdk.internal.network.executeRequest
import org.matrix.android.sdk.internal.task.Task
import timber.log.Timber
import javax.inject.Inject
//Created for Circles
internal interface ForgotPasswordUIATask : Task<ForgotPasswordUIATask.Params, Unit> {
data class Params(
val logoutAllDevices: Boolean,
val userInteractiveAuthInterceptor: UserInteractiveAuthInterceptor,
val userAuthParam: UIABaseAuth? = null
)
}
//Created for Circles
internal class DefaultForgotPasswordUIATask @Inject constructor(
private val accountAPI: AccountAPI,
private val globalErrorReceiver: GlobalErrorReceiver
) : ForgotPasswordUIATask {
override suspend fun execute(params: ForgotPasswordUIATask.Params) {
val authParams = AuthUIAParams.create(params.userAuthParam, params.logoutAllDevices)
try {
executeRequest(globalErrorReceiver) {
accountAPI.recoverPasswordUIA(authParams)
}
} catch (throwable: Throwable) {
if (handleUIA(
failure = throwable,
interceptor = params.userInteractiveAuthInterceptor,
retryBlock = { authUpdate ->
execute(params.copy(userAuthParam = authUpdate))
}
) != UiaResult.SUCCESS
) {
Timber.d("## UIA: propagate failure")
throw throwable.toRegistrationFlowResponse()
?.let { Failure.RegistrationFlowError(it) }
?: throwable
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment