From 3432835450d1bdba0f0fb0db95434d66469f2a66 Mon Sep 17 00:00:00 2001 From: Adam Velebil Date: Wed, 11 Sep 2024 00:16:27 +0200 Subject: [PATCH] use correct context/section for AddToAny --- .../yubico/authenticator/oath/OathManager.kt | 35 ++++++++++++++++++- lib/android/oath/state.dart | 3 -- lib/app/views/main_page.dart | 4 +++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/android/app/src/main/kotlin/com/yubico/authenticator/oath/OathManager.kt b/android/app/src/main/kotlin/com/yubico/authenticator/oath/OathManager.kt index e5b1036b..c8073b38 100644 --- a/android/app/src/main/kotlin/com/yubico/authenticator/oath/OathManager.kt +++ b/android/app/src/main/kotlin/com/yubico/authenticator/oath/OathManager.kt @@ -232,7 +232,31 @@ class OathManager( device.withConnection { connection -> val session = getOathSession(connection) val previousId = oathViewModel.currentSession()?.deviceId - if (session.deviceId == previousId && device is NfcYubiKeyDevice) { + // only run pending action over NFC + // when the device is still the same + // or when there is no previous device, but we have a pending action + if (device is NfcYubiKeyDevice && + ((session.deviceId == previousId) || + (previousId == null && pendingAction != null)) + ) { + // update session if it is null + if (previousId == null) { + oathViewModel.setSessionState( + Session( + session, + keyManager.isRemembered(session.deviceId) + ) + ) + + if (!session.isLocked) { + try { + // only load the accounts without calculating the codes + oathViewModel.updateCredentials(getAccounts(session)) + } catch (e: IOException) { + oathViewModel.updateCredentials(emptyMap()) + } } + } + // Either run a pending action, or just refresh codes if (pendingAction != null) { pendingAction?.let { action -> @@ -686,6 +710,15 @@ class OathManager( return session } + private fun getAccounts(session: YubiKitOathSession): Map { + return session.credentials.map { credential -> + Pair( + Credential(credential, session.deviceId), + null + ) + }.toMap() + } + private fun calculateOathCodes(session: YubiKitOathSession): Map { val isUsbKey = deviceManager.isUsbKeyConnected() var timestamp = System.currentTimeMillis() diff --git a/lib/android/oath/state.dart b/lib/android/oath/state.dart index 36416670..2b47e892 100755 --- a/lib/android/oath/state.dart +++ b/lib/android/oath/state.dart @@ -76,9 +76,6 @@ class _AndroidOathStateNotifier extends OathStateNotifier { @override Future reset() async { try { - // await ref - // .read(androidAppContextHandler) - // .switchAppContext(Application.accounts); await oath.invoke('reset'); } catch (e) { _log.debug('Calling reset failed with exception: $e'); diff --git a/lib/app/views/main_page.dart b/lib/app/views/main_page.dart index b2e18721..39176e49 100755 --- a/lib/app/views/main_page.dart +++ b/lib/app/views/main_page.dart @@ -109,6 +109,10 @@ class MainPage extends ConsumerWidget { label: Text(l10n.s_add_account), icon: const Icon(Symbols.person_add_alt), onPressed: () async { + // make sure we execute the "Add account" in OATH section + ref + .read(currentSectionProvider.notifier) + .setCurrentSection(Section.accounts); await addOathAccount(context, ref); }) ],