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 5ae70e1978c0e5bc9398560a12b81123b6a369ca..e780eb77729dea5593915b7cfabade9c0779714b 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 446f9318479fc16d0898f84d23b6c034b68645e3..75303854ca15e200b7268c542b2ebbae5f3d1a27 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 */) + } + } }