Skip to content

Commit

Permalink
tool: Add MigrateResult ref dev-1953
Browse files Browse the repository at this point in the history
  • Loading branch information
roxk committed Sep 5, 2024
1 parent 097b17e commit 75392cc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
5 changes: 4 additions & 1 deletion flutter-example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,10 @@ class _MyAppState extends State<MyApp> {
Future<void> _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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ class MigratetoolPlugin: FlutterPlugin, MethodCallHandler {
} else {
result.success(xamarinStorage.hasXamarinData(packageName, containerName))
}
} else if (call.method == "hasFlutterData") {
val packageName = call.argument<String>("packageName")
val containerName = call.argument<String>("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<String>("packageName")
val containerName = call.argument<String>("containerName")
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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!!
Expand Down
29 changes: 26 additions & 3 deletions migratetool/lib/migratetool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,36 @@ import 'exception.dart';

const _channel = MethodChannel("authgear_sdk_tool_migrate_xamarin_flutter");

Future<bool> 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<MigrateResult> 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<bool> _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<bool> _hasXamarinData(String packageName, String containerName) async {
Expand Down

0 comments on commit 75392cc

Please sign in to comment.