@@ -26,7 +26,8 @@ class IterableKeychain {
26
26
encryptionEnabled = false
27
27
sharedPrefs = context.getSharedPreferences(
28
28
IterableConstants .SHARED_PREFS_FILE ,
29
- Context .MODE_PRIVATE )
29
+ Context .MODE_PRIVATE
30
+ )
30
31
IterableLogger .v(TAG , " SharedPreferences being used" )
31
32
} else {
32
33
// See if EncryptedSharedPreferences can be created successfully
@@ -39,45 +40,42 @@ class IterableKeychain {
39
40
encryptedSharedPrefsFileName,
40
41
masterKeyAlias,
41
42
EncryptedSharedPreferences .PrefKeyEncryptionScheme .AES256_SIV ,
42
- EncryptedSharedPreferences .PrefValueEncryptionScheme .AES256_GCM )
43
+ EncryptedSharedPreferences .PrefValueEncryptionScheme .AES256_GCM
44
+ )
43
45
encryptionEnabled = true
44
- } catch (e: Exception ) {
46
+ } catch (e: Throwable ) {
47
+ if (e is Error ) {
48
+ IterableLogger .e(
49
+ TAG ,
50
+ " EncryptionSharedPreference creation failed with Error. Attempting to continue"
51
+ )
52
+ }
53
+
45
54
if (encryptionEnforced) {
46
- // TODO: In memory Or similar solution needs to be implemented in future.
47
- IterableLogger .e(TAG , " Error creating EncryptedSharedPreferences" , e)
55
+ // TODO: In-memory or similar solution needs to be implemented in the future.
56
+ IterableLogger .w(
57
+ TAG ,
58
+ " Encryption is enforced. PII will not be persisted due to EncryptionSharedPreference failure. Email/UserId and Auth token will have to be passed for every app session." ,
59
+ e
60
+ )
48
61
throw e.fillInStackTrace()
49
62
} else {
50
63
sharedPrefs = context.getSharedPreferences(
51
64
IterableConstants .SHARED_PREFS_FILE ,
52
- Context .MODE_PRIVATE )
65
+ Context .MODE_PRIVATE
66
+ )
67
+ IterableLogger .w(
68
+ TAG ,
69
+ " Using SharedPreference as EncryptionSharedPreference creation failed."
70
+ )
53
71
encryptionEnabled = false
54
72
}
55
73
}
56
74
57
75
// Try to migrate data from SharedPreferences to EncryptedSharedPreferences
58
- if (encryptionEnabled) {
76
+ if (encryptionEnabled) {
59
77
migrateAuthDataFromSharedPrefsToKeychain(context)
60
78
}
61
-
62
- }
63
- val masterKeyAlias = MasterKey .Builder (context)
64
- .setKeyScheme(MasterKey .KeyScheme .AES256_GCM )
65
- .build()
66
- sharedPrefs = try {
67
- EncryptedSharedPreferences .create(
68
- context,
69
- encryptedSharedPrefsFileName,
70
- masterKeyAlias,
71
- EncryptedSharedPreferences .PrefKeyEncryptionScheme .AES256_SIV ,
72
- EncryptedSharedPreferences .PrefValueEncryptionScheme .AES256_GCM
73
- )
74
- } catch (e: Exception ) {
75
- if (encryptionEnforced) {
76
- IterableLogger .e(TAG , " Error creating EncryptedSharedPreferences" , e)
77
- throw e.fillInStackTrace()
78
- } else {
79
- context.getSharedPreferences(encryptedSharedPrefsFileName, Context .MODE_PRIVATE )
80
- }
81
79
}
82
80
}
83
81
@@ -113,17 +111,20 @@ class IterableKeychain {
113
111
114
112
@RequiresApi(api = Build .VERSION_CODES .M )
115
113
private fun migrateAuthDataFromSharedPrefsToKeychain (context : Context ) {
116
- val oldPrefs: SharedPreferences = context.getSharedPreferences(
114
+ val oldPrefs: SharedPreferences = context.getSharedPreferences(
117
115
IterableConstants .SHARED_PREFS_FILE ,
118
- Context .MODE_PRIVATE )
116
+ Context .MODE_PRIVATE
117
+ )
119
118
val sharedPrefsEmail = oldPrefs.getString(IterableConstants .SHARED_PREFS_EMAIL_KEY , null )
120
119
val sharedPrefsUserId = oldPrefs.getString(IterableConstants .SHARED_PREFS_USERID_KEY , null )
121
- val sharedPrefsAuthToken = oldPrefs.getString(IterableConstants .SHARED_PREFS_AUTH_TOKEN_KEY , null )
120
+ val sharedPrefsAuthToken =
121
+ oldPrefs.getString(IterableConstants .SHARED_PREFS_AUTH_TOKEN_KEY , null )
122
122
val editor: SharedPreferences .Editor = oldPrefs.edit()
123
123
if (getEmail() == null && sharedPrefsEmail != null ) {
124
124
saveEmail(sharedPrefsEmail)
125
125
editor.remove(IterableConstants .SHARED_PREFS_EMAIL_KEY )
126
- IterableLogger .v(TAG ,
126
+ IterableLogger .v(
127
+ TAG ,
127
128
" UPDATED: migrated email from SharedPreferences to IterableKeychain"
128
129
)
129
130
} else if (sharedPrefsEmail != null ) {
@@ -132,7 +133,8 @@ class IterableKeychain {
132
133
if (getUserId() == null && sharedPrefsUserId != null ) {
133
134
saveUserId(sharedPrefsUserId)
134
135
editor.remove(IterableConstants .SHARED_PREFS_USERID_KEY )
135
- IterableLogger .v(TAG ,
136
+ IterableLogger .v(
137
+ TAG ,
136
138
" UPDATED: migrated userId from SharedPreferences to IterableKeychain"
137
139
)
138
140
} else if (sharedPrefsUserId != null ) {
@@ -141,7 +143,8 @@ class IterableKeychain {
141
143
if (getAuthToken() == null && sharedPrefsAuthToken != null ) {
142
144
saveAuthToken(sharedPrefsAuthToken)
143
145
editor.remove(IterableConstants .SHARED_PREFS_AUTH_TOKEN_KEY )
144
- IterableLogger .v(TAG ,
146
+ IterableLogger .v(
147
+ TAG ,
145
148
" UPDATED: migrated authToken from SharedPreferences to IterableKeychain"
146
149
)
147
150
} else if (sharedPrefsAuthToken != null ) {
0 commit comments