Skip to content

Commit

Permalink
Merge branch 'jens/continuation-error'
Browse files Browse the repository at this point in the history
  • Loading branch information
jensutbult committed Nov 26, 2024
2 parents c27f847 + ee6039d commit b219493
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
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

0 comments on commit b219493

Please sign in to comment.