Skip to content

Commit

Permalink
FR-16800 - Add support for encrypted shared preference
Browse files Browse the repository at this point in the history
  • Loading branch information
frontegg-david committed Jun 27, 2024
1 parent 131fcfe commit a5a2aee
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
5 changes: 3 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ dependencies {
implementation 'androidx.core:core-ktx:[1.7,)'
implementation 'io.reactivex.rxjava3:rxkotlin:3.0.1'
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
implementation 'com.google.code.gson:gson:2.8.9'
implementation 'com.google.code.gson:gson:2.10'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"
implementation "androidx.browser:browser:1.7.0"
implementation "androidx.browser:browser:1.8.0"
implementation 'androidx.security:security-crypto:1.0.0'
implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.5.0'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import com.frontegg.android.exceptions.KeyNotFoundException
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKeys
import com.frontegg.android.utils.CredentialKeys

open class CredentialManager(context: Context) {
Expand All @@ -16,16 +17,27 @@ open class CredentialManager(context: Context) {

private val sp: SharedPreferences;


init {
sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE)
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
sp = EncryptedSharedPreferences.create(
SHARED_PREFERENCES_NAME,
masterKeyAlias,
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
}

/**
* Save value for key into the shared preference
*/
fun save(key: CredentialKeys, value: String): Boolean {
Log.d(TAG, "Saving Frontegg $key in shared preference")
return sp.edit().putString(key.toString(), value).commit()
with(sp.edit()) {
putString(key.toString(), value)
return commit()
}
}


Expand All @@ -34,7 +46,9 @@ open class CredentialManager(context: Context) {
*/
fun get(key: CredentialKeys): String? {
Log.d(TAG, "get Frontegg $key in shared preference ")
return sp.getString(key.toString(), null)
with(sp) {
return getString(key.toString(), null)
}
}


Expand All @@ -46,9 +60,14 @@ open class CredentialManager(context: Context) {
Log.d(TAG, "clear Frontegg shared preference ")

val selectedRegion: String? = getSelectedRegion()
sp.edit().clear().commit()
if (selectedRegion != null) {
sp.edit().putString(CredentialKeys.SELECTED_REGION.toString(), selectedRegion).commit()

with(sp.edit()) {
clear()
commit()
if (selectedRegion != null) {
putString(CredentialKeys.SELECTED_REGION.toString(), selectedRegion)
commit()
}
}
}

Expand Down

0 comments on commit a5a2aee

Please sign in to comment.