UnifiedPush WIP
This commit is contained in:
parent
287fca5438
commit
20370656cd
28 changed files with 155 additions and 171 deletions
|
|
@ -21,6 +21,8 @@ import com.squareup.anvil.annotations.ContributesBinding
|
|||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.di.ApplicationContext
|
||||
import io.element.android.libraries.di.SingleIn
|
||||
import io.element.android.libraries.matrix.api.core.SessionId
|
||||
import io.element.android.libraries.matrix.api.core.asSessionId
|
||||
import io.element.android.libraries.pushstore.api.UserPushStore
|
||||
import io.element.android.libraries.pushstore.api.UserPushStoreFactory
|
||||
import io.element.android.libraries.sessionstorage.api.observer.SessionListener
|
||||
|
|
@ -38,8 +40,8 @@ class DefaultUserPushStoreFactory @Inject constructor(
|
|||
}
|
||||
|
||||
// We can have only one class accessing a single data store, so keep a cache of them.
|
||||
private val cache = mutableMapOf<String, UserPushStore>()
|
||||
override fun create(userId: String): UserPushStore {
|
||||
private val cache = mutableMapOf<SessionId, UserPushStore>()
|
||||
override fun create(userId: SessionId): UserPushStore {
|
||||
return cache.getOrPut(userId) {
|
||||
UserPushStoreDataStore(
|
||||
context = context,
|
||||
|
|
@ -58,6 +60,6 @@ class DefaultUserPushStoreFactory @Inject constructor(
|
|||
|
||||
override suspend fun onSessionDeleted(userId: String) {
|
||||
// Delete the store
|
||||
create(userId).reset()
|
||||
create(userId.asSessionId()).reset()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import androidx.datastore.preferences.core.Preferences
|
|||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import io.element.android.libraries.matrix.api.core.SessionId
|
||||
import io.element.android.libraries.pushstore.api.UserPushStore
|
||||
import kotlinx.coroutines.flow.first
|
||||
|
||||
|
|
@ -30,7 +31,7 @@ import kotlinx.coroutines.flow.first
|
|||
*/
|
||||
class UserPushStoreDataStore(
|
||||
private val context: Context,
|
||||
userId: String,
|
||||
userId: SessionId,
|
||||
) : UserPushStore {
|
||||
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "push_store_$userId")
|
||||
private val pushProviderName = stringPreferencesKey("pushProviderName")
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright (c) 2023 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.libraries.pushstore.impl.clientsecret
|
||||
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecretFactory
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
|
||||
@ContributesBinding(AppScope::class)
|
||||
class PushClientSecretFactoryImpl @Inject constructor() : PushClientSecretFactory {
|
||||
override fun create(): String {
|
||||
return UUID.randomUUID().toString()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Copyright (c) 2023 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.libraries.pushstore.impl.clientsecret
|
||||
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.matrix.api.core.SessionId
|
||||
import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecret
|
||||
import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecretFactory
|
||||
import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecretStore
|
||||
import javax.inject.Inject
|
||||
|
||||
@ContributesBinding(AppScope::class)
|
||||
class PushClientSecretImpl @Inject constructor(
|
||||
private val pushClientSecretFactory: PushClientSecretFactory,
|
||||
private val pushClientSecretStore: PushClientSecretStore,
|
||||
) : PushClientSecret {
|
||||
override suspend fun getSecretForUser(userId: SessionId): String {
|
||||
val existingSecret = pushClientSecretStore.getSecret(userId)
|
||||
if (existingSecret != null) {
|
||||
return existingSecret
|
||||
}
|
||||
val newSecret = pushClientSecretFactory.create()
|
||||
pushClientSecretStore.storeSecret(userId, newSecret)
|
||||
return newSecret
|
||||
}
|
||||
|
||||
override suspend fun getUserIdFromSecret(clientSecret: String): SessionId? {
|
||||
return pushClientSecretStore.getUserIdFromSecret(clientSecret)
|
||||
}
|
||||
|
||||
override suspend fun resetSecretForUser(userId: SessionId) {
|
||||
pushClientSecretStore.resetSecret(userId)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* Copyright (c) 2023 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.libraries.pushstore.impl.clientsecret
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.di.ApplicationContext
|
||||
import io.element.android.libraries.matrix.api.core.SessionId
|
||||
import io.element.android.libraries.matrix.api.core.asSessionId
|
||||
import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecretStore
|
||||
import kotlinx.coroutines.flow.first
|
||||
import javax.inject.Inject
|
||||
|
||||
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "push_client_secret_store")
|
||||
|
||||
@ContributesBinding(AppScope::class)
|
||||
class PushClientSecretStoreDataStore @Inject constructor(
|
||||
@ApplicationContext private val context: Context,
|
||||
) : PushClientSecretStore {
|
||||
override suspend fun storeSecret(userId: SessionId, clientSecret: String) {
|
||||
context.dataStore.edit { settings ->
|
||||
settings[getPreferenceKeyForUser(userId)] = clientSecret
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getSecret(userId: SessionId): String? {
|
||||
return context.dataStore.data.first()[getPreferenceKeyForUser(userId)]
|
||||
}
|
||||
|
||||
override suspend fun resetSecret(userId: SessionId) {
|
||||
context.dataStore.edit { settings ->
|
||||
settings.remove(getPreferenceKeyForUser(userId))
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getUserIdFromSecret(clientSecret: String): SessionId? {
|
||||
val keyValues = context.dataStore.data.first().asMap()
|
||||
val matchingKey = keyValues.keys.find {
|
||||
keyValues[it] == clientSecret
|
||||
}
|
||||
return matchingKey?.name?.asSessionId()
|
||||
}
|
||||
|
||||
private fun getPreferenceKeyForUser(userId: SessionId) = stringPreferencesKey(userId.value)
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2023 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.libraries.pushstore.impl.clientsecret
|
||||
|
||||
import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecretFactory
|
||||
|
||||
private const val A_SECRET_PREFIX = "A_SECRET_"
|
||||
|
||||
class FakePushClientSecretFactory : PushClientSecretFactory {
|
||||
private var index = 0
|
||||
|
||||
override fun create() = getSecretForUser(index++)
|
||||
|
||||
fun getSecretForUser(i: Int): String {
|
||||
return A_SECRET_PREFIX + i
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Copyright (c) 2023 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.libraries.pushstore.impl.clientsecret
|
||||
|
||||
import io.element.android.libraries.matrix.api.core.SessionId
|
||||
import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecretStore
|
||||
|
||||
class InMemoryPushClientSecretStore : PushClientSecretStore {
|
||||
private val secrets = mutableMapOf<SessionId, String>()
|
||||
|
||||
fun getSecrets(): Map<SessionId, String> = secrets
|
||||
|
||||
override suspend fun storeSecret(userId: SessionId, clientSecret: String) {
|
||||
secrets[userId] = clientSecret
|
||||
}
|
||||
|
||||
override suspend fun getSecret(userId: SessionId): String? {
|
||||
return secrets[userId]
|
||||
}
|
||||
|
||||
override suspend fun resetSecret(userId: SessionId) {
|
||||
secrets.remove(userId)
|
||||
}
|
||||
|
||||
override suspend fun getUserIdFromSecret(clientSecret: String): SessionId? {
|
||||
return secrets.keys.firstOrNull { secrets[it] == clientSecret }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* Copyright (c) 2023 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.
|
||||
*/
|
||||
|
||||
@file:OptIn(ExperimentalCoroutinesApi::class)
|
||||
|
||||
package io.element.android.libraries.pushstore.impl.clientsecret
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import io.element.android.libraries.matrix.api.core.SessionId
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Test
|
||||
|
||||
private val A_USER_ID_0 = SessionId("A_USER_ID_0")
|
||||
private val A_USER_ID_1 = SessionId("A_USER_ID_1")
|
||||
|
||||
private const val A_UNKNOWN_SECRET = "A_UNKNOWN_SECRET"
|
||||
|
||||
internal class PushClientSecretImplTest {
|
||||
|
||||
@Test
|
||||
fun test() = runTest {
|
||||
val factory = FakePushClientSecretFactory()
|
||||
val store = InMemoryPushClientSecretStore()
|
||||
val sut = PushClientSecretImpl(factory, store)
|
||||
|
||||
val secret0 = factory.getSecretForUser(0)
|
||||
val secret1 = factory.getSecretForUser(1)
|
||||
val secret2 = factory.getSecretForUser(2)
|
||||
|
||||
assertThat(store.getSecrets()).isEmpty()
|
||||
assertThat(sut.getUserIdFromSecret(secret0)).isNull()
|
||||
// Create a secret
|
||||
assertThat(sut.getSecretForUser(A_USER_ID_0)).isEqualTo(secret0)
|
||||
assertThat(store.getSecrets()).hasSize(1)
|
||||
// Same secret returned
|
||||
assertThat(sut.getSecretForUser(A_USER_ID_0)).isEqualTo(secret0)
|
||||
assertThat(store.getSecrets()).hasSize(1)
|
||||
// Another secret returned for another user
|
||||
assertThat(sut.getSecretForUser(A_USER_ID_1)).isEqualTo(secret1)
|
||||
assertThat(store.getSecrets()).hasSize(2)
|
||||
|
||||
// Get users from secrets
|
||||
assertThat(sut.getUserIdFromSecret(secret0)).isEqualTo(A_USER_ID_0)
|
||||
assertThat(sut.getUserIdFromSecret(secret1)).isEqualTo(A_USER_ID_1)
|
||||
// Unknown secret
|
||||
assertThat(sut.getUserIdFromSecret(A_UNKNOWN_SECRET)).isNull()
|
||||
|
||||
// User signs out
|
||||
sut.resetSecretForUser(A_USER_ID_0)
|
||||
assertThat(store.getSecrets()).hasSize(1)
|
||||
// Create a new secret after reset
|
||||
assertThat(sut.getSecretForUser(A_USER_ID_0)).isEqualTo(secret2)
|
||||
|
||||
// Check the store content
|
||||
assertThat(store.getSecrets()).isEqualTo(
|
||||
mapOf(
|
||||
A_USER_ID_0 to secret2,
|
||||
A_USER_ID_1 to secret1,
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue