Skip to content

Commit

Permalink
Fix android encryption (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
itaihanski authored Oct 10, 2024
1 parent 883a40f commit 329e9e8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ DescopeReactNative_minSdkVersion=24
DescopeReactNative_targetSdkVersion=33
DescopeReactNative_compileSdkVersion=33
DescopeReactNative_ndkversion=21.4.7075529
android.useAndroidX=true
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import com.facebook.react.bridge.ReactMethod
import java.security.MessageDigest
import kotlin.random.Random

private const val prefName = "com.descope.reactnative"

class DescopeReactNativeModule(private val reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {

private val storage: EncryptedStorage by lazy { EncryptedStorage(reactContext) }
private val storage: EncryptedStorage? by lazy { createEncryptedStore(reactContext) }

override fun getName(): String {
return NAME
Expand Down Expand Up @@ -84,19 +86,19 @@ class DescopeReactNativeModule(private val reactContext: ReactApplicationContext

@ReactMethod
fun loadItem(key: String, promise: Promise) {
val value = storage.loadItem(key)
val value = storage?.loadItem(key)
promise.resolve(value)
}

@ReactMethod
fun saveItem(key: String, value: String, promise: Promise) {
storage.saveItem(key, value)
storage?.saveItem(key, value)
promise.resolve(key)
}

@ReactMethod
fun removeItem(key: String, promise: Promise) {
storage.removeItem(key)
storage?.removeItem(key)
promise.resolve(key)
}

Expand All @@ -118,7 +120,7 @@ private fun launchUri(context: Context, uri: Uri) {
private class EncryptedStorage(context: Context) {
private val masterKey = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
private val sharedPreferences = EncryptedSharedPreferences.create(
"com.descope.reactnative",
prefName,
masterKey,
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
Expand All @@ -135,3 +137,19 @@ private class EncryptedStorage(context: Context) {
.remove(key)
.apply()
}

private fun createEncryptedStore(context: Context): EncryptedStorage? {
return try {
EncryptedStorage(context)
} catch (e: Exception) {
try {
// encrypted storage key unusable - deleting and recreating
// see google issue https://issuetracker.google.com/issues/164901843
context.deleteSharedPreferences(prefName)
EncryptedStorage(context)
} catch (e: Exception) {
// unable to initialize encrypted storage
null
}
}
}
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PODS:
- boost (1.76.0)
- CocoaAsyncSocket (7.6.5)
- descope-react-native (0.7.0):
- descope-react-native (0.7.1):
- RCT-Folly (= 2021.07.22.00)
- React-Core
- DoubleConversion (1.1.6)
Expand Down Expand Up @@ -662,7 +662,7 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
boost: 57d2868c099736d80fcd648bf211b4431e51a558
CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99
descope-react-native: 0bff69a27fc7d3eb71bdecedb91f95ce5676dc7b
descope-react-native: c088c2aba28881dc9fdfe9ca0e6c5846d90a0b69
DoubleConversion: 5189b271737e1565bdce30deb4a08d647e3f5f54
FBLazyVector: 5d4a3b7f411219a45a6d952f77d2c0a6c9989da5
FBReactNativeSpec: 3fc2d478e1c4b08276f9dd9128f80ec6d5d85c1f
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@descope/react-native-sdk",
"version": "0.7.0",
"version": "0.7.1",
"description": "The Descope SDK for React-Native",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down

0 comments on commit 329e9e8

Please sign in to comment.