@@ -62,6 +62,7 @@ import io.flutter.plugin.common.MethodChannel
62
62
import kotlinx.coroutines.*
63
63
import kotlinx.serialization.encodeToString
64
64
import org.slf4j.LoggerFactory
65
+ import java.io.IOException
65
66
import java.net.URI
66
67
import java.util.concurrent.Executors
67
68
import java.util.concurrent.atomic.AtomicBoolean
@@ -554,29 +555,47 @@ class OathManager(
554
555
NULL
555
556
}
556
557
557
- private suspend fun requestRefresh () =
558
+ private suspend fun requestRefresh () {
559
+
560
+ val clearCodes = {
561
+ val currentCredentials = oathViewModel.credentials.value
562
+ oathViewModel.updateCredentials(currentCredentials?.associate {
563
+ it.credential to null
564
+ } ? : emptyMap())
565
+ }
566
+
558
567
appViewModel.connectedYubiKey.value?.let { usbYubiKeyDevice ->
559
- useOathSessionUsb(usbYubiKeyDevice) { session ->
560
- try {
561
- oathViewModel.updateCredentials(calculateOathCodes(session))
562
- } catch (apduException: ApduException ) {
563
- if (apduException.sw == SW .SECURITY_CONDITION_NOT_SATISFIED ) {
564
- logger.debug(" Handled oath credential refresh on locked session." )
565
- oathViewModel.setSessionState(
566
- Session (
567
- session,
568
- keyManager.isRemembered(session.deviceId)
568
+ try {
569
+ useOathSessionUsb(usbYubiKeyDevice) { session ->
570
+ try {
571
+ oathViewModel.updateCredentials(calculateOathCodes(session))
572
+ } catch (apduException: ApduException ) {
573
+ if (apduException.sw == SW .SECURITY_CONDITION_NOT_SATISFIED ) {
574
+ logger.debug(" Handled oath credential refresh on locked session." )
575
+ oathViewModel.setSessionState(
576
+ Session (
577
+ session,
578
+ keyManager.isRemembered(session.deviceId)
579
+ )
569
580
)
570
- )
571
- } else {
572
- logger.error(
573
- " Unexpected sw when refreshing oath credentials " ,
574
- apduException
575
- )
581
+ } else {
582
+ logger.error(
583
+ " Unexpected sw when refreshing oath credentials " ,
584
+ apduException
585
+ )
586
+ }
576
587
}
577
588
}
589
+ } catch (ioException: IOException ) {
590
+ logger.error(" IOException when accessing USB device: " , ioException)
591
+ clearCodes()
592
+ } catch (illegalStateException: IllegalStateException ) {
593
+ logger.error(" IllegalStateException when accessing USB device: " , illegalStateException)
594
+ clearCodes()
578
595
}
579
596
}
597
+ }
598
+
580
599
581
600
private suspend fun calculate (credentialId : String ): String =
582
601
useOathSession(OathActionDescription .CalculateCode ) { session ->
0 commit comments