-
-
Notifications
You must be signed in to change notification settings - Fork 736
feat: Implementing encrypted local storage for user sessions #1191
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.parse; | ||
|
||
import android.content.Context; | ||
|
||
import androidx.security.crypto.EncryptedFile; | ||
import androidx.security.crypto.MasterKey; | ||
|
||
import com.parse.boltsinternal.Task; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.security.GeneralSecurityException; | ||
import java.util.concurrent.Callable; | ||
|
||
public 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(); | ||
} | ||
|
||
private void saveToDisk(ParseObject current) throws IOException, GeneralSecurityException { | ||
JSONObject json = coder.encode(current.getState(), null, PointerEncoder.get()); | ||
ParseFileUtils.writeJSONObjectToFile(encryptedFile, json); | ||
} | ||
|
||
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 | JSONException | IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
}, 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 (IOException | GeneralSecurityException e) { | ||
throw new RuntimeException(e); | ||
} | ||
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()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.parse; | ||
|
||
import com.parse.boltsinternal.Continuation; | ||
import com.parse.boltsinternal.Task; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ParseObjectStoreMigrator<T extends ParseObject> implements ParseObjectStore<T> { | ||
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. You could add some Javadoc with comments. Does this migrator needs to be here forever or just for about some migration period? 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.
Thank you guys for creating this awesome platform, I actuallty just copied the Since Robolectric doesn't mock Android's I am actually in a learning process, so please don't hesitate to edit or comment on anything in the code. 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 think the migrator should be available for old users until they migrate, or remove it on a major release. |
||
|
||
private ParseObjectStore<T> store; | ||
private ParseObjectStore<T> legacy; | ||
|
||
public ParseObjectStoreMigrator(ParseObjectStore<T> store, ParseObjectStore<T> legacy) { | ||
this.store = store; | ||
this.legacy = legacy; | ||
} | ||
|
||
private static <T extends ParseObject> Task<T> migrate( | ||
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. Is there a way to check whenever migrate is needed? 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. If 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. Thanks for pointing that out, that is actually a bug which is going to be calling |
||
final ParseObjectStore<T> from, final ParseObjectStore<T> to) { | ||
return from.getAsync() | ||
.onSuccessTask( | ||
task -> { | ||
final T object = task.getResult(); | ||
if (object == null) { | ||
return task; | ||
} | ||
|
||
return Task.whenAll( | ||
Arrays.asList(from.deleteAsync(), to.setAsync(object))) | ||
.continueWith(task1 -> object); | ||
}); | ||
} | ||
|
||
@Override | ||
public Task<T> getAsync() { | ||
return store.getAsync().continueWithTask(new Continuation<T, Task<T>>() { | ||
@Override | ||
public Task<T> then(Task<T> task) throws Exception { | ||
if (task.getResult() != null) return task; | ||
return migrate(legacy, store); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public Task<Void> setAsync(T object) { | ||
return store.setAsync(object); | ||
} | ||
|
||
@Override | ||
public Task<Boolean> existsAsync() { | ||
return store.existsAsync().continueWithTask(new Continuation<Boolean, Task<Boolean>>() { | ||
@Override | ||
public Task<Boolean> then(Task<Boolean> task) throws Exception { | ||
if (task.getResult()) return Task.forResult(true); | ||
return legacy.existsAsync(); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public Task<Void> deleteAsync() { | ||
Task<Void> storeTask = store.deleteAsync(); | ||
return Task.whenAll(Arrays.asList(legacy.deleteAsync(), storeTask)).continueWithTask(new Continuation<Void, Task<Void>>() { | ||
@Override | ||
public Task<Void> then(Task<Void> task1) throws Exception { | ||
return storeTask; | ||
} | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.parse | ||
|
||
/* | ||
* Copyright 2020 Appmattus Limited | ||
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. We need to make sure to consider the license implications. This repository is planned to be migrated to Apache 2, we should do that before merging this, otherwise we need to add this as a second license. |
||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import java.security.spec.AlgorithmParameterSpec | ||
|
||
internal val AlgorithmParameterSpec.keystoreAlias: String | ||
get() = this::class.java.getDeclaredMethod("getKeystoreAlias").invoke(this) as String |
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.
Can you add Javadoc on the public classes and their public methods you have introduced?