-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create LegacyCredentialsMigrator to help loading old credentials vers…
…ions We've changed the Credentials type twice, so there are two legacy formats of that type that we possibly encounter when loading data from EncryptedSharedPreferences. This class and the legacy types takes care of trying and deserializing them in a clean way, so we don't have to pollute our DefaultTokenStore with the necessary logic. This also means reintroducing the kotlinx.datetime dependency - however, this won't lead to any issues with 3rd party apps, and our own one is protected since it's using desugaring.
- Loading branch information
Showing
8 changed files
with
145 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 0 additions & 31 deletions
31
auth/src/main/kotlin/com/tidal/sdk/auth/storage/LegacyCredentials.kt
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
auth/src/main/kotlin/com/tidal/sdk/auth/storage/LegacyTokens.kt
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
auth/src/main/kotlin/com/tidal/sdk/auth/storage/legacycredentials/LegacyCredentials.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.tidal.sdk.auth.storage.legacycredentials | ||
|
||
import com.tidal.sdk.auth.model.Credentials | ||
import kotlinx.datetime.Instant | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
sealed class LegacyCredentials { | ||
abstract fun toCredentials(): Credentials | ||
} | ||
|
||
/** | ||
* Represents the credentials of a user or client. | ||
*/ | ||
@Deprecated("Use [Credentials] instead.") | ||
@Serializable | ||
data class LegacyCredentialsV1( | ||
val clientId: String, | ||
val requestedScopes: Scopes, | ||
val clientUniqueKey: String?, | ||
val grantedScopes: Scopes, | ||
val userId: String?, | ||
val expires: Instant?, | ||
val token: String?, | ||
) : LegacyCredentials() { | ||
override fun toCredentials(): Credentials = Credentials( | ||
clientId, | ||
requestedScopes.scopes, | ||
clientUniqueKey, | ||
grantedScopes.scopes, | ||
userId, | ||
expires?.epochSeconds, | ||
token, | ||
) | ||
} | ||
|
||
/** | ||
* Represents the credentials of a user or client. | ||
*/ | ||
@Deprecated("Use [Credentials] instead.") | ||
@Serializable | ||
data class LegacyCredentialsV2( | ||
val clientId: String, | ||
val requestedScopes: Set<String>, | ||
val clientUniqueKey: String?, | ||
val grantedScopes: Set<String>, | ||
val userId: String?, | ||
val expires: Instant?, | ||
val token: String?, | ||
) : LegacyCredentials() { | ||
override fun toCredentials(): Credentials = Credentials( | ||
clientId, | ||
requestedScopes, | ||
clientUniqueKey, | ||
grantedScopes, | ||
userId, | ||
expires?.epochSeconds, | ||
token, | ||
) | ||
} |
40 changes: 40 additions & 0 deletions
40
...src/main/kotlin/com/tidal/sdk/auth/storage/legacycredentials/LegacyCredentialsMigrator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.tidal.sdk.auth.storage.legacycredentials | ||
|
||
import com.tidal.sdk.auth.model.Tokens | ||
import com.tidal.sdk.common.e | ||
import com.tidal.sdk.common.i | ||
import com.tidal.sdk.common.logger | ||
import kotlinx.serialization.json.Json | ||
|
||
internal class LegacyCredentialsMigrator { | ||
|
||
private inline fun <reified T : LegacyTokens> decodeJsonString(jsonString: String): Tokens = | ||
Json.decodeFromString<T>( | ||
jsonString, | ||
).toTokens() | ||
|
||
@Suppress("TooGenericExceptionCaught") | ||
fun migrateCredentials(jsonString: String): Tokens { | ||
// if ever necessary, add further legacy type operations here | ||
val operations = listOf( | ||
{ decodeJsonString<TokensV1>(jsonString) }, | ||
{ decodeJsonString<TokensV2>(jsonString) }, | ||
) | ||
logger.i { "Attempting to decode using legacy types." } | ||
val exceptions = mutableListOf<Exception>() | ||
for (operation in operations) { | ||
try { | ||
return operation().also { | ||
println("Successfully decoded using legacy types.") | ||
} | ||
} catch (e: Exception) { | ||
logger.i { "Failed to decode using legacy types." } | ||
println("Failed to decode using legacy types.") | ||
exceptions.plus(e) | ||
} | ||
} | ||
logger.e { "Failed to decode using legacy types! Exceptions caught:" } | ||
exceptions.forEach { logger.e { it } } | ||
throw exceptions.first() | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
auth/src/main/kotlin/com/tidal/sdk/auth/storage/legacycredentials/LegacyTokens.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.tidal.sdk.auth.storage.legacycredentials | ||
|
||
import com.tidal.sdk.auth.model.Tokens | ||
import kotlinx.serialization.Serializable | ||
|
||
internal sealed class LegacyTokens { | ||
abstract fun toTokens(): Tokens | ||
} | ||
|
||
@Serializable | ||
internal data class TokensV1( | ||
val credentials: LegacyCredentialsV1, | ||
val refreshToken: String? = null, | ||
) : LegacyTokens() { | ||
override fun toTokens(): Tokens = Tokens( | ||
credentials.toCredentials(), | ||
refreshToken, | ||
) | ||
} | ||
|
||
@Serializable | ||
internal data class TokensV2( | ||
val credentials: LegacyCredentialsV2, | ||
val refreshToken: String? = null, | ||
) : LegacyTokens() { | ||
override fun toTokens(): Tokens = Tokens( | ||
credentials.toCredentials(), | ||
refreshToken, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters