From 75392ccbab3f4f57e97f8d63832e5c0c7734f74c Mon Sep 17 00:00:00 2001 From: Roxk Date: Thu, 5 Sep 2024 18:40:53 +0800 Subject: [PATCH] tool: Add MigrateResult ref dev-1953 --- flutter-example/lib/main.dart | 5 ++- .../xamarinflutter/MigratetoolPlugin.kt | 36 ++++++++++++++++++- migratetool/lib/migratetool.dart | 29 +++++++++++++-- 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/flutter-example/lib/main.dart b/flutter-example/lib/main.dart index 642e46c..96c9c85 100644 --- a/flutter-example/lib/main.dart +++ b/flutter-example/lib/main.dart @@ -842,7 +842,10 @@ class _MyAppState extends State { Future _onPressConfigure() async { final packageName = (await PackageInfo.fromPlatform()).packageName; try { - await migrate(packageName); + final result = await migrate(packageName); + if (kDebugMode) { + print(result); + } } catch (e) { if (kDebugMode) { print(e); diff --git a/migratetool/android/src/main/kotlin/com/authgear/sdk/migrateplugin/xamarinflutter/MigratetoolPlugin.kt b/migratetool/android/src/main/kotlin/com/authgear/sdk/migrateplugin/xamarinflutter/MigratetoolPlugin.kt index 51e85c0..d8ecf9b 100644 --- a/migratetool/android/src/main/kotlin/com/authgear/sdk/migrateplugin/xamarinflutter/MigratetoolPlugin.kt +++ b/migratetool/android/src/main/kotlin/com/authgear/sdk/migrateplugin/xamarinflutter/MigratetoolPlugin.kt @@ -194,6 +194,14 @@ class MigratetoolPlugin: FlutterPlugin, MethodCallHandler { } else { result.success(xamarinStorage.hasXamarinData(packageName, containerName)) } + } else if (call.method == "hasFlutterData") { + val packageName = call.argument("packageName") + val containerName = call.argument("containerName") + if (packageName == null || containerName == null) { + result.error("INVALID_ARGUMENT", "Expected {packageName, containerName}", null); + } else { + result.success(hasFlutterData(packageName, containerName)) + } } else if (call.method == "migrate") { val packageName = call.argument("packageName") val containerName = call.argument("containerName") @@ -221,7 +229,6 @@ class MigratetoolPlugin: FlutterPlugin, MethodCallHandler { val anonymousId = xamarinStorage.getValue(keyStore, packageName, containerName, keyAnonymousId) val biometricKeyId = xamarinStorage.getValue(keyStore, packageName, containerName, keyBiometricKeyId) val keyMaker = KeyMaker() - val prefs = getSharedPreferences() if (refreshToken != null) { storageSetItem(keyMaker.keyRefreshToken(containerName), refreshToken) } @@ -234,6 +241,14 @@ class MigratetoolPlugin: FlutterPlugin, MethodCallHandler { return false } + fun hasFlutterData(packageName: String, containerName: String): Boolean { + val keyMaker = KeyMaker() + val hasRefreshToken = storageGetItem(keyMaker.keyRefreshToken(containerName)) != null + val hasAnonymousKeyId = storageGetItem(keyMaker.keyAnonymousKeyId(containerName)) != null + val hasBiometricKeyId = storageGetItem(keyMaker.keyBiometricKeyId(containerName)) != null + return hasRefreshToken || hasAnonymousKeyId || hasBiometricKeyId + } + // Direct copy of https://github.com/authgear/authgear-sdk-flutter/blob/main/android/src/main/kotlin/com/authgear/flutter/AuthgearPlugin.kt#L469C3-L486C4 private fun storageSetItem(key: String, value: String) { try { @@ -252,6 +267,25 @@ class MigratetoolPlugin: FlutterPlugin, MethodCallHandler { } } + private fun storageGetItem(key: String): String? { + try { + val sharedPreferences = this.getSharedPreferences() + val value = sharedPreferences.getString(key, null) + return value + } catch (e: Exception) { + // NOTE(backup): Please search NOTE(backup) to understand what is going on here. + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + if (e is GeneralSecurityException) { + Log.w(TAG, "try to recover from backup problem in storageGetItem", e) + val context = pluginBinding?.applicationContext!! + deleteSharedPreferences(context, ENCRYPTED_SHARED_PREFERENCES_NAME) + return storageGetItem(key) + } + } + throw e + } + } + // Direct copy of https://github.com/authgear/authgear-sdk-flutter/blob/main/android/src/main/kotlin/com/authgear/flutter/AuthgearPlugin.kt#L437 private fun getSharedPreferences(): SharedPreferences { val context = pluginBinding?.applicationContext!! diff --git a/migratetool/lib/migratetool.dart b/migratetool/lib/migratetool.dart index 871ba5b..43a3b1e 100644 --- a/migratetool/lib/migratetool.dart +++ b/migratetool/lib/migratetool.dart @@ -6,13 +6,36 @@ import 'exception.dart'; const _channel = MethodChannel("authgear_sdk_tool_migrate_xamarin_flutter"); -Future migrate(String packageName, +enum MigrateResult { + /// No xamarin data available to migrate, so no migration needed + noMigrationNeeded, + + /// Migration already done previously, so would not migrate + wouldNotMigrate, + + /// Migrated this time + migrated +} + +Future migrate(String packageName, [String containerName = "default"]) async { + if (await _hasFlutterData(packageName, containerName)) { + return MigrateResult.wouldNotMigrate; + } if (await _hasXamarinData(packageName, containerName)) { _migrate(packageName, containerName); - return true; + return MigrateResult.migrated; + } + return MigrateResult.noMigrationNeeded; +} + +Future _hasFlutterData(String packageName, String containerName) async { + try { + return await _channel.invokeMethod("hasFlutterData", + {"packageName": packageName, "containerName": containerName}); + } on PlatformException catch (e) { + throw AuthgearException(e); } - return false; } Future _hasXamarinData(String packageName, String containerName) async {