Login screens: mutualize the code which create session from the credentials

This commit is contained in:
Benoit Marty 2019-11-25 18:01:25 +01:00
parent 0014e8ef06
commit ab4d42fb20
5 changed files with 60 additions and 28 deletions

View file

@ -60,4 +60,7 @@ internal abstract class AuthModule {
@Binds
abstract fun bindAuthenticationService(authenticationService: DefaultAuthenticationService): AuthenticationService
@Binds
abstract fun bindSessionCreator(sessionCreator: DefaultSessionCreator): SessionCreator
}

View file

@ -47,7 +47,8 @@ internal class DefaultAuthenticationService @Inject constructor(@Unauthenticated
private val retrofitFactory: RetrofitFactory,
private val coroutineDispatchers: MatrixCoroutineDispatchers,
private val sessionParamsStore: SessionParamsStore,
private val sessionManager: SessionManager
private val sessionManager: SessionManager,
private val sessionCreator: SessionCreator
) : AuthenticationService {
private var currentHomeServerConnectionConfig: HomeServerConnectionConfig? = null
@ -104,8 +105,7 @@ internal class DefaultAuthenticationService @Inject constructor(@Unauthenticated
okHttpClient,
retrofitFactory,
coroutineDispatchers,
sessionParamsStore,
sessionManager
sessionCreator
)
} ?: error("Please call getLoginFlow() with success first")
}
@ -114,11 +114,10 @@ internal class DefaultAuthenticationService @Inject constructor(@Unauthenticated
currentHomeServerConnectionConfig?.let {
return DefaultLoginWizard(
it,
coroutineDispatchers,
sessionParamsStore,
sessionManager,
okHttpClient,
retrofitFactory,
okHttpClient
coroutineDispatchers,
sessionCreator
)
} ?: error("Please call getLoginFlow() with success first")
}
@ -133,9 +132,7 @@ internal class DefaultAuthenticationService @Inject constructor(@Unauthenticated
private suspend fun createSessionFromSso(credentials: Credentials,
homeServerConnectionConfig: HomeServerConnectionConfig): Session = withContext(coroutineDispatchers.computation) {
val sessionParams = SessionParams(credentials, homeServerConnectionConfig)
sessionParamsStore.save(sessionParams)
sessionManager.getOrCreateSession(sessionParams)
sessionCreator.createSession(credentials, homeServerConnectionConfig)
}
private fun buildAuthAPI(homeServerConnectionConfig: HomeServerConnectionConfig): AuthAPI {

View file

@ -0,0 +1,40 @@
/*
* Copyright 2019 New Vector Ltd
*
* 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 im.vector.matrix.android.internal.auth
import im.vector.matrix.android.api.auth.data.Credentials
import im.vector.matrix.android.api.auth.data.HomeServerConnectionConfig
import im.vector.matrix.android.api.auth.data.SessionParams
import im.vector.matrix.android.api.session.Session
import im.vector.matrix.android.internal.SessionManager
internal interface SessionCreator {
suspend fun createSession(credentials: Credentials, homeServerConnectionConfig: HomeServerConnectionConfig): Session
}
internal class DefaultSessionCreator @Inject constructor(
private val sessionParamsStore: SessionParamsStore,
private val sessionManager: SessionManager
) : SessionCreator {
override suspend fun createSession(credentials: Credentials, homeServerConnectionConfig: HomeServerConnectionConfig): Session {
val sessionParams = SessionParams(credentials, homeServerConnectionConfig)
sessionParamsStore.save(sessionParams)
return sessionManager.getOrCreateSession(sessionParams)
}
}

View file

@ -21,15 +21,13 @@ import dagger.Lazy
import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.auth.data.Credentials
import im.vector.matrix.android.api.auth.data.HomeServerConnectionConfig
import im.vector.matrix.android.api.auth.data.SessionParams
import im.vector.matrix.android.api.auth.login.LoginWizard
import im.vector.matrix.android.api.auth.registration.RegisterThreePid
import im.vector.matrix.android.api.session.Session
import im.vector.matrix.android.api.util.Cancelable
import im.vector.matrix.android.api.util.NoOpCancellable
import im.vector.matrix.android.internal.SessionManager
import im.vector.matrix.android.internal.auth.AuthAPI
import im.vector.matrix.android.internal.auth.SessionParamsStore
import im.vector.matrix.android.internal.auth.SessionCreator
import im.vector.matrix.android.internal.auth.data.PasswordLoginParams
import im.vector.matrix.android.internal.auth.data.ThreePidMedium
import im.vector.matrix.android.internal.auth.registration.AddThreePidRegistrationParams
@ -52,11 +50,10 @@ internal data class ResetPasswordData(
internal class DefaultLoginWizard(
private val homeServerConnectionConfig: HomeServerConnectionConfig,
private val coroutineDispatchers: MatrixCoroutineDispatchers,
private val sessionParamsStore: SessionParamsStore,
private val sessionManager: SessionManager,
okHttpClient: Lazy<OkHttpClient>,
retrofitFactory: RetrofitFactory,
okHttpClient: Lazy<OkHttpClient>
private val coroutineDispatchers: MatrixCoroutineDispatchers,
private val sessionCreator: SessionCreator
) : LoginWizard {
private var clientSecret = UUID.randomUUID().toString()
@ -87,9 +84,8 @@ internal class DefaultLoginWizard(
val credentials = executeRequest<Credentials> {
apiCall = authAPI.login(loginParams)
}
val sessionParams = SessionParams(credentials, homeServerConnectionConfig)
sessionParamsStore.save(sessionParams)
sessionManager.getOrCreateSession(sessionParams)
sessionCreator.createSession(credentials, homeServerConnectionConfig)
}
override fun resetPassword(email: String, newPassword: String, callback: MatrixCallback<Unit>): Cancelable {

View file

@ -19,7 +19,6 @@ package im.vector.matrix.android.internal.auth.registration
import dagger.Lazy
import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.auth.data.HomeServerConnectionConfig
import im.vector.matrix.android.api.auth.data.SessionParams
import im.vector.matrix.android.api.auth.registration.RegisterThreePid
import im.vector.matrix.android.api.auth.registration.RegistrationResult
import im.vector.matrix.android.api.auth.registration.RegistrationWizard
@ -27,9 +26,8 @@ import im.vector.matrix.android.api.failure.Failure
import im.vector.matrix.android.api.failure.Failure.RegistrationFlowError
import im.vector.matrix.android.api.util.Cancelable
import im.vector.matrix.android.api.util.NoOpCancellable
import im.vector.matrix.android.internal.SessionManager
import im.vector.matrix.android.internal.auth.AuthAPI
import im.vector.matrix.android.internal.auth.SessionParamsStore
import im.vector.matrix.android.internal.auth.SessionCreator
import im.vector.matrix.android.internal.auth.data.LoginFlowTypes
import im.vector.matrix.android.internal.network.RetrofitFactory
import im.vector.matrix.android.internal.task.launchToCallback
@ -53,8 +51,7 @@ internal class DefaultRegistrationWizard(private val homeServerConnectionConfig:
private val okHttpClient: Lazy<OkHttpClient>,
private val retrofitFactory: RetrofitFactory,
private val coroutineDispatchers: MatrixCoroutineDispatchers,
private val sessionParamsStore: SessionParamsStore,
private val sessionManager: SessionManager) : RegistrationWizard {
private val sessionCreator: SessionCreator) : RegistrationWizard {
private var clientSecret = UUID.randomUUID().toString()
private var sendAttempt = 0
@ -225,9 +222,8 @@ internal class DefaultRegistrationWizard(private val homeServerConnectionConfig:
throw exception
}
}
val sessionParams = SessionParams(credentials, homeServerConnectionConfig)
sessionParamsStore.save(sessionParams)
val session = sessionManager.getOrCreateSession(sessionParams)
val session = sessionCreator.createSession(credentials, homeServerConnectionConfig)
return RegistrationResult.Success(session)
}