-
-
Notifications
You must be signed in to change notification settings - Fork 735
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: Implementing encrypted local storage for user sessions with tests #1211
Open
rommansabbir
wants to merge
11
commits into
parse-community:master
Choose a base branch
from
rommansabbir:master
base: master
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.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
19a252a
Bugfix: fixes #1155 - supporting concurrent download of the same Pars…
techyourchance d2135c2
Update parse/src/test/java/com/parse/ParseFileControllerTest.java
mtrezza d5c50f4
Update parse/src/test/java/com/parse/ParseFileControllerTest.java
mtrezza 8d8a00d
ci failing : fixed spotless violations
rommansabbir c19946b
Revert "ci failing : fixed spotless violations"
rommansabbir ec87160
fix: ci failing on PR #1179
rommansabbir 4653c28
Merge branch 'parse-community:master' into master
rommansabbir 4d0494a
feat: Implementing encrypted local storage for user sessions with tests
rommansabbir 6d208d7
fix : spotless apply issue fixed, using a new version.
rommansabbir 6887cfe
refactoring : removed duplicate code
rommansabbir eb7142a
Merge branch 'master' into master
mtrezza 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
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
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
135 changes: 135 additions & 0 deletions
135
parse/src/main/java/com/parse/EncryptedFileObjectStore.java
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,135 @@ | ||
package com.parse; | ||
|
||
import android.content.Context; | ||
import androidx.security.crypto.EncryptedFile; | ||
import androidx.security.crypto.MasterKey; | ||
import com.parse.boltsinternal.Task; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.security.GeneralSecurityException; | ||
import java.util.concurrent.Callable; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
/** | ||
* a file based {@link ParseObjectStore} using Jetpack's {@link EncryptedFile} class to protect | ||
* files from a malicious copy. | ||
*/ | ||
class EncryptedFileObjectStore<T extends ParseObject> implements ParseObjectStore<T> { | ||
|
||
private final String className; | ||
private final File file; | ||
private final EncryptedFile encryptedFile; | ||
private final ParseObjectCurrentCoder coder; | ||
|
||
public EncryptedFileObjectStore(Class<T> clazz, File file, ParseObjectCurrentCoder coder) { | ||
this(getSubclassingController().getClassName(clazz), file, coder); | ||
} | ||
|
||
public EncryptedFileObjectStore(String className, File file, ParseObjectCurrentCoder coder) { | ||
this.className = className; | ||
this.file = file; | ||
this.coder = coder; | ||
Context context = ParsePlugins.get().applicationContext(); | ||
try { | ||
encryptedFile = | ||
new EncryptedFile.Builder( | ||
context, | ||
file, | ||
new MasterKey.Builder(context) | ||
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM) | ||
.build(), | ||
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB) | ||
.build(); | ||
} catch (GeneralSecurityException | IOException e) { | ||
throw new RuntimeException(e.getMessage()); | ||
} | ||
} | ||
|
||
private static ParseObjectSubclassingController getSubclassingController() { | ||
return ParseCorePlugins.getInstance().getSubclassingController(); | ||
} | ||
|
||
/** | ||
* Saves the {@code ParseObject} to the a file on disk as JSON in /2/ format. | ||
* | ||
* @param current ParseObject which needs to be saved to disk. | ||
* @throws IOException thrown if an error occurred during writing of the file | ||
* @throws GeneralSecurityException thrown if there is an error with encryption keys or during | ||
* the encryption of the file | ||
*/ | ||
private void saveToDisk(ParseObject current) throws IOException, GeneralSecurityException { | ||
JSONObject json = coder.encode(current.getState(), null, PointerEncoder.get()); | ||
ParseFileUtils.writeJSONObjectToFile(encryptedFile, json); | ||
} | ||
|
||
/** | ||
* Retrieves a {@code ParseObject} from a file on disk in /2/ format. | ||
* | ||
* @return The {@code ParseObject} that was retrieved. If the file wasn't found, or the contents | ||
* of the file is an invalid {@code ParseObject}, returns {@code null}. | ||
* @throws GeneralSecurityException thrown if there is an error with encryption keys or during | ||
* the encryption of the file | ||
* @throws JSONException thrown if an error occurred during the decoding process of the | ||
* ParseObject to a JSONObject | ||
* @throws IOException thrown if an error occurred during writing of the file | ||
*/ | ||
private T getFromDisk() throws GeneralSecurityException, JSONException, IOException { | ||
return ParseObject.from( | ||
coder.decode( | ||
ParseObject.State.newBuilder(className), | ||
ParseFileUtils.readFileToJSONObject(encryptedFile), | ||
ParseDecoder.get()) | ||
.isComplete(true) | ||
.build()); | ||
} | ||
|
||
@Override | ||
public Task<T> getAsync() { | ||
return Task.call( | ||
new Callable<T>() { | ||
@Override | ||
public T call() throws Exception { | ||
if (!file.exists()) return null; | ||
try { | ||
return getFromDisk(); | ||
} catch (GeneralSecurityException e) { | ||
throw new RuntimeException(e.getMessage()); | ||
} | ||
} | ||
}, | ||
ParseExecutors.io()); | ||
} | ||
|
||
@Override | ||
public Task<Void> setAsync(T object) { | ||
return Task.call( | ||
() -> { | ||
if (file.exists() && !ParseFileUtils.deleteQuietly(file)) | ||
throw new RuntimeException("Unable to delete"); | ||
try { | ||
saveToDisk(object); | ||
} catch (GeneralSecurityException e) { | ||
throw new RuntimeException(e.getMessage()); | ||
} | ||
return null; | ||
}, | ||
ParseExecutors.io()); | ||
} | ||
|
||
@Override | ||
public Task<Boolean> existsAsync() { | ||
return Task.call(file::exists, ParseExecutors.io()); | ||
} | ||
|
||
@Override | ||
public Task<Void> deleteAsync() { | ||
return Task.call( | ||
() -> { | ||
if (file.exists() && !ParseFileUtils.deleteQuietly(file)) | ||
throw new RuntimeException("Unable to delete"); | ||
return null; | ||
}, | ||
ParseExecutors.io()); | ||
} | ||
} |
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
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
Oops, something went wrong.
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.
Please revert these unrelated changes in bolts, twitter and wherever you find these comment-only refactors.
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.
Okay.