Some cleanup after merge of #3084

This commit is contained in:
Benoit Marty 2021-04-08 18:48:43 +02:00 committed by Benoit Marty
parent 9d96f90e49
commit 2a1e52745f
6 changed files with 38 additions and 25 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2020 The Matrix.org Foundation C.I.C.
* Copyright 2021 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.
@ -20,5 +20,5 @@ import org.matrix.android.sdk.api.failure.Failure
sealed class RegistrationAvailability {
object Available : RegistrationAvailability()
data class NotAvailable(val failure: Failure.ServerError): RegistrationAvailability()
data class NotAvailable(val failure: Failure.ServerError) : RegistrationAvailability()
}

View File

@ -65,13 +65,16 @@ fun Throwable.isInvalidUIAAuth(): Boolean {
* Try to convert to a RegistrationFlowResponse. Return null in the cases it's not possible
*/
fun Throwable.toRegistrationFlowResponse(): RegistrationFlowResponse? {
return if (this is Failure.OtherServerError && httpCode == 401) {
return if (this is Failure.OtherServerError
&& httpCode == HttpsURLConnection.HTTP_UNAUTHORIZED /* 401 */) {
tryOrNull {
MoshiProvider.providesMoshi()
.adapter(RegistrationFlowResponse::class.java)
.fromJson(errorBody)
}
} else if (this is Failure.ServerError && httpCode == 401 && error.code == MatrixError.M_FORBIDDEN) {
} else if (this is Failure.ServerError
&& httpCode == HttpsURLConnection.HTTP_UNAUTHORIZED /* 401 */
&& error.code == MatrixError.M_FORBIDDEN) {
// This happens when the submission for this stage was bad (like bad password)
if (error.session != null && error.flows != null) {
RegistrationFlowResponse(
@ -90,8 +93,8 @@ fun Throwable.toRegistrationFlowResponse(): RegistrationFlowResponse? {
fun Throwable.isRegistrationAvailabilityError(): Boolean {
return this is Failure.ServerError
&& httpCode == HttpsURLConnection.HTTP_BAD_REQUEST /* 400 */
&& (error.code == MatrixError.M_USER_IN_USE
|| error.code == MatrixError.M_INVALID_USERNAME
|| error.code == MatrixError.M_EXCLUSIVE)
&& httpCode == 400
|| error.code == MatrixError.M_INVALID_USERNAME
|| error.code == MatrixError.M_EXCLUSIVE)
}

View File

@ -16,8 +16,8 @@
package org.matrix.android.sdk.internal.auth
import org.matrix.android.sdk.api.auth.data.Availability
import org.matrix.android.sdk.api.auth.data.Credentials
import org.matrix.android.sdk.internal.auth.data.Availability
import org.matrix.android.sdk.internal.auth.data.LoginFlowResponse
import org.matrix.android.sdk.internal.auth.data.PasswordLoginParams
import org.matrix.android.sdk.internal.auth.data.RiotConfig

View File

@ -1,5 +1,5 @@
/*
* Copyright 2020 The Matrix.org Foundation C.I.C.
* Copyright 2021 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.
@ -14,13 +14,16 @@
* limitations under the License.
*/
package org.matrix.android.sdk.api.auth.data
package org.matrix.android.sdk.internal.auth.data
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
internal data class Availability(
@Json(name = "available")
val available: Boolean
/**
* A flag to indicate that the username is available. This should always be true when the server replies with 200 OK.
*/
@Json(name = "available")
val available: Boolean? = null
)

View File

@ -41,10 +41,10 @@ internal class DefaultRegistrationWizard(
private var pendingSessionData: PendingSessionData = pendingSessionStore.getPendingSessionData() ?: error("Pending session data should exist here")
private val registerTask = DefaultRegisterTask(authAPI)
private val registerAvailableTask = RegisterAvailableTask(authAPI)
private val registerAddThreePidTask = DefaultRegisterAddThreePidTask(authAPI)
private val validateCodeTask = DefaultValidateCodeTask(authAPI)
private val registerTask: RegisterTask = DefaultRegisterTask(authAPI)
private val registerAvailableTask: RegisterAvailableTask = DefaultRegisterAvailableTask(authAPI)
private val registerAddThreePidTask: RegisterAddThreePidTask = DefaultRegisterAddThreePidTask(authAPI)
private val validateCodeTask: ValidateCodeTask = DefaultValidateCodeTask(authAPI)
override val currentThreePid: String?
get() {
@ -206,5 +206,7 @@ internal class DefaultRegistrationWizard(
return RegistrationResult.Success(session)
}
override suspend fun registrationAvailable(userName: String): RegistrationAvailability = registerAvailableTask.execute(userName)
override suspend fun registrationAvailable(userName: String): RegistrationAvailability {
return registerAvailableTask.execute(RegisterAvailableTask.Params(userName))
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2020 The Matrix.org Foundation C.I.C.
* Copyright 2021 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.
@ -23,20 +23,25 @@ import org.matrix.android.sdk.internal.auth.AuthAPI
import org.matrix.android.sdk.internal.network.executeRequest
import org.matrix.android.sdk.internal.task.Task
internal class RegisterAvailableTask(private val authAPI: AuthAPI) : Task<String, RegistrationAvailability> {
override suspend fun execute(params: String): RegistrationAvailability {
try {
internal interface RegisterAvailableTask : Task<RegisterAvailableTask.Params, RegistrationAvailability> {
data class Params(
val userName: String
)
}
internal class DefaultRegisterAvailableTask(private val authAPI: AuthAPI) : RegisterAvailableTask {
override suspend fun execute(params: RegisterAvailableTask.Params): RegistrationAvailability {
return try {
executeRequest(null) {
authAPI.registerAvailable(params)
authAPI.registerAvailable(params.userName)
}
RegistrationAvailability.Available
} catch (exception: Throwable) {
if (exception.isRegistrationAvailabilityError()) {
return RegistrationAvailability.NotAvailable(exception as Failure.ServerError)
RegistrationAvailability.NotAvailable(exception as Failure.ServerError)
} else {
throw exception
}
}
return RegistrationAvailability.Available
}
}