update names for overlay in native code

This commit is contained in:
Adam Velebil 2024-09-09 13:21:56 +02:00
parent dc8822d54d
commit e5e61648cf
No known key found for this signature in database
GPG Key ID: C9B1E4A3CBBD2E10
9 changed files with 53 additions and 58 deletions

View File

@ -396,7 +396,7 @@ class MainActivity : FlutterFragmentActivity() {
private var contextManager: AppContextManager? = null
private lateinit var deviceManager: DeviceManager
private lateinit var appContext: AppContext
private lateinit var dialogManager: DialogManager
private lateinit var nfcOverlayManager: NfcOverlayManager
private lateinit var appPreferences: AppPreferences
private lateinit var flutterLog: FlutterLog
private lateinit var flutterStreams: List<Closeable>
@ -411,8 +411,8 @@ class MainActivity : FlutterFragmentActivity() {
messenger = flutterEngine.dartExecutor.binaryMessenger
flutterLog = FlutterLog(messenger)
appMethodChannel = AppMethodChannel(messenger)
dialogManager = DialogManager(messenger, this.lifecycleScope)
deviceManager = DeviceManager(this, viewModel,appMethodChannel, dialogManager)
nfcOverlayManager = NfcOverlayManager(messenger, this.lifecycleScope)
deviceManager = DeviceManager(this, viewModel,appMethodChannel, nfcOverlayManager)
appContext = AppContext(messenger, this.lifecycleScope, viewModel)
appPreferences = AppPreferences(this)
@ -463,7 +463,7 @@ class MainActivity : FlutterFragmentActivity() {
messenger,
deviceManager,
oathViewModel,
dialogManager,
nfcOverlayManager,
appPreferences
)
@ -473,7 +473,7 @@ class MainActivity : FlutterFragmentActivity() {
this,
deviceManager,
appMethodChannel,
dialogManager,
nfcOverlayManager,
fidoViewModel,
viewModel
)

View File

@ -23,35 +23,35 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
typealias OnDialogCancelled = suspend () -> Unit
typealias OnCancelled = suspend () -> Unit
class DialogManager(messenger: BinaryMessenger, private val coroutineScope: CoroutineScope) {
class NfcOverlayManager(messenger: BinaryMessenger, private val coroutineScope: CoroutineScope) {
private val channel =
MethodChannel(messenger, "com.yubico.authenticator.channel.dialog")
MethodChannel(messenger, "com.yubico.authenticator.channel.nfc_overlay")
private var onCancelled: OnDialogCancelled? = null
private var onCancelled: OnCancelled? = null
init {
channel.setHandler(coroutineScope) { method, _ ->
when (method) {
"cancel" -> dialogClosed()
"cancel" -> onClosed()
else -> throw NotImplementedError()
}
}
}
fun showDialog(cancelled: OnDialogCancelled?) {
fun show(cancelled: OnCancelled?) {
onCancelled = cancelled
coroutineScope.launch {
channel.invoke("show", null)
}
}
suspend fun closeDialog() {
suspend fun close() {
channel.invoke("close", NULL)
}
private suspend fun dialogClosed(): String {
private suspend fun onClosed(): String {
onCancelled?.let {
onCancelled = null
withContext(Dispatchers.Main) {

View File

@ -21,9 +21,9 @@ import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.yubico.authenticator.ContextDisposedException
import com.yubico.authenticator.DialogManager
import com.yubico.authenticator.MainActivity
import com.yubico.authenticator.MainViewModel
import com.yubico.authenticator.NfcOverlayManager
import com.yubico.authenticator.OperationContext
import com.yubico.authenticator.yubikit.NfcState
import com.yubico.yubikit.android.transport.usb.UsbYubiKeyDevice
@ -48,7 +48,7 @@ class DeviceManager(
private val lifecycleOwner: LifecycleOwner,
private val appViewModel: MainViewModel,
private val appMethodChannel: MainActivity.AppMethodChannel,
private val dialogManager: DialogManager
private val nfcOverlayManager: NfcOverlayManager
) {
var clearDeviceInfoOnDisconnect: Boolean = true
@ -189,24 +189,42 @@ class DeviceManager(
suspend fun <T> withKey(
onUsb: suspend (UsbYubiKeyDevice) -> T,
onNfc: suspend () -> com.yubico.yubikit.core.util.Result<T, Throwable>,
onDialogCancelled: () -> Unit,
onCancelled: () -> Unit,
retryOnNfcFailure: Boolean
): T =
appViewModel.connectedYubiKey.value?.let {
onUsb(it)
} ?: if (retryOnNfcFailure == true) {
onNfcWithRetries(onNfc, onDialogCancelled)
onNfcWithRetries(onNfc, onCancelled)
} else {
onNfc(onNfc, onDialogCancelled)
onNfc(onNfc, onCancelled)
}
private suspend fun <T> onNfc(
onNfc: suspend () -> com.yubico.yubikit.core.util.Result<T, Throwable>,
onCancelled: () -> Unit
): T {
nfcOverlayManager.show {
logger.debug("NFC action was cancelled")
onCancelled.invoke()
}
try {
return onNfc.invoke().value
} catch (e: Exception) {
appMethodChannel.nfcStateChanged(NfcState.FAILURE)
throw e
}
}
private suspend fun <T> onNfcWithRetries(
onNfc: suspend () -> com.yubico.yubikit.core.util.Result<T, Throwable>,
onDialogCancelled: () -> Unit) : T {
onCancelled: () -> Unit
): T {
dialogManager.showDialog {
logger.debug("Cancelled dialog")
onDialogCancelled.invoke()
nfcOverlayManager.show {
logger.debug("NFC action with retries was cancelled")
onCancelled.invoke()
}
while (true) {
@ -230,21 +248,4 @@ class DeviceManager(
}
}
}
private suspend fun <T> onNfc(
onNfc: suspend () -> com.yubico.yubikit.core.util.Result<T, Throwable>,
onDialogCancelled: () -> Unit) : T {
dialogManager.showDialog {
onDialogCancelled.invoke()
}
try {
return onNfc.invoke().value
} catch (e: Exception) {
appMethodChannel.nfcStateChanged(NfcState.FAILURE)
throw e
}
}
}

View File

@ -17,7 +17,6 @@
package com.yubico.authenticator.fido
import com.yubico.authenticator.device.DeviceManager
import com.yubico.authenticator.device.Info
import com.yubico.authenticator.fido.data.YubiKitFidoSession
import com.yubico.authenticator.yubikit.DeviceInfoHelper.Companion.getDeviceInfo
import com.yubico.authenticator.yubikit.withConnection
@ -25,11 +24,9 @@ import com.yubico.yubikit.android.transport.usb.UsbYubiKeyDevice
import com.yubico.yubikit.core.fido.FidoConnection
import com.yubico.yubikit.core.util.Result
import org.slf4j.LoggerFactory
import java.util.Timer
import java.util.TimerTask
import kotlin.coroutines.cancellation.CancellationException
import kotlin.coroutines.suspendCoroutine
import kotlin.concurrent.schedule
class FidoConnectionHelper(private val deviceManager: DeviceManager) {
private var pendingAction: FidoAction? = null
@ -59,7 +56,7 @@ class FidoConnectionHelper(private val deviceManager: DeviceManager) {
return deviceManager.withKey(
onUsb = { useSessionUsb(it, updateDeviceInfo, block) },
onNfc = { useSessionNfc(block) },
onDialogCancelled = {
onCancelled = {
pendingAction?.invoke(Result.failure(CancellationException()))
pendingAction = null
},

View File

@ -18,7 +18,7 @@ package com.yubico.authenticator.fido
import androidx.lifecycle.LifecycleOwner
import com.yubico.authenticator.AppContextManager
import com.yubico.authenticator.DialogManager
import com.yubico.authenticator.NfcOverlayManager
import com.yubico.authenticator.MainActivity
import com.yubico.authenticator.MainViewModel
import com.yubico.authenticator.NULL
@ -72,7 +72,7 @@ class FidoManager(
lifecycleOwner: LifecycleOwner,
private val deviceManager: DeviceManager,
private val appMethodChannel: MainActivity.AppMethodChannel,
private val dialogManager: DialogManager,
private val nfcOverlayManager: NfcOverlayManager,
private val fidoViewModel: FidoViewModel,
mainViewModel: MainViewModel
) : AppContextManager(), DeviceListener {
@ -120,7 +120,7 @@ class FidoManager(
lifecycleOwner,
deviceManager,
appMethodChannel,
dialogManager,
nfcOverlayManager,
fidoViewModel,
mainViewModel,
connectionHelper,

View File

@ -18,7 +18,7 @@ package com.yubico.authenticator.fido
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.yubico.authenticator.DialogManager
import com.yubico.authenticator.NfcOverlayManager
import com.yubico.authenticator.MainActivity
import com.yubico.authenticator.MainViewModel
import com.yubico.authenticator.NULL
@ -71,7 +71,7 @@ class FidoResetHelper(
private val lifecycleOwner: LifecycleOwner,
private val deviceManager: DeviceManager,
private val appMethodChannel: MainActivity.AppMethodChannel,
private val dialogManager: DialogManager,
private val nfcOverlayManager: NfcOverlayManager,
private val fidoViewModel: FidoViewModel,
private val mainViewModel: MainViewModel,
private val connectionHelper: FidoConnectionHelper,
@ -214,7 +214,7 @@ class FidoResetHelper(
private suspend fun resetOverNfc() = suspendCoroutine { continuation ->
coroutineScope.launch {
dialogManager.showDialog {
nfcOverlayManager.show {
}
fidoViewModel.updateResetState(FidoResetState.Touch)

View File

@ -36,7 +36,7 @@ class ManagementConnectionHelper(
deviceManager.withKey(
onUsb = { useSessionUsb(it, block) },
onNfc = { useSessionNfc(block) },
onDialogCancelled = {
onCancelled = {
action?.invoke(Result.failure(CancellationException()))
action = null
},

View File

@ -26,7 +26,6 @@ import com.yubico.authenticator.*
import com.yubico.authenticator.device.Capabilities
import com.yubico.authenticator.device.DeviceListener
import com.yubico.authenticator.device.DeviceManager
import com.yubico.authenticator.device.Info
import com.yubico.authenticator.device.UnknownDevice
import com.yubico.authenticator.oath.data.Code
import com.yubico.authenticator.oath.data.CodeType
@ -64,12 +63,10 @@ import kotlinx.serialization.encodeToString
import org.slf4j.LoggerFactory
import java.io.IOException
import java.net.URI
import java.util.Timer
import java.util.TimerTask
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.coroutines.suspendCoroutine
import kotlin.concurrent.schedule
typealias OathAction = (Result<YubiKitOathSession, Exception>) -> Unit
@ -78,7 +75,7 @@ class OathManager(
messenger: BinaryMessenger,
private val deviceManager: DeviceManager,
private val oathViewModel: OathViewModel,
private val dialogManager: DialogManager,
private val nfcOverlayManager: NfcOverlayManager,
private val appPreferences: AppPreferences
) : AppContextManager(), DeviceListener {
@ -118,10 +115,10 @@ class OathManager(
// cancel any pending actions, except for addToAny
if (!addToAny) {
pendingAction?.let {
logger.debug("Cancelling pending action/closing nfc dialog.")
logger.debug("Cancelling pending action/closing nfc overlay.")
it.invoke(Result.failure(CancellationException()))
coroutineScope.launch {
dialogManager.closeDialog()
nfcOverlayManager.close()
}
pendingAction = null
}
@ -695,7 +692,7 @@ class OathManager(
return deviceManager.withKey(
onUsb = { useSessionUsb(it, updateDeviceInfo, block) },
onNfc = { useSessionNfc(block) },
onDialogCancelled = {
onCancelled = {
pendingAction?.invoke(Result.failure(CancellationException()))
pendingAction = null
},

View File

@ -29,7 +29,7 @@ import 'views/nfc_overlay_icons.dart';
import 'views/nfc_overlay_widget.dart';
final _log = Logger('android.tap_request_dialog');
const _channel = MethodChannel('com.yubico.authenticator.channel.dialog');
const _channel = MethodChannel('com.yubico.authenticator.channel.nfc_overlay');
final nfcOverlay =
NotifierProvider<_NfcOverlayNotifier, int>(_NfcOverlayNotifier.new);