Skip to content
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(android): use EncryptedSharedPreferences #13848

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -120,6 +120,13 @@ public void setString(String key, String value)
}
}

@Kroll.setProperty
public void useEncryption(boolean value)
{
TiProperties.useEncryption = value;
appProperties.getEditor();
}

@Override
public String getApiName()
{
Expand Down
1 change: 1 addition & 0 deletions android/titanium/build.gradle
Expand Up @@ -262,6 +262,7 @@ dependencies {
implementation 'androidx.vectordrawable:vectordrawable-animated:1.1.0'
implementation 'androidx.viewpager:viewpager:1.0.0'
implementation 'androidx.annotation:annotation:1.5.0'
implementation 'androidx.security:security-crypto:1.1.0-alpha06'

// Google's "Material Components" themed UI library.
implementation "com.google.android.material:material:${project.ext.tiMaterialLibVersion}"
Expand Down
Expand Up @@ -16,6 +16,9 @@
import android.content.Context;
import android.content.SharedPreferences;

import androidx.security.crypto.EncryptedSharedPreferences;
import androidx.security.crypto.MasterKey;

/**
* API for accessing, storing, and modifying application properties that are
* exposed via Ti.App.Properties.
Expand All @@ -24,8 +27,10 @@ public class TiProperties
{
private static final String TAG = "TiProperties";
private static JSONObject systemProperties;
public static boolean useEncryption = false;

SharedPreferences preferences;
SharedPreferences preferencesPlain;

/**
* Instantiates the private SharedPreferences collection with the given name and context.
Expand All @@ -36,7 +41,8 @@ public class TiProperties
*/
public TiProperties(Context context, String name, boolean clear)
{
preferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
preferencesPlain = context.getSharedPreferences(name, Context.MODE_PRIVATE);
preferences = preferencesPlain;
if (clear) {
preferences.edit().clear().apply();
}
Expand All @@ -53,7 +59,6 @@ public String getString(String key, String def)
if (Log.isDebugModeEnabled()) {
Log.d(TAG, "getString called with key:" + key + ", def:" + def);
}

Object value = getPreference(key);
if (value != null) {
return value.toString();
Expand All @@ -65,7 +70,7 @@ public String getString(String key, String def)
public Object getPreference(String key)
{
Object value = null;
if (systemProperties != null) {
if (!useEncryption && systemProperties != null) {
try {
value = systemProperties.get(key);
} catch (JSONException e) {
Expand All @@ -78,6 +83,32 @@ public Object getPreference(String key)
return value;
}

public SharedPreferences.Editor getEditor()
{
if (useEncryption) {
try {
MasterKey masterKey = new MasterKey.Builder(TiApplication.getAppCurrentActivity())
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build();

SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(
TiApplication.getAppCurrentActivity(),
"secret_shared_prefs",
masterKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
);
preferences = sharedPreferences;
return sharedPreferences.edit();
} catch (Exception ex) {
Log.e(TAG, "Error: " + ex.getMessage());
}
} else {
preferences = preferencesPlain;
}
return preferences.edit();
}

/**
* Maps the specified key with a String value. If value is null, existing key will be removed from preferences.
* Otherwise, its value will be overwritten.
Expand All @@ -97,7 +128,7 @@ public void setString(String key, String value)
return;
}

SharedPreferences.Editor editor = preferences.edit();
SharedPreferences.Editor editor = getEditor();
if (value == null) {
editor.remove(key);
} else {
Expand Down Expand Up @@ -158,7 +189,7 @@ public void setInt(String key, int value)
return;
}

SharedPreferences.Editor editor = preferences.edit();
SharedPreferences.Editor editor = getEditor();
editor.putInt(key, value);
editor.apply();
}
Expand Down Expand Up @@ -206,7 +237,7 @@ public void setDouble(String key, double value)
return;
}

SharedPreferences.Editor editor = preferences.edit();
SharedPreferences.Editor editor = getEditor();
editor.putString(key, value + "");
editor.apply();
}
Expand Down Expand Up @@ -264,7 +295,7 @@ public void setBool(String key, boolean value)
return;
}

SharedPreferences.Editor editor = preferences.edit();
SharedPreferences.Editor editor = getEditor();
editor.putBoolean(key, value);
editor.apply();
}
Expand Down Expand Up @@ -305,7 +336,7 @@ public void setList(String key, String[] value)
Log.d(TAG, "setList called with key:" + key + ", value:" + value);
}

SharedPreferences.Editor editor = preferences.edit();
SharedPreferences.Editor editor = getEditor();
for (int i = 0; i < value.length; i++) {
editor.putString(key + "." + i, value[i]);
}
Expand Down Expand Up @@ -375,7 +406,7 @@ public void removeProperty(String key)
}

if (preferences.contains(key)) {
SharedPreferences.Editor editor = preferences.edit();
SharedPreferences.Editor editor = getEditor();
editor.remove(key);
editor.apply();
}
Expand Down