From 329e9e88ae86f62d840c49f001932d61ce551b31 Mon Sep 17 00:00:00 2001 From: Itai Hanski Date: Thu, 10 Oct 2024 14:56:53 +0300 Subject: [PATCH] Fix android encryption (#25) --- android/gradle.properties | 1 + .../DescopeReactNativeModule.kt | 28 +++++++++++++++---- example/ios/Podfile.lock | 4 +-- package.json | 2 +- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/android/gradle.properties b/android/gradle.properties index e5b287d..45d3c26 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -3,3 +3,4 @@ DescopeReactNative_minSdkVersion=24 DescopeReactNative_targetSdkVersion=33 DescopeReactNative_compileSdkVersion=33 DescopeReactNative_ndkversion=21.4.7075529 +android.useAndroidX=true diff --git a/android/src/main/java/com/descopereactnative/DescopeReactNativeModule.kt b/android/src/main/java/com/descopereactnative/DescopeReactNativeModule.kt index 95f8587..b67cfd9 100644 --- a/android/src/main/java/com/descopereactnative/DescopeReactNativeModule.kt +++ b/android/src/main/java/com/descopereactnative/DescopeReactNativeModule.kt @@ -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 @@ -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) } @@ -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, @@ -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 + } + } +} diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 7877596..3fbc527 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -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) @@ -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 diff --git a/package.json b/package.json index bc54822..2e14d4a 100644 --- a/package.json +++ b/package.json @@ -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",