Skip to content

Commit fc79615

Browse files
authored
Merge pull request #8193 from vector-im/feature/bma/certList
Be able to let the user trust several Fingerprints during login flow.
2 parents d5f75c6 + 448374f commit fc79615

File tree

4 files changed

+21
-24
lines changed

4 files changed

+21
-24
lines changed

vector/src/main/java/im/vector/app/features/login/HomeServerConnectionConfigFactory.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,15 @@ import javax.inject.Inject
2323

2424
class HomeServerConnectionConfigFactory @Inject constructor() {
2525

26-
fun create(url: String?, fingerprint: Fingerprint? = null): HomeServerConnectionConfig? {
26+
fun create(url: String?, fingerprints: List<Fingerprint>? = null): HomeServerConnectionConfig? {
2727
if (url == null) {
2828
return null
2929
}
3030

3131
return try {
3232
HomeServerConnectionConfig.Builder()
3333
.withHomeServerUri(url)
34-
.run {
35-
if (fingerprint == null) {
36-
this
37-
} else {
38-
withAllowedFingerPrints(listOf(fingerprint))
39-
}
40-
}
34+
.withAllowedFingerPrints(fingerprints)
4135
.build()
4236
} catch (t: Throwable) {
4337
Timber.e(t)

vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ class OnboardingViewModel @AssistedInject constructor(
160160
private var emailVerificationPollingJob: Job? by cancelCurrentOnSet()
161161
private var currentJob: Job? by cancelCurrentOnSet()
162162

163+
private val trustedFingerprints = mutableListOf<Fingerprint>()
164+
163165
override fun handle(action: OnboardingAction) {
164166
when (action) {
165167
is OnboardingAction.SplashAction -> handleSplashAction(action)
@@ -293,13 +295,14 @@ class OnboardingViewModel @AssistedInject constructor(
293295
private fun handleUserAcceptCertificate(action: OnboardingAction.UserAcceptCertificate) {
294296
// It happens when we get the login flow, or during direct authentication.
295297
// So alter the homeserver config and retrieve again the login flow
298+
trustedFingerprints.add(action.fingerprint)
296299
when (action.retryAction) {
297-
is OnboardingAction.HomeServerChange -> handleHomeserverChange(action.retryAction, fingerprint = action.fingerprint)
300+
is OnboardingAction.HomeServerChange -> handleHomeserverChange(action.retryAction, fingerprints = trustedFingerprints)
298301
is AuthenticateAction.LoginDirect ->
299302
handleDirectLogin(
300303
action.retryAction,
301304
// Will be replaced by the task
302-
homeServerConnectionConfigFactory.create("https://dummy.org", action.fingerprint)
305+
homeServerConnectionConfigFactory.create("https://dummy.org", trustedFingerprints)
303306
)
304307
else -> Unit
305308
}
@@ -696,10 +699,10 @@ class OnboardingViewModel @AssistedInject constructor(
696699
private fun handleHomeserverChange(
697700
action: OnboardingAction.HomeServerChange,
698701
serverTypeOverride: ServerType? = null,
699-
fingerprint: Fingerprint? = null,
702+
fingerprints: List<Fingerprint>? = null,
700703
postAction: suspend () -> Unit = {},
701704
) {
702-
val homeServerConnectionConfig = homeServerConnectionConfigFactory.create(action.homeServerUrl, fingerprint)
705+
val homeServerConnectionConfig = homeServerConnectionConfigFactory.create(action.homeServerUrl, fingerprints)
703706
if (homeServerConnectionConfig == null) {
704707
// This is invalid
705708
_viewEvents.post(OnboardingViewEvents.Failure(Throwable("Unable to create a HomeServerConnectionConfig")))

vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ class OnboardingViewModelTest {
621621

622622
@Test
623623
fun `when editing homeserver errors with certificate error, then emits error`() = runTest {
624-
fakeHomeServerConnectionConfigFactory.givenConfigFor(A_HOMESERVER_URL, fingerprint = null, A_HOMESERVER_CONFIG)
624+
fakeHomeServerConnectionConfigFactory.givenConfigFor(A_HOMESERVER_URL, fingerprints = null, A_HOMESERVER_CONFIG)
625625
fakeStartAuthenticationFlowUseCase.givenErrors(A_HOMESERVER_CONFIG, AN_UNRECOGNISED_CERTIFICATE_ERROR)
626626
val editAction = OnboardingAction.HomeServerChange.EditHomeServer(A_HOMESERVER_URL)
627627
val test = viewModel.test()
@@ -640,7 +640,7 @@ class OnboardingViewModelTest {
640640

641641
@Test
642642
fun `when selecting homeserver errors with certificate error, then emits error`() = runTest {
643-
fakeHomeServerConnectionConfigFactory.givenConfigFor(A_HOMESERVER_URL, fingerprint = null, A_HOMESERVER_CONFIG)
643+
fakeHomeServerConnectionConfigFactory.givenConfigFor(A_HOMESERVER_URL, fingerprints = null, A_HOMESERVER_CONFIG)
644644
fakeStartAuthenticationFlowUseCase.givenErrors(A_HOMESERVER_CONFIG, AN_UNRECOGNISED_CERTIFICATE_ERROR)
645645
val selectAction = OnboardingAction.HomeServerChange.SelectHomeServer(A_HOMESERVER_URL)
646646
val test = viewModel.test()
@@ -842,7 +842,7 @@ class OnboardingViewModelTest {
842842
A_HOMESERVER_URL,
843843
SELECTED_HOMESERVER_STATE,
844844
config = A_HOMESERVER_CONFIG.copy(allowedFingerprints = listOf(A_FINGERPRINT)),
845-
fingerprint = A_FINGERPRINT,
845+
fingerprints = listOf(A_FINGERPRINT),
846846
)
847847

848848
viewModel.handle(OnboardingAction.UserAcceptCertificate(A_FINGERPRINT, OnboardingAction.HomeServerChange.SelectHomeServer(A_HOMESERVER_URL)))
@@ -866,7 +866,7 @@ class OnboardingViewModelTest {
866866
A_HOMESERVER_URL,
867867
SELECTED_HOMESERVER_STATE,
868868
config = A_HOMESERVER_CONFIG.copy(allowedFingerprints = listOf(A_FINGERPRINT)),
869-
fingerprint = A_FINGERPRINT,
869+
fingerprints = listOf(A_FINGERPRINT),
870870
)
871871
val test = viewModel.test()
872872

@@ -886,7 +886,7 @@ class OnboardingViewModelTest {
886886

887887
@Test
888888
fun `given DirectLogin retry action, when accepting user certificate, then logs in directly`() = runTest {
889-
fakeHomeServerConnectionConfigFactory.givenConfigFor("https://dummy.org", A_FINGERPRINT, A_HOMESERVER_CONFIG)
889+
fakeHomeServerConnectionConfigFactory.givenConfigFor("https://dummy.org", listOf(A_FINGERPRINT), A_HOMESERVER_CONFIG)
890890
fakeDirectLoginUseCase.givenSuccessResult(A_DIRECT_LOGIN, config = A_HOMESERVER_CONFIG, result = fakeSession)
891891
givenInitialisesSession(fakeSession)
892892
val test = viewModel.test()
@@ -1175,16 +1175,16 @@ class OnboardingViewModelTest {
11751175
homeserverUrl: String,
11761176
resultingState: SelectedHomeserverState,
11771177
config: HomeServerConnectionConfig = A_HOMESERVER_CONFIG,
1178-
fingerprint: Fingerprint? = null,
1178+
fingerprints: List<Fingerprint>? = null,
11791179
) {
1180-
fakeHomeServerConnectionConfigFactory.givenConfigFor(homeserverUrl, fingerprint, config)
1180+
fakeHomeServerConnectionConfigFactory.givenConfigFor(homeserverUrl, fingerprints, config)
11811181
fakeStartAuthenticationFlowUseCase.givenResult(config, StartAuthenticationResult(isHomeserverOutdated = false, resultingState))
11821182
givenRegistrationResultFor(RegisterAction.StartRegistration, RegistrationActionHandler.Result.StartRegistration)
11831183
fakeHomeServerHistoryService.expectUrlToBeAdded(config.homeServerUri.toString())
11841184
}
11851185

11861186
private fun givenUpdatingHomeserverErrors(homeserverUrl: String, resultingState: SelectedHomeserverState, error: Throwable) {
1187-
fakeHomeServerConnectionConfigFactory.givenConfigFor(homeserverUrl, fingerprint = null, A_HOMESERVER_CONFIG)
1187+
fakeHomeServerConnectionConfigFactory.givenConfigFor(homeserverUrl, fingerprints = null, A_HOMESERVER_CONFIG)
11881188
fakeStartAuthenticationFlowUseCase.givenResult(A_HOMESERVER_CONFIG, StartAuthenticationResult(isHomeserverOutdated = false, resultingState))
11891189
givenRegistrationResultFor(RegisterAction.StartRegistration, RegistrationActionHandler.Result.Error(error))
11901190
fakeHomeServerHistoryService.expectUrlToBeAdded(A_HOMESERVER_CONFIG.homeServerUri.toString())
@@ -1209,13 +1209,13 @@ class OnboardingViewModelTest {
12091209

12101210
private fun givenHomeserverSelectionFailsWithNetworkError() {
12111211
fakeContext.givenHasConnection()
1212-
fakeHomeServerConnectionConfigFactory.givenConfigFor(A_HOMESERVER_URL, fingerprint = null, A_HOMESERVER_CONFIG)
1212+
fakeHomeServerConnectionConfigFactory.givenConfigFor(A_HOMESERVER_URL, fingerprints = null, A_HOMESERVER_CONFIG)
12131213
fakeStartAuthenticationFlowUseCase.givenHomeserverUnavailable(A_HOMESERVER_CONFIG)
12141214
}
12151215

12161216
private fun givenHomeserverSelectionFailsWith(cause: Throwable) {
12171217
fakeContext.givenHasConnection()
1218-
fakeHomeServerConnectionConfigFactory.givenConfigFor(A_HOMESERVER_URL, fingerprint = null, A_HOMESERVER_CONFIG)
1218+
fakeHomeServerConnectionConfigFactory.givenConfigFor(A_HOMESERVER_URL, fingerprints = null, A_HOMESERVER_CONFIG)
12191219
fakeStartAuthenticationFlowUseCase.givenErrors(A_HOMESERVER_CONFIG, cause)
12201220
}
12211221
}

vector/src/test/java/im/vector/app/test/fakes/FakeHomeServerConnectionConfigFactory.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import org.matrix.android.sdk.api.network.ssl.Fingerprint
2525
class FakeHomeServerConnectionConfigFactory {
2626
val instance: HomeServerConnectionConfigFactory = mockk()
2727

28-
fun givenConfigFor(url: String, fingerprint: Fingerprint? = null, config: HomeServerConnectionConfig) {
29-
every { instance.create(url, fingerprint) } returns config
28+
fun givenConfigFor(url: String, fingerprints: List<Fingerprint>? = null, config: HomeServerConnectionConfig) {
29+
every { instance.create(url, fingerprints) } returns config
3030
}
3131
}

0 commit comments

Comments
 (0)