Skip to content
Snippets Groups Projects
Commit 3821acff authored by Taras's avatar Taras
Browse files

Fix bad request on login

parent aa521fe3
No related branches found
No related tags found
No related merge requests found
...@@ -12,7 +12,6 @@ import org.futo.circles.auth.feature.log_in.stages.LoginStagesDataSource ...@@ -12,7 +12,6 @@ import org.futo.circles.auth.feature.log_in.stages.LoginStagesDataSource
import org.futo.circles.core.extensions.createResult import org.futo.circles.core.extensions.createResult
import org.futo.circles.core.provider.MatrixInstanceProvider import org.futo.circles.core.provider.MatrixInstanceProvider
import org.futo.circles.core.utils.HomeServerUtils.buildHomeServerConfigFromDomain import org.futo.circles.core.utils.HomeServerUtils.buildHomeServerConfigFromDomain
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
import org.matrix.android.sdk.api.auth.registration.Stage import org.matrix.android.sdk.api.auth.registration.Stage
import javax.inject.Inject import javax.inject.Inject
...@@ -28,38 +27,36 @@ class LoginDataSource @Inject constructor( ...@@ -28,38 +27,36 @@ class LoginDataSource @Inject constructor(
domain: String domain: String
) = createResult { ) = createResult {
authService.cancelPendingLoginOrRegistration() authService.cancelPendingLoginOrRegistration()
val homeServerConfig = buildHomeServerConfigFromDomain(domain) val stages = prepareLoginStages(userName, domain)
authService.initiateAuth(homeServerConfig)
val stages = prepareLoginStages(homeServerConfig, userName, domain)
loginStagesDataSource.startLoginStages(stages, userName, domain) loginStagesDataSource.startLoginStages(stages, userName, domain)
} }
private suspend fun prepareLoginStages( private suspend fun prepareLoginStages(
homeServerConfig: HomeServerConnectionConfig,
userName: String, userName: String,
domain: String domain: String
): List<Stage> { ): List<Stage> {
val homeServerConfig = buildHomeServerConfigFromDomain(domain)
val supportedLoginMethods = authService.getLoginFlow(homeServerConfig).supportedLoginTypes
return if (supportedLoginMethods.isEmpty()) {
getCircleLoginStages(userName, domain)
} else if (isPasswordLogin(supportedLoginMethods)) {
listOf(Stage.Other(true, DIRECT_LOGIN_PASSWORD_TYPE, null))
} else {
throw IllegalArgumentException(context.getString(R.string.unsupported_login_method))
}
}
private fun isPasswordLogin(methods: List<String>) = methods.contains(LOGIN_PASSWORD_TYPE)
private suspend fun getCircleLoginStages(userName: String, domain: String): List<Stage> {
val identifierParams = mapOf( val identifierParams = mapOf(
USER_PARAM_KEY to "@$userName:$domain", USER_PARAM_KEY to "@$userName:$domain",
TYPE_PARAM_KEY to LOGIN_PASSWORD_USER_ID_TYPE TYPE_PARAM_KEY to LOGIN_PASSWORD_USER_ID_TYPE
) )
val flows = val flows = authService.getLoginWizard()
authService.getLoginWizard() .getAllLoginFlows(identifierParams, context.getString(R.string.initial_device_name))
.getAllLoginFlows(identifierParams, context.getString(R.string.initial_device_name)) return flows.firstOrNull()
val stages = if (flows.isEmpty()) { ?: throw IllegalArgumentException(context.getString(R.string.unsupported_login_method))
val supportedLoginMethods = try {
authService.getLoginFlow(homeServerConfig).supportedLoginTypes
} catch (e: Throwable) {
throw IllegalArgumentException(context.getString(R.string.not_found_login_flow_for_user))
}
if (supportedLoginMethods.contains(LOGIN_PASSWORD_TYPE))
listOf(Stage.Other(true, DIRECT_LOGIN_PASSWORD_TYPE, null))
else
throw IllegalArgumentException(context.getString(R.string.unsupported_login_method))
} else {
flows.firstOrNull()
?: throw IllegalArgumentException(context.getString(R.string.unsupported_login_method))
}
return stages
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment