-
Notifications
You must be signed in to change notification settings - Fork 404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add reset after failure, result.error after failure #865
Open
juliansteenbakker
wants to merge
1
commit into
develop
Choose a base branch
from
feat/reset-after-exception
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+53
−40
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,51 +31,45 @@ public class FlutterSecureStorage { | |
|
||
private String sharedPreferencesName = DEFAULT_PREF_NAME; | ||
private String preferencesKeyPrefix = DEFAULT_KEY_PREFIX; | ||
private SharedPreferences encryptedPreferences; | ||
private final SharedPreferences encryptedPreferences; | ||
|
||
public FlutterSecureStorage(Context context, Map<String, Object> options) { | ||
public FlutterSecureStorage(Context context, Map<String, Object> options) throws GeneralSecurityException, IOException { | ||
this.applicationContext = context.getApplicationContext(); | ||
this.options = options; | ||
ensureOptions(); | ||
getEncryptedSharedPreferences(); | ||
encryptedPreferences = getEncryptedSharedPreferences(); | ||
} | ||
|
||
public boolean containsKey(String key) { | ||
SharedPreferences preferences = getEncryptedSharedPreferences(); | ||
return preferences != null && preferences.contains(addPrefixToKey(key)); | ||
return encryptedPreferences != null && encryptedPreferences.contains(addPrefixToKey(key)); | ||
} | ||
|
||
public String read(String key) { | ||
SharedPreferences preferences = getEncryptedSharedPreferences(); | ||
return preferences != null ? preferences.getString(addPrefixToKey(key), null) : null; | ||
return encryptedPreferences != null ? encryptedPreferences.getString(addPrefixToKey(key), null) : null; | ||
} | ||
|
||
public void write(String key, String value) { | ||
SharedPreferences preferences = getEncryptedSharedPreferences(); | ||
if (preferences != null) { | ||
preferences.edit().putString(addPrefixToKey(key), value).apply(); | ||
if (encryptedPreferences != null) { | ||
encryptedPreferences.edit().putString(addPrefixToKey(key), value).apply(); | ||
} | ||
} | ||
|
||
public void delete(String key) { | ||
SharedPreferences preferences = getEncryptedSharedPreferences(); | ||
if (preferences != null) { | ||
preferences.edit().remove(addPrefixToKey(key)).apply(); | ||
if (encryptedPreferences != null) { | ||
encryptedPreferences.edit().remove(addPrefixToKey(key)).apply(); | ||
} | ||
} | ||
|
||
public void deleteAll() { | ||
SharedPreferences preferences = getEncryptedSharedPreferences(); | ||
if (preferences != null) { | ||
preferences.edit().clear().apply(); | ||
if (encryptedPreferences != null) { | ||
encryptedPreferences.edit().clear().apply(); | ||
} | ||
} | ||
|
||
public Map<String, String> readAll() { | ||
SharedPreferences preferences = getEncryptedSharedPreferences(); | ||
Map<String, String> result = new HashMap<>(); | ||
if (preferences != null) { | ||
Map<String, ?> allEntries = preferences.getAll(); | ||
if (encryptedPreferences != null) { | ||
Map<String, ?> allEntries = encryptedPreferences.getAll(); | ||
for (Map.Entry<String, ?> entry : allEntries.entrySet()) { | ||
String key = entry.getKey(); | ||
Object value = entry.getValue(); | ||
|
@@ -102,18 +96,21 @@ private void ensureOptions() { | |
: DEFAULT_KEY_PREFIX; | ||
} | ||
|
||
private SharedPreferences getEncryptedSharedPreferences() { | ||
if (encryptedPreferences == null) { | ||
private SharedPreferences getEncryptedSharedPreferences() throws GeneralSecurityException, IOException { | ||
try { | ||
final SharedPreferences encryptedPreferences = initializeEncryptedSharedPreferencesManager(applicationContext); | ||
migrateToEncryptedPreferences(encryptedPreferences); | ||
return encryptedPreferences; | ||
} catch (Exception e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am a bit unsure, if we should reset the storage on any exception; maybe, we should only reset it on exceptions where we know that the reset is absolutely necessary? (I think, |
||
Log.w(TAG, "EncryptedSharedPreferences initialization failed, resetting storage", e); | ||
applicationContext.getSharedPreferences(sharedPreferencesName, Context.MODE_PRIVATE).edit().clear().apply(); | ||
try { | ||
encryptedPreferences = initializeEncryptedSharedPreferencesManager(applicationContext); | ||
migrateToEncryptedPreferences(encryptedPreferences); | ||
} catch (Exception e) { | ||
Log.e(TAG, "EncryptedSharedPreferences initialization failed", e); | ||
return initializeEncryptedSharedPreferencesManager(applicationContext); | ||
} catch (Exception f) { | ||
Log.e(TAG, "EncryptedSharedPreferences initialization after reset failed", e); | ||
throw f; | ||
} | ||
} else { | ||
return encryptedPreferences; | ||
} | ||
return null; | ||
} | ||
|
||
private SharedPreferences initializeEncryptedSharedPreferencesManager(Context context) throws GeneralSecurityException, IOException { | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand correctly, the constructor of FlutterSecureStorage should throw an error, if the initialization of
encryptedPreferences
fails. I believe, that this means, thatencryptedPreferences
cannot be null (seeEncryptedSharedPreferences.create
).If so, we could/should maybe add the
@NonNull
annotation for encryptedPreferences here (and we can probably remove most null checks in class methods).