Skip to content

Commit 29d3f09

Browse files
committed
[MOB-9235] Always encrypting
1 parent 9db02a0 commit 29d3f09

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

iterableapi/src/main/java/com/iterable/iterableapi/IterableKeychain.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class IterableKeychain {
1616
private var sharedPrefs: SharedPreferences
1717
private val encryptor: IterableDataEncryptor
1818

19-
private val emailKey = "iterable-email"
20-
private val userIdKey = "iterable-user-id"
21-
private val authTokenKey = "iterable-auth-token"
19+
internal val emailKey = "iterable-email"
20+
internal val userIdKey = "iterable-user-id"
21+
internal val authTokenKey = "iterable-auth-token"
2222

2323
constructor(context: Context) {
2424
sharedPrefs = context.getSharedPreferences(
@@ -28,11 +28,16 @@ class IterableKeychain {
2828
encryptor = IterableDataEncryptor()
2929
IterableLogger.v(TAG, "SharedPreferences being used with encryption")
3030

31-
// Attempt migration from encrypted preferences
32-
IterableKeychainEncryptedDataMigrator(context, sharedPrefs, this).attemptMigration()
31+
try {
32+
// Attempt migration from encrypted preferences
33+
IterableKeychainEncryptedDataMigrator(context, sharedPrefs, this).attemptMigration()
34+
} catch (e: IterableKeychainEncryptedDataMigrator.MigrationException) {
35+
IterableLogger.w(TAG, "Migration failed, clearing data", e)
36+
handleDecryptionError()
37+
}
3338
}
3439

35-
fun handleDecryptionError() {
40+
private fun handleDecryptionError() {
3641
IterableLogger.w(TAG, "Decryption failed, clearing all data and regenerating key")
3742
sharedPrefs.edit()
3843
.remove(emailKey)

iterableapi/src/main/java/com/iterable/iterableapi/IterableKeychainEncryptedDataMigrator.kt

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,31 @@ class IterableKeychainEncryptedDataMigrator(
1414
private val TAG = "IterableKeychainMigrator"
1515

1616
private val encryptedSharedPrefsFileName = "iterable-encrypted-shared-preferences"
17-
private val migrationAttemptedKey = "iterable-encrypted-migration-attempted"
1817
private val migrationStartedKey = "iterable-encrypted-migration-started"
1918
private val migrationCompletedKey = "iterable-encrypted-migration-completed"
2019

21-
private val emailKey = "iterable-email"
22-
private val userIdKey = "iterable-user-id"
23-
private val authTokenKey = "iterable-auth-token"
20+
class MigrationException(message: String, cause: Throwable? = null) : Exception(message, cause)
2421

2522
fun attemptMigration() {
26-
// Skip if migration was already attempted and completed
23+
// Skip if migration was already completed
2724
if (sharedPrefs.getBoolean(migrationCompletedKey, false)) {
2825
IterableLogger.v(TAG, "Migration was already completed, skipping")
2926
return
3027
}
3128

32-
// Check if migration was started but not completed (potential crash during migration)
33-
if (sharedPrefs.getBoolean(migrationStartedKey, false)) {
34-
IterableLogger.w(TAG, "Previous migration attempt was interrupted, clearing data")
35-
keychain.handleDecryptionError()
36-
return
37-
}
38-
3929
// Only attempt migration on Android M and above
4030
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
4131
markMigrationCompleted()
4232
return
4333
}
4434

35+
// If previous migration was interrupted, mark as completed and throw exception
36+
if (sharedPrefs.getBoolean(migrationStartedKey, false)) {
37+
IterableLogger.w(TAG, "Previous migration attempt was interrupted")
38+
markMigrationCompleted()
39+
throw MigrationException("Previous migration attempt was interrupted")
40+
}
41+
4542
// Mark migration as started
4643
sharedPrefs.edit()
4744
.putBoolean(migrationStartedKey, true)
@@ -73,17 +70,18 @@ class IterableKeychainEncryptedDataMigrator(
7370

7471
IterableLogger.v(TAG, "Successfully migrated data from encrypted preferences")
7572
} catch (e: Throwable) {
76-
IterableLogger.w(TAG, "Failed to access encrypted preferences, skipping migration", e)
77-
markMigrationCompleted() // Mark as completed even on failure to prevent retries
73+
IterableLogger.w(TAG, "Failed to access encrypted preferences", e)
74+
markMigrationCompleted() // Mark as completed even on failure
75+
throw MigrationException("Failed to migrate data", e)
7876
}
7977
}.start()
8078
}
8179

8280
private fun migrateData(encryptedPrefs: SharedPreferences) {
8381
// Use keychain methods to ensure proper encryption
84-
encryptedPrefs.getString(emailKey, null)?.let { keychain.saveEmail(it) }
85-
encryptedPrefs.getString(userIdKey, null)?.let { keychain.saveUserId(it) }
86-
encryptedPrefs.getString(authTokenKey, null)?.let { keychain.saveAuthToken(it) }
82+
encryptedPrefs.getString(keychain.emailKey, null)?.let { keychain.saveEmail(it) }
83+
encryptedPrefs.getString(keychain.userIdKey, null)?.let { keychain.saveUserId(it) }
84+
encryptedPrefs.getString(keychain.authTokenKey, null)?.let { keychain.saveAuthToken(it) }
8785
}
8886

8987
private fun markMigrationCompleted() {

0 commit comments

Comments
 (0)