Skip to content

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

fix(provider): decode migrated configs without embedded type

David Nashashf3b5b0c15816
diff --git a/data/src/main/java/com/streamvault/data/provider/ProviderConfigurationCodec.kt b/data/src/main/java/com/streamvault/data/provider/ProviderConfigurationCodec.kt
index 22aab6f4..d272db28 100644
--- a/data/src/main/java/com/streamvault/data/provider/ProviderConfigurationCodec.kt
+++ b/data/src/main/java/com/streamvault/data/provider/ProviderConfigurationCodec.kt
@@ -19,18 +19,11 @@ class ProviderConfigurationCodec @Inject constructor(
private val gson: Gson,
private val credentialCrypto: CredentialCrypto
) {
- fun encode(configuration: ProviderConfiguration): String = gson.toJson(configuration.encryptSecrets())
+ fun encode(configuration: ProviderConfiguration): String =
+ encodeProviderConfiguration(gson, credentialCrypto, configuration)
- fun decode(type: ProviderType, payload: String): ProviderConfiguration {
- val encrypted = when (type) {
- ProviderType.XTREAM_CODES -> gson.fromJson(payload, XtreamConfig::class.java)
- ProviderType.M3U -> gson.fromJson(payload, M3uConfig::class.java)
- ProviderType.STALKER_PORTAL -> gson.fromJson(payload, StalkerConfig::class.java)
- ProviderType.JELLYFIN -> gson.fromJson(payload, JellyfinConfig::class.java)
- } ?: throw IllegalArgumentException("Provider configuration payload is empty")
- require(encrypted.type == type) { "Configuration payload does not match stored provider type" }
- return encrypted.decryptSecrets()
- }
+ fun decode(type: ProviderType, payload: String): ProviderConfiguration =
+ decodeProviderConfigurationCompat(gson, credentialCrypto, type, payload)
fun identityKey(configuration: ProviderConfiguration): String {
val canonical = when (configuration) {
@@ -49,20 +42,6 @@ class ProviderConfigurationCodec @Inject constructor(
.joinToString("") { "%02x".format(it) }
}
- private fun ProviderConfiguration.encryptSecrets(): ProviderConfiguration = when (this) {
- is XtreamConfig -> copy(password = credentialCrypto.encryptIfNeeded(password))
- is M3uConfig -> this
- is StalkerConfig -> copy(password = credentialCrypto.encryptIfNeeded(password))
- is JellyfinConfig -> copy(credential = credentialCrypto.encryptIfNeeded(credential))
- }
-
- private fun ProviderConfiguration.decryptSecrets(): ProviderConfiguration = when (this) {
- is XtreamConfig -> copy(password = credentialCrypto.decryptIfNeeded(password))
- is M3uConfig -> this
- is StalkerConfig -> copy(password = credentialCrypto.decryptIfNeeded(password))
- is JellyfinConfig -> copy(credential = credentialCrypto.decryptIfNeeded(credential))
- }
-
private fun normalizeOrigin(value: String): String = runCatching {
val uri = URI(value.trim())
val scheme = uri.scheme?.lowercase().orEmpty()
diff --git a/data/src/main/java/com/streamvault/data/provider/ProviderConfigurationMigrationCompat.kt b/data/src/main/java/com/streamvault/data/provider/ProviderConfigurationMigrationCompat.kt
new file mode 100644
index 00000000..a5fbe1ed
--- /dev/null
+++ b/data/src/main/java/com/streamvault/data/provider/ProviderConfigurationMigrationCompat.kt
@@ -0,0 +1,59 @@
+package com.streamvault.data.provider
+
+import com.google.gson.Gson
+import com.streamvault.data.security.CredentialCrypto
+import com.streamvault.domain.model.JellyfinConfig
+import com.streamvault.domain.model.M3uConfig
+import com.streamvault.domain.model.ProviderConfiguration
+import com.streamvault.domain.model.ProviderType
+import com.streamvault.domain.model.StalkerConfig
+import com.streamvault.domain.model.XtreamConfig
+
+internal fun encodeProviderConfiguration(
+ gson: Gson,
+ credentialCrypto: CredentialCrypto,
+ configuration: ProviderConfiguration
+): String {
+ val encrypted = when (configuration) {
+ is XtreamConfig -> configuration.copy(password = credentialCrypto.encryptIfNeeded(configuration.password))
+ is M3uConfig -> configuration
+ is StalkerConfig -> configuration.copy(password = credentialCrypto.encryptIfNeeded(configuration.password))
+ is JellyfinConfig -> configuration.copy(credential = credentialCrypto.encryptIfNeeded(configuration.credential))
+ }
+ return gson.toJson(encrypted)
+}
+
+internal fun decodeProviderConfigurationCompat(
+ gson: Gson,
+ credentialCrypto: CredentialCrypto,
+ type: ProviderType,
+ payload: String
+): ProviderConfiguration {
+ val payloadHasType = gson.fromJson(payload, Map::class.java)?.containsKey("type") == true
+ val decoded = when (type) {
+ ProviderType.XTREAM_CODES -> gson.fromJson(payload, XtreamConfig::class.java)
+ ProviderType.M3U -> gson.fromJson(payload, M3uConfig::class.java)
+ ProviderType.STALKER_PORTAL -> gson.fromJson(payload, StalkerConfig::class.java)
+ ProviderType.JELLYFIN -> gson.fromJson(payload, JellyfinConfig::class.java)
+ } ?: throw IllegalArgumentException("Provider configuration payload is empty")
+
+ // The 1.0.17 typed-provider backfill stored provider_configs.type separately but omitted
+ // the matching JSON field. Gson may instantiate these Kotlin classes without running
+ // body-property initializers, leaving decoded.type unset. Reconstruct only those legacy
+ // payloads; an explicit mismatching JSON type must still fail below.
+ val normalized = if (payloadHasType) decoded else when (decoded) {
+ is XtreamConfig -> decoded.copy()
+ is M3uConfig -> decoded.copy()
+ is StalkerConfig -> decoded.copy()
+ is JellyfinConfig -> decoded.copy()
+ }
+
+ require(normalized.type == type) { "Configuration payload does not match stored provider type" }
+
+ return when (normalized) {
+ is XtreamConfig -> normalized.copy(password = credentialCrypto.decryptIfNeeded(normalized.password))
+ is M3uConfig -> normalized
+ is StalkerConfig -> normalized.copy(password = credentialCrypto.decryptIfNeeded(normalized.password))
+ is JellyfinConfig -> normalized.copy(credential = credentialCrypto.decryptIfNeeded(normalized.credential))
+ }
+}
diff --git a/data/src/test/java/com/streamvault/data/provider/ProviderConfigurationCodecMigrationTest.kt b/data/src/test/java/com/streamvault/data/provider/ProviderConfigurationCodecMigrationTest.kt
new file mode 100644
index 00000000..b0dd05b4
--- /dev/null
+++ b/data/src/test/java/com/streamvault/data/provider/ProviderConfigurationCodecMigrationTest.kt
@@ -0,0 +1,41 @@
+package com.streamvault.data.provider
+
+import com.google.common.truth.Truth.assertThat
+import com.google.gson.Gson
+import com.streamvault.data.security.CredentialCrypto
+import com.streamvault.domain.model.JellyfinConfig
+import com.streamvault.domain.model.M3uConfig
+import com.streamvault.domain.model.ProviderConfiguration
+import com.streamvault.domain.model.StalkerConfig
+import com.streamvault.domain.model.StalkerDeviceIdentity
+import com.streamvault.domain.model.XtreamConfig
+import org.junit.Test
+
+class ProviderConfigurationCodecMigrationTest {
+ private val gson = Gson()
+ private val crypto = object : CredentialCrypto {
+ override fun encryptIfNeeded(value: String) = value
+ override fun decryptIfNeeded(value: String) = value
+ }
+ private val codec = ProviderConfigurationCodec(gson, crypto)
+
+ @Test
+ fun `decode accepts migration payloads without embedded type`() {
+ val configurations = listOf<ProviderConfiguration>(
+ XtreamConfig("https://x.test", "alice", "secret"),
+ M3uConfig("https://m.test/list.m3u"),
+ StalkerConfig(
+ portalUrl = "https://s.test",
+ device = StalkerDeviceIdentity("00:11:22:33:44:55"),
+ username = "bob",
+ password = "secret2"
+ ),
+ JellyfinConfig("https://j.test", "carol", "token")
+ )
+
+ configurations.forEach { configuration ->
+ val migratedPayload = gson.toJsonTree(configuration).asJsonObject.apply { remove("type") }.toString()
+ assertThat(codec.decode(configuration.type, migratedPayload)).isEqualTo(configuration)
+ }
+ }
+}
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index c088e95b..054092bb 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -18,6 +18,7 @@ All notable product changes are recorded in this document.
- Improved device orientation behavior by allowing portrait rotation on phones and tablets while keeping Android TV and Fire TV interfaces locked to landscape.
- Improved grouped VOD deduplication performance by moving movie and series presentation work off the main thread, adding bounded normalization caches, and avoiding unnecessary ICU normalization for ASCII titles.
- Fixed grouped VOD browse pagination and infinite scroll by using cursor windows, deduplicating before offset/limit application, and handling zero-timestamp catalog entries consistently.
+- Fixed provider configuration decoding after typed-provider migrations that omitted the embedded configuration type, preserving access to existing provider settings and credentials.
## [1.0.17]