From 984b1dd6a8cd3a2d58d15ec83053ce1635a848bf Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 29 Jun 2021 18:08:02 +0200 Subject: [PATCH] RawService.getWellknown() now takes a domain instead of a matrixId as parameter --- .idea/dictionaries/bmarty.xml | 1 + changelog.d/3572.misc | 1 + .../sdk/api/auth/wellknown/WellknownResult.kt | 5 --- .../sdk/api/failure/MatrixIdFailure.kt | 21 +++++++++++ .../matrix/android/sdk/api/raw/RawService.kt | 4 +- .../auth/DefaultAuthenticationService.kt | 15 ++++++-- .../sdk/internal/raw/DefaultRawService.kt | 5 +-- .../GetHomeServerCapabilitiesTask.kt | 5 ++- .../internal/wellknown/GetWellknownTask.kt | 14 +++---- .../vector/app/core/error/ErrorFormatter.kt | 23 +++++++----- .../app/features/login/LoginViewModel.kt | 37 +++++++++---------- .../app/features/login2/LoginViewModel2.kt | 4 -- 12 files changed, 79 insertions(+), 56 deletions(-) create mode 100644 changelog.d/3572.misc create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/MatrixIdFailure.kt diff --git a/.idea/dictionaries/bmarty.xml b/.idea/dictionaries/bmarty.xml index 8299f1c4de..e143720aa9 100644 --- a/.idea/dictionaries/bmarty.xml +++ b/.idea/dictionaries/bmarty.xml @@ -38,6 +38,7 @@ unpublish unwedging vctr + wellknown \ No newline at end of file diff --git a/changelog.d/3572.misc b/changelog.d/3572.misc new file mode 100644 index 0000000000..384b8e69c7 --- /dev/null +++ b/changelog.d/3572.misc @@ -0,0 +1 @@ +RawService.getWellknown() now takes a domain instead of a matrixId as parameter \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/wellknown/WellknownResult.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/wellknown/WellknownResult.kt index c68a9e9699..56257db79c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/wellknown/WellknownResult.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/wellknown/WellknownResult.kt @@ -22,11 +22,6 @@ import org.matrix.android.sdk.api.auth.data.WellKnown * Ref: https://matrix.org/docs/spec/client_server/latest#well-known-uri */ sealed class WellknownResult { - /** - * The provided matrixId is no valid. Unable to extract a domain name. - */ - object InvalidMatrixId : WellknownResult() - /** * Retrieve the specific piece of information from the user in a way which fits within the existing client user experience, * if the client is inclined to do so. Failure can take place instead if no good user experience for this is possible at this point. diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/MatrixIdFailure.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/MatrixIdFailure.kt new file mode 100644 index 0000000000..8f7bca803d --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/MatrixIdFailure.kt @@ -0,0 +1,21 @@ +/* + * Copyright (c) 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. + * 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.failure + +sealed class MatrixIdFailure : Failure.FeatureFailure() { + object InvalidMatrixId : MatrixIdFailure() +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/raw/RawService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/raw/RawService.kt index f1722b2189..3366d040f7 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/raw/RawService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/raw/RawService.kt @@ -29,8 +29,10 @@ interface RawService { /** * Specific case for the well-known file. Cache validity is 8 hours + * @param domain the domain to get the .well-known file, for instance "matrix.org". + * The URL will be "https://{domain}/.well-known/matrix/client" */ - suspend fun getWellknown(userId: String): String + suspend fun getWellknown(domain: String): String /** * Clear all the cache data diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/DefaultAuthenticationService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/DefaultAuthenticationService.kt index 20ce438d8e..7077453aae 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/DefaultAuthenticationService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/DefaultAuthenticationService.kt @@ -19,6 +19,7 @@ package org.matrix.android.sdk.internal.auth import android.net.Uri import dagger.Lazy import okhttp3.OkHttpClient +import org.matrix.android.sdk.api.MatrixPatterns import org.matrix.android.sdk.api.auth.AuthenticationService import org.matrix.android.sdk.api.auth.data.Credentials import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig @@ -28,6 +29,7 @@ import org.matrix.android.sdk.api.auth.login.LoginWizard import org.matrix.android.sdk.api.auth.registration.RegistrationWizard import org.matrix.android.sdk.api.auth.wellknown.WellknownResult import org.matrix.android.sdk.api.failure.Failure +import org.matrix.android.sdk.api.failure.MatrixIdFailure import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.util.appendParamToUrl import org.matrix.android.sdk.internal.SessionManager @@ -275,9 +277,7 @@ internal class DefaultAuthenticationService @Inject constructor( val domain = homeServerConnectionConfig.homeServerUri.host ?: throw Failure.OtherServerError("", HttpsURLConnection.HTTP_NOT_FOUND /* 404 */) - // Create a fake userId, for the getWellknown task - val fakeUserId = "@alice:$domain" - val wellknownResult = getWellknownTask.execute(GetWellknownTask.Params(fakeUserId, homeServerConnectionConfig)) + val wellknownResult = getWellknownTask.execute(GetWellknownTask.Params(domain, homeServerConnectionConfig)) return when (wellknownResult) { is WellknownResult.Prompt -> { @@ -379,7 +379,14 @@ internal class DefaultAuthenticationService @Inject constructor( override suspend fun getWellKnownData(matrixId: String, homeServerConnectionConfig: HomeServerConnectionConfig?): WellknownResult { - return getWellknownTask.execute(GetWellknownTask.Params(matrixId, homeServerConnectionConfig)) + if (!MatrixPatterns.isUserId(matrixId)) { + throw MatrixIdFailure.InvalidMatrixId + } + + return getWellknownTask.execute(GetWellknownTask.Params( + domain = matrixId.substringAfter(":"), + homeServerConnectionConfig = homeServerConnectionConfig) + ) } override suspend fun directAuthentication(homeServerConnectionConfig: HomeServerConnectionConfig, diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/raw/DefaultRawService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/raw/DefaultRawService.kt index 42b826de16..bca1e498de 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/raw/DefaultRawService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/raw/DefaultRawService.kt @@ -29,10 +29,9 @@ internal class DefaultRawService @Inject constructor( return getUrlTask.execute(GetUrlTask.Params(url, cacheStrategy)) } - override suspend fun getWellknown(userId: String): String { - val homeServerDomain = userId.substringAfter(":") + override suspend fun getWellknown(domain: String): String { return getUrl( - "https://$homeServerDomain/.well-known/matrix/client", + "https://$domain/.well-known/matrix/client", CacheStrategy.TtlCache(TimeUnit.HOURS.toMillis(8), false) ) } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/homeserver/GetHomeServerCapabilitiesTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/homeserver/GetHomeServerCapabilitiesTask.kt index 740370123f..7b9a304c96 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/homeserver/GetHomeServerCapabilitiesTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/homeserver/GetHomeServerCapabilitiesTask.kt @@ -89,7 +89,10 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor( }.getOrNull() val wellknownResult = runCatching { - getWellknownTask.execute(GetWellknownTask.Params(userId, homeServerConnectionConfig)) + getWellknownTask.execute(GetWellknownTask.Params( + domain = userId.substringAfter(":"), + homeServerConnectionConfig = homeServerConnectionConfig + )) }.getOrNull() insertInDb(capabilities, mediaConfig, versions, wellknownResult) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/wellknown/GetWellknownTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/wellknown/GetWellknownTask.kt index 7a9beac8c0..0cdb6c999b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/wellknown/GetWellknownTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/wellknown/GetWellknownTask.kt @@ -39,7 +39,11 @@ import javax.net.ssl.HttpsURLConnection internal interface GetWellknownTask : Task { data class Params( - val matrixId: String, + /** + * domain, for instance "matrix.org" + * the URL will be https://{domain}/.well-known/matrix/client + */ + val domain: String, val homeServerConnectionConfig: HomeServerConnectionConfig? ) } @@ -54,14 +58,8 @@ internal class DefaultGetWellknownTask @Inject constructor( ) : GetWellknownTask { override suspend fun execute(params: GetWellknownTask.Params): WellknownResult { - if (!MatrixPatterns.isUserId(params.matrixId)) { - return WellknownResult.InvalidMatrixId - } - - val homeServerDomain = params.matrixId.substringAfter(":") - val client = buildClient(params.homeServerConnectionConfig) - return findClientConfig(homeServerDomain, client) + return findClientConfig(params.domain, client) } private fun buildClient(homeServerConnectionConfig: HomeServerConnectionConfig?): OkHttpClient { diff --git a/vector/src/main/java/im/vector/app/core/error/ErrorFormatter.kt b/vector/src/main/java/im/vector/app/core/error/ErrorFormatter.kt index c0219b26e2..e7602e5cfe 100644 --- a/vector/src/main/java/im/vector/app/core/error/ErrorFormatter.kt +++ b/vector/src/main/java/im/vector/app/core/error/ErrorFormatter.kt @@ -21,6 +21,7 @@ import im.vector.app.core.resources.StringProvider import im.vector.app.features.call.dialpad.DialPadLookup import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.failure.MatrixError +import org.matrix.android.sdk.api.failure.MatrixIdFailure import org.matrix.android.sdk.api.failure.isInvalidPassword import org.matrix.android.sdk.api.session.identity.IdentityServiceError import java.net.HttpURLConnection @@ -39,9 +40,9 @@ class DefaultErrorFormatter @Inject constructor( override fun toHumanReadable(throwable: Throwable?): String { return when (throwable) { - null -> null - is IdentityServiceError -> identityServerError(throwable) - is Failure.NetworkConnection -> { + null -> null + is IdentityServiceError -> identityServerError(throwable) + is Failure.NetworkConnection -> { when (throwable.ioException) { is SocketTimeoutException -> stringProvider.getString(R.string.error_network_timeout) @@ -54,7 +55,7 @@ class DefaultErrorFormatter @Inject constructor( stringProvider.getString(R.string.error_no_network) } } - is Failure.ServerError -> { + is Failure.ServerError -> { when { throwable.error.code == MatrixError.M_CONSENT_NOT_GIVEN -> { // Special case for terms and conditions @@ -104,23 +105,25 @@ class DefaultErrorFormatter @Inject constructor( } } } - is Failure.OtherServerError -> { + is Failure.OtherServerError -> { when (throwable.httpCode) { - HttpURLConnection.HTTP_NOT_FOUND -> + HttpURLConnection.HTTP_NOT_FOUND -> // homeserver not found stringProvider.getString(R.string.login_error_no_homeserver_found) HttpURLConnection.HTTP_UNAUTHORIZED -> // uia errors? stringProvider.getString(R.string.error_unauthorized) - else -> + else -> throwable.localizedMessage } } - is DialPadLookup.Failure.NumberIsYours -> + is DialPadLookup.Failure.NumberIsYours -> stringProvider.getString(R.string.cannot_call_yourself) - is DialPadLookup.Failure.NoResult -> + is DialPadLookup.Failure.NoResult -> stringProvider.getString(R.string.call_dial_pad_lookup_error) - else -> throwable.localizedMessage + is MatrixIdFailure.InvalidMatrixId -> + stringProvider.getString(R.string.login_signin_matrix_id_error_invalid_matrix_id) + else -> throwable.localizedMessage } ?: stringProvider.getString(R.string.unknown_error) } diff --git a/vector/src/main/java/im/vector/app/features/login/LoginViewModel.kt b/vector/src/main/java/im/vector/app/features/login/LoginViewModel.kt index ce68ff4f38..c9b571dca1 100644 --- a/vector/src/main/java/im/vector/app/features/login/LoginViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/login/LoginViewModel.kt @@ -51,6 +51,7 @@ import org.matrix.android.sdk.api.auth.registration.RegistrationWizard import org.matrix.android.sdk.api.auth.registration.Stage import org.matrix.android.sdk.api.auth.wellknown.WellknownResult import org.matrix.android.sdk.api.failure.Failure +import org.matrix.android.sdk.api.failure.MatrixIdFailure import org.matrix.android.sdk.api.session.Session import timber.log.Timber import java.util.concurrent.CancellationException @@ -569,14 +570,6 @@ class LoginViewModel @AssistedInject constructor( } else { onWellKnownError() } - is WellknownResult.InvalidMatrixId -> { - setState { - copy( - asyncLoginAction = Uninitialized - ) - } - _viewEvents.post(LoginViewEvents.Failure(Exception(stringProvider.getString(R.string.login_signin_matrix_id_error_invalid_matrix_id)))) - } else -> { onWellKnownError() } @@ -620,19 +613,23 @@ class LoginViewModel @AssistedInject constructor( } private fun onDirectLoginError(failure: Throwable) { - if (failure is Failure.UnrecognizedCertificateFailure) { - // Display this error in a dialog - _viewEvents.post(LoginViewEvents.Failure(failure)) - setState { - copy( - asyncLoginAction = Uninitialized - ) + when (failure) { + is MatrixIdFailure.InvalidMatrixId, + is Failure.UnrecognizedCertificateFailure -> { + // Display this error in a dialog + _viewEvents.post(LoginViewEvents.Failure(failure)) + setState { + copy( + asyncLoginAction = Uninitialized + ) + } } - } else { - setState { - copy( - asyncLoginAction = Fail(failure) - ) + else -> { + setState { + copy( + asyncLoginAction = Fail(failure) + ) + } } } } diff --git a/vector/src/main/java/im/vector/app/features/login2/LoginViewModel2.kt b/vector/src/main/java/im/vector/app/features/login2/LoginViewModel2.kt index 2e457551dc..110e4f7fe4 100644 --- a/vector/src/main/java/im/vector/app/features/login2/LoginViewModel2.kt +++ b/vector/src/main/java/im/vector/app/features/login2/LoginViewModel2.kt @@ -595,10 +595,6 @@ class LoginViewModel2 @AssistedInject constructor( } else { onWellKnownError() } - is WellknownResult.InvalidMatrixId -> { - setState { copy(isLoading = false) } - _viewEvents.post(LoginViewEvents2.Failure(Exception(stringProvider.getString(R.string.login_signin_matrix_id_error_invalid_matrix_id)))) - } else -> { onWellKnownError() }