diff --git a/Authenticator/Model/MainViewModel.swift b/Authenticator/Model/MainViewModel.swift index 2b30a6c1..6e64ad00 100644 --- a/Authenticator/Model/MainViewModel.swift +++ b/Authenticator/Model/MainViewModel.swift @@ -106,15 +106,16 @@ class MainViewModel: ObservableObject { } } } catch { - self?.sessionTask?.cancel() - // Only handle .otpEnabledError by presenting the disable OTP modal - if let sessionError = error as? OATHSessionError, sessionError == .otpEnabledError { - self?.sessionTask?.cancel() - self?.presentDisableOTP = true + if let sessionError = error as? OATHSessionError { + if sessionError == .otpEnabledError { + self?.presentDisableOTP = true + } else if sessionError != .connectionCancelled { + self?.connectionError = error + } } else { - guard let oathSessionError = error as? OATHSessionError, oathSessionError != .connectionCancelled else { return } self?.connectionError = error } + self?.sessionTask?.cancel() } } } diff --git a/Authenticator/Model/OATHSession.swift b/Authenticator/Model/OATHSession.swift index 8508d0d5..e68b5718 100644 --- a/Authenticator/Model/OATHSession.swift +++ b/Authenticator/Model/OATHSession.swift @@ -21,6 +21,7 @@ enum OATHSessionError: Error, LocalizedError, Equatable { case credentialAlreadyPresent(YKFOATHCredentialTemplate) case otpEnabledError case connectionCancelled + case invalidDeviceInfo public var errorDescription: String? { switch self { @@ -30,6 +31,8 @@ enum OATHSessionError: Error, LocalizedError, Equatable { return String(localized: "Yubico OTP enabled", comment: "OATH otp enabled error message") case .connectionCancelled: return String(localized: "Connection cancelled by user", comment: "Internal error message not to be displayed to the user.") + case .invalidDeviceInfo: + return String(localized: "Invalid device info received from YubiKey", comment: "Internal error message not to be displayed to the user.") } } } @@ -146,12 +149,27 @@ class OATHSessionHandler: NSObject, YKFManagerDelegate { self.wiredConnectionCallback = { connection in if connection.isKind(of: YKFSmartCardConnection.self) && deviceType == .phone { connection.managementSession { session, error in - guard let session else { continuation.resume(throwing: error!); return } + guard let session else { + self.wiredContinuation?.resume(throwing: error!) + self.wiredContinuation = nil + self.wiredConnectionCallback = nil + return + } session.getDeviceInfo { deviceInfo, error in - guard let deviceInfo else { continuation.resume(throwing: error!); return } - guard let configuration = deviceInfo.configuration else { continuation.resume(throwing: "Error!!!"); return } + guard let deviceInfo else { + self.wiredContinuation?.resume(throwing: error!) + self.wiredContinuation = nil + self.wiredConnectionCallback = nil + return + } + guard let configuration = deviceInfo.configuration else { + self.wiredContinuation?.resume(throwing: OATHSessionError.invalidDeviceInfo) + self.wiredContinuation = nil + self.wiredConnectionCallback = nil + return + } guard !configuration.isEnabled(.OTP, overTransport: .USB) || SettingsConfig.isOTPOverUSBIgnored(deviceId: deviceInfo.serialNumber) else { - continuation.resume(throwing: OATHSessionError.otpEnabledError) + self.wiredContinuation?.resume(throwing: OATHSessionError.otpEnabledError) self.wiredContinuation = nil self.wiredConnectionCallback = nil return @@ -159,9 +177,9 @@ class OATHSessionHandler: NSObject, YKFManagerDelegate { connection.oathSession { session, error in if let session { self.currentSession = session - continuation.resume(returning: OATHSession(session: session, type: .wired)) + self.wiredContinuation?.resume(returning: OATHSession(session: session, type: .wired)) } else { - continuation.resume(throwing: error!) + self.wiredContinuation?.resume(throwing: error!) } self.wiredContinuation = nil self.wiredConnectionCallback = nil @@ -173,9 +191,9 @@ class OATHSessionHandler: NSObject, YKFManagerDelegate { connection.oathSession { session, error in if let session { self.currentSession = session - continuation.resume(returning: OATHSession(session: session, type: .wired)) + self.wiredContinuation?.resume(returning: OATHSession(session: session, type: .wired)) } else { - continuation.resume(throwing: error!) + self.wiredContinuation?.resume(throwing: error!) } self.wiredContinuation = nil self.wiredConnectionCallback = nil @@ -202,9 +220,9 @@ class OATHSessionHandler: NSObject, YKFManagerDelegate { connection.oathSession { session, error in if let session { self.currentSession = session - continuation.resume(returning: OATHSession(session: session, type: .nfc)) + self.nfcContinuation?.resume(returning: OATHSession(session: session, type: .nfc)) } else { - continuation.resume(throwing: error!) + self.nfcContinuation?.resume(throwing: error!) } self.nfcContinuation = nil }