-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MOB-9235] Use shared prefs and try to migrate date from encrypted to…
… shared prefs
- Loading branch information
1 parent
8fdec83
commit 284fe2e
Showing
2 changed files
with
88 additions
and
100 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
79 changes: 79 additions & 0 deletions
79
iterableapi/src/main/java/com/iterable/iterableapi/IterableKeychainEncryptedDataMigrator.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,79 @@ | ||
package com.iterable.iterableapi | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import android.os.Build | ||
import androidx.security.crypto.EncryptedSharedPreferences | ||
import androidx.security.crypto.MasterKey | ||
|
||
class IterableKeychainEncryptedDataMigrator( | ||
private val context: Context, | ||
private val sharedPrefs: SharedPreferences | ||
) { | ||
private val TAG = "IterableKeychainMigrator" | ||
|
||
private val encryptedSharedPrefsFileName = "iterable-encrypted-shared-preferences" | ||
private val migrationAttemptedKey = "iterable-encrypted-migration-attempted" | ||
|
||
private val emailKey = "iterable-email" | ||
private val userIdKey = "iterable-user-id" | ||
private val authTokenKey = "iterable-auth-token" | ||
|
||
fun attemptMigration() { | ||
// Skip if migration was already attempted | ||
if (sharedPrefs.getBoolean(migrationAttemptedKey, false)) { | ||
IterableLogger.v(TAG, "Migration was already attempted, skipping") | ||
return | ||
} | ||
|
||
// Mark that we attempted migration | ||
sharedPrefs.edit() | ||
.putBoolean(migrationAttemptedKey, true) | ||
.apply() | ||
|
||
// Only attempt migration on Android M and above | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { | ||
return | ||
} | ||
|
||
// Run migration in background thread | ||
Thread { | ||
try { | ||
val masterKeyAlias = MasterKey.Builder(context) | ||
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM) | ||
.build() | ||
|
||
val encryptedPrefs = EncryptedSharedPreferences.create( | ||
context, | ||
encryptedSharedPrefsFileName, | ||
masterKeyAlias, | ||
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, | ||
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM | ||
) | ||
|
||
// Migrate data | ||
migrateData(encryptedPrefs) | ||
|
||
// Clear encrypted prefs after successful migration | ||
encryptedPrefs.edit().clear().apply() | ||
|
||
IterableLogger.v(TAG, "Successfully migrated data from encrypted preferences") | ||
} catch (e: Throwable) { | ||
IterableLogger.w(TAG, "Failed to access encrypted preferences, skipping migration", e) | ||
} | ||
}.start() | ||
} | ||
|
||
private fun migrateData(encryptedPrefs: SharedPreferences) { | ||
val email = encryptedPrefs.getString(emailKey, null) | ||
val userId = encryptedPrefs.getString(userIdKey, null) | ||
val authToken = encryptedPrefs.getString(authTokenKey, null) | ||
|
||
// Only migrate non-null values | ||
sharedPrefs.edit().apply { | ||
email?.let { putString(emailKey, it) } | ||
userId?.let { putString(userIdKey, it) } | ||
authToken?.let { putString(authTokenKey, it) } | ||
}.apply() | ||
} | ||
} |