-
Notifications
You must be signed in to change notification settings - Fork 186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enforce mandatory session verification only for new logins #2811
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Enforce mandatory session verification only for new logins. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,40 +24,46 @@ import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import io.element.android.features.api.MigrationState | ||
import io.element.android.features.rageshake.api.logs.LogFilesRemover | ||
import io.element.android.features.migration.impl.migrations.AppMigration | ||
import io.element.android.libraries.architecture.AsyncData | ||
import io.element.android.libraries.architecture.Presenter | ||
import io.element.android.libraries.di.AppScope | ||
import io.element.android.libraries.di.SingleIn | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
@SingleIn(AppScope::class) | ||
class MigrationPresenter @Inject constructor( | ||
private val migrationStore: MigrationStore, | ||
private val logFilesRemover: LogFilesRemover, | ||
private val migrations: Set<@JvmSuppressWildcards AppMigration>, | ||
) : Presenter<MigrationState> { | ||
@Composable | ||
override fun present(): MigrationState { | ||
val migrationStoreVersion = migrationStore.applicationMigrationVersion().collectAsState(initial = null) | ||
val migrationStoreVersion by migrationStore.applicationMigrationVersion().collectAsState(initial = null) | ||
var migrationAction: AsyncData<Unit> by remember { mutableStateOf(AsyncData.Uninitialized) } | ||
|
||
/* | ||
// Uncomment this block to run the migration everytime | ||
LaunchedEffect(Unit) { | ||
migrationStore.setApplicationMigrationVersion(0) | ||
} | ||
*/ | ||
// LaunchedEffect(Unit) { | ||
// Timber.d("Resetting migration version to 0") | ||
// migrationStore.setApplicationMigrationVersion(0) | ||
// } | ||
|
||
val orderedMigrations = migrations.sortedBy { it.order } | ||
|
||
LaunchedEffect(migrationStoreVersion.value) { | ||
val migrationValue = migrationStoreVersion.value ?: return@LaunchedEffect | ||
LaunchedEffect(migrationStoreVersion) { | ||
val migrationValue = migrationStoreVersion ?: return@LaunchedEffect | ||
if (migrationValue == MIGRATION_VERSION) { | ||
Timber.d("Current app migration version: $migrationValue. No migration needed.") | ||
migrationAction = AsyncData.Success(Unit) | ||
return@LaunchedEffect | ||
} | ||
migrationAction = AsyncData.Loading(Unit) | ||
if (migrationValue < 1) { | ||
logFilesRemover.perform() | ||
val nextMigration = orderedMigrations.firstOrNull { it.order > migrationValue } | ||
if (nextMigration != null) { | ||
Timber.d("Current app migration version: $migrationValue. Applying migration: ${nextMigration.order}") | ||
nextMigration.migrate() | ||
migrationStore.setApplicationMigrationVersion(nextMigration.order) | ||
} | ||
// Add new step here | ||
|
||
migrationStore.setApplicationMigrationVersion(MIGRATION_VERSION) | ||
} | ||
|
||
return MigrationState( | ||
|
@@ -68,6 +74,6 @@ class MigrationPresenter @Inject constructor( | |
companion object { | ||
// Increment this value when you need to run the migration again, and | ||
// add step in the LaunchedEffect above | ||
const val MIGRATION_VERSION = 1 | ||
const val MIGRATION_VERSION = 2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For instance, the new code never write
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright (c) 2024 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 io.element.android.features.migration.impl.migrations | ||
|
||
interface AppMigration { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be moved to the |
||
val order: Int | ||
suspend fun migrate() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (c) 2024 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 io.element.android.features.migration.impl.migrations | ||
|
||
import com.squareup.anvil.annotations.ContributesMultibinding | ||
import io.element.android.features.rageshake.api.logs.LogFilesRemover | ||
import io.element.android.libraries.di.AppScope | ||
import javax.inject.Inject | ||
|
||
@ContributesMultibinding(AppScope::class) | ||
class AppMigration01 @Inject constructor( | ||
private val logFilesRemover: LogFilesRemover, | ||
) : AppMigration { | ||
override val order: Int = 1 | ||
|
||
override suspend fun migrate() { | ||
logFilesRemover.perform() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2024 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 io.element.android.features.migration.impl.migrations | ||
|
||
import com.squareup.anvil.annotations.ContributesMultibinding | ||
import io.element.android.features.preferences.api.store.SessionPreferencesStoreFactory | ||
import io.element.android.libraries.di.AppScope | ||
import io.element.android.libraries.matrix.api.core.SessionId | ||
import io.element.android.libraries.sessionstorage.api.SessionStore | ||
import kotlinx.coroutines.coroutineScope | ||
import javax.inject.Inject | ||
|
||
@ContributesMultibinding(AppScope::class) | ||
class AppMigration02 @Inject constructor( | ||
private val sessionStore: SessionStore, | ||
private val sessionPreferenceStoreFactory: SessionPreferencesStoreFactory, | ||
) : AppMigration { | ||
override val order: Int = 2 | ||
|
||
override suspend fun migrate() { | ||
coroutineScope { | ||
for (session in sessionStore.getAllSessions()) { | ||
val sessionId = SessionId(session.userId) | ||
val preferences = sessionPreferenceStoreFactory.get(sessionId, this) | ||
preferences.setSkipSessionVerification(true) | ||
// This session preference store must be ephemeral since it's not created with the right coroutine scope | ||
sessionPreferenceStoreFactory.remove(sessionId) | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (c) 2024 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 io.element.android.features.migration.impl.migrations | ||
|
||
import io.element.android.features.rageshake.test.logs.FakeLogFilesRemover | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Test | ||
|
||
class AppMigration01Test { | ||
@Test | ||
fun `test migration`() = runTest { | ||
val logsFileRemover = FakeLogFilesRemover() | ||
val migration = AppMigration01(logsFileRemover) | ||
|
||
migration.migrate() | ||
|
||
logsFileRemover.performLambda.assertions().isCalledOnce() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be moved as a Class member I think, and so
migrations
should not be a class member, to avoid any mistake.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean storing it as a property and making
migrations
just a constructor parameter?