Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes an issue where a continuation was called twice #151

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions Authenticator/Model/MainViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
Expand Down
38 changes: 28 additions & 10 deletions Authenticator/Model/OATHSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum OATHSessionError: Error, LocalizedError, Equatable {
case credentialAlreadyPresent(YKFOATHCredentialTemplate)
case otpEnabledError
case connectionCancelled
case invalidDeviceInfo

public var errorDescription: String? {
switch self {
Expand All @@ -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.")
}
}
}
Expand Down Expand Up @@ -146,22 +149,37 @@ 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
}
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
Expand All @@ -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
Expand All @@ -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
}
Expand Down
Loading