From 2e9f8ae6ae728a1d2a279d455cf2fdabf154ef8a Mon Sep 17 00:00:00 2001 From: Aleks Date: Mon, 29 Mar 2021 09:20:22 +0200 Subject: [PATCH 1/6] registration availability endpoint added --- .../android/sdk/api/auth/data/Availability.kt | 26 ++++++++++++++++ .../registration/RegistrationAvailability.kt | 24 +++++++++++++++ .../auth/registration/RegistrationWizard.kt | 2 ++ .../android/sdk/api/failure/Extensions.kt | 8 +++++ .../android/sdk/internal/auth/AuthAPI.kt | 15 ++++++---- .../registration/DefaultRegistrationWizard.kt | 21 ++++++++++--- .../registration/RegisterAvailableTask.kt | 30 +++++++++++++++++++ 7 files changed, 116 insertions(+), 10 deletions(-) create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/data/Availability.kt create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationAvailability.kt create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/data/Availability.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/data/Availability.kt new file mode 100644 index 0000000000..3fbbdd161f --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/data/Availability.kt @@ -0,0 +1,26 @@ +/* + * 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.api.auth.data + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) +data class Availability( + @Json(name = "available") + val available: Boolean +) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationAvailability.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationAvailability.kt new file mode 100644 index 0000000000..7c7d769a35 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationAvailability.kt @@ -0,0 +1,24 @@ +/* + * 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.api.auth.registration + +import org.matrix.android.sdk.api.failure.Failure + +sealed class RegistrationAvailability { + data class Available(val available: Boolean): RegistrationAvailability() + data class NotAvailable(val failure: Failure.ServerError): RegistrationAvailability() +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationWizard.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationWizard.kt index d00c9a0c82..38a5a77291 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationWizard.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationWizard.kt @@ -36,6 +36,8 @@ interface RegistrationWizard { suspend fun checkIfEmailHasBeenValidated(delayMillis: Long): RegistrationResult + suspend fun registrationAvailable(userName: String): RegistrationAvailability + val currentThreePid: String? // True when login and password has been sent with success to the homeserver diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt index 17362ff8d7..7f25eedb23 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt @@ -87,3 +87,11 @@ fun Throwable.toRegistrationFlowResponse(): RegistrationFlowResponse? { null } } + +fun Throwable.isRegistrationAvailabilityError(): Boolean { + return this is Failure.ServerError + && (error.code == MatrixError.M_USER_IN_USE + || error.code == MatrixError.M_INVALID_USERNAME + || error.code == MatrixError.M_EXCLUSIVE) + && httpCode == 400 +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt index 2ce5c67a94..6566aae5fe 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt @@ -16,6 +16,7 @@ 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.LoginFlowResponse import org.matrix.android.sdk.internal.auth.data.PasswordLoginParams @@ -29,12 +30,8 @@ import org.matrix.android.sdk.internal.auth.registration.SuccessResult import org.matrix.android.sdk.internal.auth.registration.ValidationCodeBody import org.matrix.android.sdk.internal.auth.version.Versions import org.matrix.android.sdk.internal.network.NetworkConstants -import retrofit2.http.Body -import retrofit2.http.GET -import retrofit2.http.Headers -import retrofit2.http.POST -import retrofit2.http.Path -import retrofit2.http.Url +import retrofit2.Call +import retrofit2.http.* /** * The login REST API. @@ -65,6 +62,12 @@ internal interface AuthAPI { @POST(NetworkConstants.URI_API_PREFIX_PATH_R0 + "register") suspend fun register(@Body registrationParams: RegistrationParams): Credentials + /** + * Checks to see if a username is available, and valid, for the server. + */ + @GET(NetworkConstants.URI_API_PREFIX_PATH_R0 + "register/available") + fun registerAvailable(@Query("username") username: String): Call + /** * Add 3Pid during registration * Ref: https://gist.github.com/jryans/839a09bf0c5a70e2f36ed990d50ed928 diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt index 91e414e689..3be95f6a4a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt @@ -18,12 +18,10 @@ package org.matrix.android.sdk.internal.auth.registration import kotlinx.coroutines.delay import org.matrix.android.sdk.api.auth.data.LoginFlowTypes -import org.matrix.android.sdk.api.auth.registration.RegisterThreePid -import org.matrix.android.sdk.api.auth.registration.RegistrationResult -import org.matrix.android.sdk.api.auth.registration.RegistrationWizard -import org.matrix.android.sdk.api.auth.registration.toFlowResult +import org.matrix.android.sdk.api.auth.registration.* import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.failure.Failure.RegistrationFlowError +import org.matrix.android.sdk.api.failure.isRegistrationAvailabilityError import org.matrix.android.sdk.internal.auth.AuthAPI import org.matrix.android.sdk.internal.auth.PendingSessionStore import org.matrix.android.sdk.internal.auth.SessionCreator @@ -41,6 +39,7 @@ 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) @@ -203,4 +202,18 @@ internal class DefaultRegistrationWizard( val session = sessionCreator.createSession(credentials, pendingSessionData.homeServerConnectionConfig) return RegistrationResult.Success(session) } + + override suspend fun registrationAvailable(userName: String): RegistrationAvailability { + val availability = try { + registerAvailableTask.execute(userName) + } catch (exception: Throwable) { + if(exception.isRegistrationAvailabilityError()) { + return RegistrationAvailability.NotAvailable(exception as Failure.ServerError) + } else { + throw exception + } + } + + return RegistrationAvailability.Available(availability.available) + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt new file mode 100644 index 0000000000..aa2bb767f9 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt @@ -0,0 +1,30 @@ +/* + * 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.auth.registration + +import org.matrix.android.sdk.api.auth.data.Availability +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 { + override suspend fun execute(params: String): Availability { + return executeRequest(null) { + apiCall = authAPI.registerAvailable(params) + } + } +} \ No newline at end of file From 9ce9d1e5491b800bc3291bd575f45ad5742959cb Mon Sep 17 00:00:00 2001 From: Aleks Date: Mon, 29 Mar 2021 15:31:53 +0200 Subject: [PATCH 2/6] CHANGES.md updated --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 1a684ee56f..3362588cf8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -19,6 +19,7 @@ Improvements 🙌: - Fix quick click action (#3127) - Get Event after a Push for a faster notification display in some conditions - Always try to retry Http requests in case of 429 (#1300) + - registration availability endpoint added to matrix-sdk Bugfix 🐛: - Fix bad theme change for the MainActivity From 4451b682b17ba9d4cb6a853a747fad94e93f5d6a Mon Sep 17 00:00:00 2001 From: Aleks Date: Thu, 8 Apr 2021 11:30:41 +0200 Subject: [PATCH 3/6] merge + register/available endpoint corrections --- .../android/sdk/api/auth/data/Availability.kt | 2 +- .../registration/RegistrationAvailability.kt | 4 ++-- .../android/sdk/internal/auth/AuthAPI.kt | 2 +- .../registration/DefaultRegistrationWizard.kt | 15 +----------- .../registration/RegisterAvailableTask.kt | 24 ++++++++++++++----- 5 files changed, 23 insertions(+), 24 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/data/Availability.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/data/Availability.kt index 3fbbdd161f..93e0ac4e44 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/data/Availability.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/data/Availability.kt @@ -20,7 +20,7 @@ import com.squareup.moshi.Json import com.squareup.moshi.JsonClass @JsonClass(generateAdapter = true) -data class Availability( +internal data class Availability( @Json(name = "available") val available: Boolean ) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationAvailability.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationAvailability.kt index 7c7d769a35..5f944a86d9 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationAvailability.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationAvailability.kt @@ -19,6 +19,6 @@ package org.matrix.android.sdk.api.auth.registration import org.matrix.android.sdk.api.failure.Failure sealed class RegistrationAvailability { - data class Available(val available: Boolean): RegistrationAvailability() + object Available : RegistrationAvailability() data class NotAvailable(val failure: Failure.ServerError): RegistrationAvailability() -} \ No newline at end of file +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt index 6566aae5fe..216be83ea3 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt @@ -66,7 +66,7 @@ internal interface AuthAPI { * Checks to see if a username is available, and valid, for the server. */ @GET(NetworkConstants.URI_API_PREFIX_PATH_R0 + "register/available") - fun registerAvailable(@Query("username") username: String): Call + suspend fun registerAvailable(@Query("username") username: String): Availability /** * Add 3Pid during registration diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt index 3be95f6a4a..ace2e62043 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt @@ -21,7 +21,6 @@ import org.matrix.android.sdk.api.auth.data.LoginFlowTypes import org.matrix.android.sdk.api.auth.registration.* import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.failure.Failure.RegistrationFlowError -import org.matrix.android.sdk.api.failure.isRegistrationAvailabilityError import org.matrix.android.sdk.internal.auth.AuthAPI import org.matrix.android.sdk.internal.auth.PendingSessionStore import org.matrix.android.sdk.internal.auth.SessionCreator @@ -203,17 +202,5 @@ internal class DefaultRegistrationWizard( return RegistrationResult.Success(session) } - override suspend fun registrationAvailable(userName: String): RegistrationAvailability { - val availability = try { - registerAvailableTask.execute(userName) - } catch (exception: Throwable) { - if(exception.isRegistrationAvailabilityError()) { - return RegistrationAvailability.NotAvailable(exception as Failure.ServerError) - } else { - throw exception - } - } - - return RegistrationAvailability.Available(availability.available) - } + override suspend fun registrationAvailable(userName: String): RegistrationAvailability = registerAvailableTask.execute(userName) } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt index aa2bb767f9..791b725357 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt @@ -16,15 +16,27 @@ package org.matrix.android.sdk.internal.auth.registration -import org.matrix.android.sdk.api.auth.data.Availability +import org.matrix.android.sdk.api.auth.registration.RegistrationAvailability +import org.matrix.android.sdk.api.failure.Failure +import org.matrix.android.sdk.api.failure.isRegistrationAvailabilityError 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 { - override suspend fun execute(params: String): Availability { - return executeRequest(null) { - apiCall = authAPI.registerAvailable(params) +internal class RegisterAvailableTask(private val authAPI: AuthAPI) : Task { + override suspend fun execute(params: String): RegistrationAvailability { + try { + executeRequest(null) { + authAPI.registerAvailable(params) + } + } catch (exception: Throwable) { + if(exception.isRegistrationAvailabilityError()) { + return RegistrationAvailability.NotAvailable(exception as Failure.ServerError) + } else { + throw exception + } } + + return RegistrationAvailability.Available } -} \ No newline at end of file +} From 9d96f90e49dcd05319b06ae6457989add0578b0e Mon Sep 17 00:00:00 2001 From: Aleks Date: Thu, 8 Apr 2021 12:15:19 +0200 Subject: [PATCH 4/6] keyword-spacing fix + wildcard-imports fix --- .../internal/auth/registration/DefaultRegistrationWizard.kt | 6 +++++- .../sdk/internal/auth/registration/RegisterAvailableTask.kt | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt index ace2e62043..16c05dd8a9 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt @@ -18,7 +18,11 @@ package org.matrix.android.sdk.internal.auth.registration import kotlinx.coroutines.delay import org.matrix.android.sdk.api.auth.data.LoginFlowTypes -import org.matrix.android.sdk.api.auth.registration.* +import org.matrix.android.sdk.api.auth.registration.RegisterThreePid +import org.matrix.android.sdk.api.auth.registration.RegistrationAvailability +import org.matrix.android.sdk.api.auth.registration.RegistrationResult +import org.matrix.android.sdk.api.auth.registration.RegistrationWizard +import org.matrix.android.sdk.api.auth.registration.toFlowResult import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.failure.Failure.RegistrationFlowError import org.matrix.android.sdk.internal.auth.AuthAPI diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt index 791b725357..d1aa265636 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt @@ -30,7 +30,7 @@ internal class RegisterAvailableTask(private val authAPI: AuthAPI) : Task Date: Thu, 8 Apr 2021 18:48:43 +0200 Subject: [PATCH 5/6] Some cleanup after merge of #3084 --- .../registration/RegistrationAvailability.kt | 4 ++-- .../android/sdk/api/failure/Extensions.kt | 13 +++++++----- .../android/sdk/internal/auth/AuthAPI.kt | 2 +- .../auth/data/Availability.kt | 11 ++++++---- .../registration/DefaultRegistrationWizard.kt | 12 ++++++----- .../registration/RegisterAvailableTask.kt | 21 ++++++++++++------- 6 files changed, 38 insertions(+), 25 deletions(-) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/{api => internal}/auth/data/Availability.kt (68%) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationAvailability.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationAvailability.kt index 5f944a86d9..f9a7ace7ba 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationAvailability.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationAvailability.kt @@ -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() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt index 7f25eedb23..0ba61e5890 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt @@ -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) } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt index 216be83ea3..d6a197b06b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt @@ -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 diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/data/Availability.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/data/Availability.kt similarity index 68% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/data/Availability.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/data/Availability.kt index 93e0ac4e44..5ef3c0d06a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/data/Availability.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/data/Availability.kt @@ -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 ) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt index 16c05dd8a9..4a3d53a8fc 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt @@ -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)) + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt index d1aa265636..314a24dad4 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt @@ -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 { - override suspend fun execute(params: String): RegistrationAvailability { - try { +internal interface RegisterAvailableTask : Task { + 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 } } From ee3eb8e1d6744049e524f8e55342506437cc019d Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 8 Apr 2021 18:52:53 +0200 Subject: [PATCH 6/6] Cleanup --- .../java/org/matrix/android/sdk/internal/auth/AuthAPI.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt index d6a197b06b..f93f285c6e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt @@ -30,8 +30,13 @@ import org.matrix.android.sdk.internal.auth.registration.SuccessResult import org.matrix.android.sdk.internal.auth.registration.ValidationCodeBody import org.matrix.android.sdk.internal.auth.version.Versions import org.matrix.android.sdk.internal.network.NetworkConstants -import retrofit2.Call -import retrofit2.http.* +import retrofit2.http.Body +import retrofit2.http.GET +import retrofit2.http.Headers +import retrofit2.http.POST +import retrofit2.http.Path +import retrofit2.http.Query +import retrofit2.http.Url /** * The login REST API.