From 6699568eb333ad95213c1d70cf294205a030cedc Mon Sep 17 00:00:00 2001 From: Taras <tarassmakula@gmail.com> Date: Mon, 26 Sep 2022 16:47:06 +0300 Subject: [PATCH] Add initiateAuth --- .../sdk/api/auth/AuthenticationService.kt | 6 +++ .../auth/DefaultAuthenticationService.kt | 38 ++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/AuthenticationService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/AuthenticationService.kt index 5ae70e19..e780eb77 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/AuthenticationService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/AuthenticationService.kt @@ -124,4 +124,10 @@ interface AuthenticationService { initialDeviceName: String, deviceId: String? = null ): Session + + /** + * //Added to initiate auth without GET /login + * @return wellKnownResult.homeServerUrl + */ + suspend fun initiateAuth(homeServerConnectionConfig: HomeServerConnectionConfig): String } 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 446f9318..75303854 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 @@ -280,7 +280,7 @@ internal class DefaultAuthenticationService @Inject constructor( getLoginFlowResult(newAuthAPI, versions, wellknownResult.homeServerUrl) } - else -> throw Failure.OtherServerError("", HttpsURLConnection.HTTP_NOT_FOUND /* 404 */) + else -> throw Failure.OtherServerError("", HttpsURLConnection.HTTP_NOT_FOUND /* 404 */) } } @@ -415,4 +415,40 @@ internal class DefaultAuthenticationService @Inject constructor( .addSocketFactory(homeServerConnectionConfig) .build() } + + //Added to initiate auth without GET /login + override suspend fun initiateAuth(homeServerConnectionConfig: HomeServerConnectionConfig): String { + val result = runCatching { + getHomeServerUserFromWellKnown(homeServerConnectionConfig) + } + return result.fold( + { + val alteredHomeServerConnectionConfig = homeServerConnectionConfig.copy( + homeServerUriBase = Uri.parse(it) + ) + + pendingSessionData = PendingSessionData(alteredHomeServerConnectionConfig) + .also { data -> pendingSessionStore.savePendingSessionData(data) } + it + }, + { + if (it is UnrecognizedCertificateException) { + throw Failure.UnrecognizedCertificateFailure(homeServerConnectionConfig.homeServerUriBase.toString(), it.fingerprint) + } else { + throw it + } + } + ) + } + + //Added to initiate auth without GET /login + private suspend fun getHomeServerUserFromWellKnown(homeServerConnectionConfig: HomeServerConnectionConfig): String { + val domain = homeServerConnectionConfig.homeServerUri.host + ?: throw Failure.OtherServerError("", HttpsURLConnection.HTTP_NOT_FOUND /* 404 */) + + return when (val wellKnownResult = getWellknownTask.execute(GetWellknownTask.Params(domain, homeServerConnectionConfig))) { + is WellknownResult.Prompt -> wellKnownResult.homeServerUrl + else -> throw Failure.OtherServerError("", HttpsURLConnection.HTTP_NOT_FOUND /* 404 */) + } + } } -- GitLab