Skip to content

StreamVault is a TV-first free IPTV player for Android TV built with Kotlin, Jetpack Compose, Room, Hilt, and Media3.

perf: add redacted provider configuration projection

David Nashash27343b09ae69
diff --git a/data/src/main/java/com/streamvault/data/provider/ProviderPublicProjection.kt b/data/src/main/java/com/streamvault/data/provider/ProviderPublicProjection.kt
new file mode 100644
index 00000000..27ecec0c
--- /dev/null
+++ b/data/src/main/java/com/streamvault/data/provider/ProviderPublicProjection.kt
@@ -0,0 +1,138 @@
+package com.streamvault.data.provider
+
+import com.google.gson.Gson
+import com.streamvault.data.local.entity.ProviderAccountRuntimeEntity
+import com.streamvault.data.local.entity.ProviderConfigEntity
+import com.streamvault.data.local.entity.ProviderEntity
+import com.streamvault.data.local.entity.StalkerPortalStateEntity
+import com.streamvault.data.mapper.toDomain
+import com.streamvault.domain.model.LegacyProvider as Provider
+import com.streamvault.domain.model.Provider as StableProvider
+import com.streamvault.domain.model.ProviderAccountRuntime
+import com.streamvault.domain.model.ProviderType
+
+internal data class RedactedProviderConfigurationProjection(
+ val providerType: ProviderType,
+ val configurationGeneration: Long,
+ val providerTemplate: Provider
+)
+
+internal interface ProviderPublicProjection {
+ fun decode(entity: ProviderConfigEntity): RedactedProviderConfigurationProjection
+
+ fun assemble(
+ identity: ProviderEntity,
+ configuration: RedactedProviderConfigurationProjection?,
+ runtime: ProviderAccountRuntimeEntity?,
+ portalState: StalkerPortalStateEntity?
+ ): Provider
+}
+
+internal class DefaultProviderPublicProjection(
+ private val codec: ProviderConfigurationCodec,
+ private val gson: Gson
+) : ProviderPublicProjection {
+
+ override fun decode(entity: ProviderConfigEntity): RedactedProviderConfigurationProjection {
+ val configuration = codec.decode(entity.type, entity.encryptedConfigJson)
+ val templateIdentity = StableProvider(
+ id = entity.providerId,
+ name = PROJECTION_TEMPLATE_NAME,
+ type = entity.type
+ )
+ val template = configuration
+ .toLegacyProvider(templateIdentity, ProviderAccountRuntime())
+ .redactedCredentials()
+ return RedactedProviderConfigurationProjection(
+ providerType = entity.type,
+ configurationGeneration = entity.configurationGeneration,
+ providerTemplate = template
+ )
+ }
+
+ override fun assemble(
+ identity: ProviderEntity,
+ configuration: RedactedProviderConfigurationProjection?,
+ runtime: ProviderAccountRuntimeEntity?,
+ portalState: StalkerPortalStateEntity?
+ ): Provider {
+ configuration ?: return identity.toPublicDomain()
+ check(identity.type == configuration.providerType) {
+ "Provider/configuration type mismatch for ${identity.id}"
+ }
+
+ val accountRuntime = runtime?.toDomainRuntime(gson) ?: ProviderAccountRuntime()
+ val learning = if (identity.type == ProviderType.STALKER_PORTAL) {
+ portalState?.toGenerationValidLearning(gson, configuration.configurationGeneration)
+ } else {
+ null
+ }
+ return configuration.providerTemplate.copy(
+ id = identity.id,
+ name = identity.name,
+ type = identity.type,
+ isActive = identity.isActive,
+ status = identity.status,
+ lastSyncedAt = identity.lastSyncedAt,
+ createdAt = identity.createdAt,
+ maxConnections = accountRuntime.maxConnections,
+ expirationDate = accountRuntime.expirationDate,
+ apiVersion = accountRuntime.apiVersion,
+ allowedOutputFormats = accountRuntime.allowedOutputFormats,
+ catalogLayout = accountRuntime.catalogLayout,
+ catalogLayoutDetectionVersion = accountRuntime.catalogLayoutDetectionVersion,
+ stalkerConfigurationGeneration = if (identity.type == ProviderType.STALKER_PORTAL) {
+ configuration.configurationGeneration
+ } else {
+ configuration.providerTemplate.stalkerConfigurationGeneration
+ },
+ stalkerLearnedProfileId = learning?.profileId?.value
+ ?: configuration.providerTemplate.stalkerLearnedProfileId,
+ stalkerProfileRevision = learning?.profileRevision?.value
+ ?: configuration.providerTemplate.stalkerProfileRevision,
+ stalkerProfileVerification = learning?.profileVerification?.value
+ ?: configuration.providerTemplate.stalkerProfileVerification,
+ stalkerPortalProfile = learning?.portalProfile?.value
+ ?: configuration.providerTemplate.stalkerPortalProfile,
+ stalkerPortalFingerprint = learning?.portalFingerprint?.value
+ ?: configuration.providerTemplate.stalkerPortalFingerprint,
+ stalkerMagPreset = learning?.magPreset?.value
+ ?: configuration.providerTemplate.stalkerMagPreset,
+ stalkerProtocolFamily = learning?.protocolFamily?.value
+ ?: configuration.providerTemplate.stalkerProtocolFamily,
+ stalkerLastBootstrapRecipe = learning?.bootstrapRecipe?.value
+ ?: configuration.providerTemplate.stalkerLastBootstrapRecipe,
+ stalkerEndpointPreference = learning?.endpointPreference?.value
+ ?: configuration.providerTemplate.stalkerEndpointPreference,
+ stalkerCookieMode = learning?.cookieMode?.value
+ ?: configuration.providerTemplate.stalkerCookieMode,
+ stalkerPlaybackBackendHint = learning?.playbackBackendHint?.value
+ ?: configuration.providerTemplate.stalkerPlaybackBackendHint,
+ stalkerLastPlaybackMode = learning?.lastPlaybackMode?.value
+ ?: configuration.providerTemplate.stalkerLastPlaybackMode,
+ password = ""
+ )
+ }
+
+ private companion object {
+ const val PROJECTION_TEMPLATE_NAME = "__provider_projection__"
+ }
+}
+
+internal fun ProviderAccountRuntimeEntity.toDomainRuntime(gson: Gson): ProviderAccountRuntime =
+ ProviderAccountRuntime(
+ maxConnections = maxConnections,
+ expirationDate = expirationDate,
+ apiVersion = apiVersion,
+ allowedOutputFormats = runCatching {
+ gson.fromJson(allowedOutputFormatsJson, Array<String>::class.java).toList()
+ }.getOrDefault(emptyList()),
+ catalogLayout = catalogLayout,
+ catalogLayoutDetectionVersion = catalogLayoutDetectionVersion,
+ observedAt = observedAt
+ )
+
+internal fun ProviderEntity.toPublicDomain(): Provider = toDomain().copy(password = "")
+
+/** Public provider projections must never expose decrypted account credentials. */
+internal fun Provider.redactedCredentials(): Provider = copy(password = "")
diff --git a/data/src/main/java/com/streamvault/data/repository/ProviderRepositoryImpl.kt b/data/src/main/java/com/streamvault/data/repository/ProviderRepositoryImpl.kt
index c8adc3c3..9221f4b8 100644
--- a/data/src/main/java/com/streamvault/data/repository/ProviderRepositoryImpl.kt
+++ b/data/src/main/java/com/streamvault/data/repository/ProviderRepositoryImpl.kt
@@ -27,6 +27,9 @@ import com.streamvault.data.provider.ProviderCapabilityResolver
import com.streamvault.data.provider.StalkerClientOptions
import com.streamvault.data.provider.TypedProviderClientFactory
import com.streamvault.data.provider.toAccountRuntime
+import com.streamvault.data.provider.toDomainRuntime
+import com.streamvault.data.provider.toPublicDomain
+import com.streamvault.data.provider.redactedCredentials
import com.streamvault.data.provider.toTypedConfiguration
import com.streamvault.data.provider.toLegacyProvider
import com.streamvault.data.provider.toGenerationValidLearning
@@ -178,7 +181,7 @@ class ProviderRepositoryImpl @Inject constructor(
lastSyncedAt = identity.lastSyncedAt,
createdAt = identity.createdAt
)
- val runtime = runtimesByProvider[identity.id]?.toDomainRuntime()
+ val runtime = runtimesByProvider[identity.id]?.toDomainRuntime(gson)
?: ProviderAccountRuntime()
val learning = if (identity.type == ProviderType.STALKER_PORTAL) {
portalStatesByProvider[identity.id]?.toGenerationValidLearning(
@@ -1498,18 +1501,6 @@ class ProviderRepositoryImpl @Inject constructor(
private suspend fun loadLegacyProvider(providerId: Long): Provider? =
providerCapabilityResolver.snapshot(providerId)?.toLegacyProvider()
- private fun ProviderAccountRuntimeEntity.toDomainRuntime() = ProviderAccountRuntime(
- maxConnections = maxConnections,
- expirationDate = expirationDate,
- apiVersion = apiVersion,
- allowedOutputFormats = runCatching {
- gson.fromJson(allowedOutputFormatsJson, Array<String>::class.java).toList()
- }.getOrDefault(emptyList()),
- catalogLayout = catalogLayout,
- catalogLayoutDetectionVersion = catalogLayoutDetectionVersion,
- observedAt = observedAt
- )
-
private suspend fun restoreStalkerEditIfStillPending(
existingProvider: Provider?,
pendingEdit: PendingProviderEdit?,
@@ -2056,13 +2047,6 @@ class ProviderRepositoryImpl @Inject constructor(
}
}
- private fun ProviderEntity.toPublicDomain(): Provider {
- return toDomain().copy(password = "")
- }
-
- /** Public provider projections must never expose decrypted account credentials. */
- private fun Provider.redactedCredentials(): Provider = copy(password = "")
-
/**
* Settings edit a redacted public projection. Do not turn those non-secret edits into a
* credential deletion; explicit setup/password changes still pass a nonblank value through.
diff --git a/data/src/test/java/com/streamvault/data/provider/ProviderPublicProjectionTest.kt b/data/src/test/java/com/streamvault/data/provider/ProviderPublicProjectionTest.kt
new file mode 100644
index 00000000..6915f8b1
--- /dev/null
+++ b/data/src/test/java/com/streamvault/data/provider/ProviderPublicProjectionTest.kt
@@ -0,0 +1,235 @@
+package com.streamvault.data.provider
+
+import com.google.common.truth.Truth.assertThat
+import com.google.gson.Gson
+import com.streamvault.data.local.entity.ProviderAccountRuntimeEntity
+import com.streamvault.data.local.entity.ProviderConfigEntity
+import com.streamvault.data.local.entity.ProviderEntity
+import com.streamvault.data.local.entity.StalkerPortalStateEntity
+import com.streamvault.data.security.CredentialCrypto
+import com.streamvault.domain.model.CatalogLayout
+import com.streamvault.domain.model.JellyfinConfig
+import com.streamvault.domain.model.M3uConfig
+import com.streamvault.domain.model.Provider as StableProvider
+import com.streamvault.domain.model.ProviderConfiguration
+import com.streamvault.domain.model.ProviderEpgSyncMode
+import com.streamvault.domain.model.ProviderSnapshot
+import com.streamvault.domain.model.ProviderStatus
+import com.streamvault.domain.model.ProviderType
+import com.streamvault.domain.model.StalkerConfig
+import com.streamvault.domain.model.StalkerDeviceIdentity
+import com.streamvault.domain.model.StalkerObservation
+import com.streamvault.domain.model.StalkerObservationSource
+import com.streamvault.domain.model.StalkerPortalLearning
+import com.streamvault.domain.model.XtreamConfig
+import org.junit.Assert.assertThrows
+import org.junit.Test
+import java.util.concurrent.atomic.AtomicInteger
+
+class ProviderPublicProjectionTest {
+
+ private val gson = Gson()
+ private val crypto = CountingCredentialCrypto()
+ private val codec = ProviderConfigurationCodec(gson, crypto)
+
+ @Test
+ fun `decode returns a redacted template and preserves public configuration fields`() {
+ val projection = defaultPublicProjection()
+
+ val decoded = projection.decode(
+ configEntity(
+ XtreamConfig(
+ serverUrl = "https://example.test",
+ username = "user",
+ password = "secret"
+ )
+ )
+ )
+
+ assertThat(decoded.providerType).isEqualTo(ProviderType.XTREAM_CODES)
+ assertThat(decoded.configurationGeneration).isEqualTo(7L)
+ assertThat(decoded.providerTemplate.serverUrl).isEqualTo("https://example.test")
+ assertThat(decoded.providerTemplate.username).isEqualTo("user")
+ assertThat(decoded.providerTemplate.password).isEmpty()
+ assertThat(crypto.decryptCalls.get()).isEqualTo(1)
+ }
+
+ @Test
+ fun `assemble overlays identity runtime and generation-valid Stalker learning`() {
+ val projection = defaultPublicProjection()
+ val configuration = StalkerConfig(
+ portalUrl = "https://portal.example",
+ device = StalkerDeviceIdentity("00:11:22:33:44:55"),
+ username = "user",
+ password = "secret"
+ )
+ val row = configEntity(configuration, generation = 4L)
+ val decoded = projection.decode(row)
+ val portalState = StalkerPortalStateEntity(
+ providerId = 9L,
+ configurationGeneration = 4L,
+ learningJson = gson.toJson(
+ StalkerPortalLearning(
+ configurationGeneration = 4L,
+ profileId = StalkerObservation(
+ value = "learned-profile",
+ configurationGeneration = 4L,
+ source = StalkerObservationSource.DISCOVERY,
+ observedAt = 10L
+ )
+ )
+ )
+ )
+
+ val result = projection.assemble(
+ identity = providerEntity(id = 9L, name = "Living room", type = ProviderType.STALKER_PORTAL),
+ configuration = decoded,
+ runtime = ProviderAccountRuntimeEntity(
+ providerId = 9L,
+ maxConnections = 3,
+ allowedOutputFormatsJson = "[\"m3u8\"]",
+ catalogLayout = CatalogLayout.UNIFIED_VOD,
+ catalogLayoutDetectionVersion = 2
+ ),
+ portalState = portalState
+ )
+
+ assertThat(result.name).isEqualTo("Living room")
+ assertThat(result.maxConnections).isEqualTo(3)
+ assertThat(result.allowedOutputFormats).containsExactly("m3u8")
+ assertThat(result.catalogLayout).isEqualTo(CatalogLayout.UNIFIED_VOD)
+ assertThat(result.stalkerConfigurationGeneration).isEqualTo(4L)
+ assertThat(result.stalkerLearnedProfileId).isEqualTo("learned-profile")
+ assertThat(result.password).isEmpty()
+ }
+
+ @Test
+ fun `assemble rejects a configuration type mismatch`() {
+ val projection = defaultPublicProjection()
+ val mismatched = RedactedProviderConfigurationProjection(
+ providerType = ProviderType.M3U,
+ configurationGeneration = 1L,
+ providerTemplate = M3uConfig(
+ playlistUrl = "https://playlist.example"
+ ).toLegacyProvider(
+ identity = StableProvider(
+ id = 1L,
+ name = "template",
+ type = ProviderType.M3U
+ ),
+ runtime = com.streamvault.domain.model.ProviderAccountRuntime()
+ )
+ )
+
+ assertThrows(IllegalStateException::class.java) {
+ projection.assemble(
+ identity = providerEntity(id = 1L, name = "Xtream", type = ProviderType.XTREAM_CODES),
+ configuration = mismatched,
+ runtime = null,
+ portalState = null
+ )
+ }
+ }
+
+ @Test
+ fun `assembly matches the snapshot mapper for every provider subtype`() {
+ val cases = listOf<ProviderConfiguration>(
+ XtreamConfig("https://x.test", "alice", "secret"),
+ M3uConfig("https://m.test/list.m3u", epgUrl = "https://m.test/epg.xml"),
+ StalkerConfig(
+ portalUrl = "https://s.test",
+ device = StalkerDeviceIdentity("00:11:22:33:44:55"),
+ username = "bob",
+ password = "secret2"
+ ),
+ JellyfinConfig("https://j.test", "carol", "token")
+ )
+
+ cases.forEach { configuration ->
+ val row = configEntity(configuration, providerId = 5L, generation = 3L)
+ val identity = providerEntity(id = 5L, name = "Provider", type = configuration.type)
+ val runtime = com.streamvault.domain.model.ProviderAccountRuntime(
+ maxConnections = 2,
+ catalogLayout = CatalogLayout.UNIFIED_VOD,
+ catalogLayoutDetectionVersion = 4
+ )
+ val expected = ProviderSnapshot(
+ provider = StableProvider(
+ id = identity.id,
+ name = identity.name,
+ type = identity.type,
+ isActive = identity.isActive,
+ status = identity.status,
+ lastSyncedAt = identity.lastSyncedAt,
+ createdAt = identity.createdAt
+ ),
+ configuration = configuration,
+ configurationGeneration = row.configurationGeneration,
+ accountRuntime = runtime
+ ).toLegacyProvider().redactedCredentials()
+ val actual = defaultPublicProjection().assemble(
+ identity = identity,
+ configuration = defaultPublicProjection().decode(row),
+ runtime = ProviderAccountRuntimeEntity(
+ providerId = identity.id,
+ maxConnections = runtime.maxConnections,
+ allowedOutputFormatsJson = "[]",
+ catalogLayout = runtime.catalogLayout,
+ catalogLayoutDetectionVersion = runtime.catalogLayoutDetectionVersion
+ ),
+ portalState = null
+ )
+
+ assertThat(actual).isEqualTo(expected)
+ }
+ }
+
+ @Test
+ fun `missing configuration returns the redacted provider identity`() {
+ val identity = providerEntity(id = 2L, name = "Legacy", type = ProviderType.M3U)
+
+ assertThat(defaultPublicProjection().assemble(identity, null, null, null))
+ .isEqualTo(identity.toPublicDomain())
+ }
+
+ private fun defaultPublicProjection() = DefaultProviderPublicProjection(codec, gson)
+
+ private fun configEntity(
+ configuration: ProviderConfiguration,
+ providerId: Long = 9L,
+ generation: Long = 7L
+ ) = ProviderConfigEntity(
+ providerId = providerId,
+ type = configuration.type,
+ schemaVersion = configuration.schemaVersion,
+ configurationGeneration = generation,
+ identityKey = codec.identityKey(configuration),
+ encryptedConfigJson = codec.encode(configuration),
+ updatedAt = 1L
+ )
+
+ private fun providerEntity(
+ id: Long,
+ name: String,
+ type: ProviderType
+ ) = ProviderEntity(
+ id = id,
+ name = name,
+ type = type,
+ isActive = false,
+ status = ProviderStatus.ACTIVE,
+ lastSyncedAt = 11L,
+ createdAt = 12L
+ )
+
+ private class CountingCredentialCrypto : CredentialCrypto {
+ val decryptCalls = AtomicInteger()
+
+ override fun encryptIfNeeded(value: String): String = value
+
+ override fun decryptIfNeeded(value: String): String {
+ decryptCalls.incrementAndGet()
+ return value
+ }
+ }
+}