registration availability endpoint added

This commit is contained in:
Aleks 2021-03-29 09:20:22 +02:00 committed by Benoit Marty
parent 65b4b2915f
commit 2e9f8ae6ae
7 changed files with 116 additions and 10 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -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<Availability>
/**
* Add 3Pid during registration
* Ref: https://gist.github.com/jryans/839a09bf0c5a70e2f36ed990d50ed928

View file

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

View file

@ -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<String, Availability> {
override suspend fun execute(params: String): Availability {
return executeRequest(null) {
apiCall = authAPI.registerAvailable(params)
}
}
}