Skip to content

Commit b05eb8b

Browse files
committed
Error handling and removed debug print.
1 parent b0e0655 commit b05eb8b

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

Authenticator/Model/TokenRequestViewModel.swift

+9-26
Original file line numberDiff line numberDiff line change
@@ -182,39 +182,29 @@ class TokenRequestViewModel: NSObject {
182182
extension TokenRequestViewModel {
183183

184184
func isYubiOTPEnabledOverUSBC(completion: @escaping (Bool) -> Void) {
185-
print(#function)
186185
connection.smartCardConnection { connection in
187-
print("got \(connection)")
188186
connection?.managementSession { session, error in
189-
print("got \(session)")
190-
guard let session else { return }
187+
guard let session else { completion(false); return }
191188
session.getDeviceInfo { deviceInfo, error in
192-
print("got \(deviceInfo)")
193-
guard let deviceInfo, let configuration = deviceInfo.configuration else { return }
194-
guard !configuration.isEnabled(.OTP, overTransport: .USB) || SettingsConfig.isOTPOverUSBIgnored(deviceId: deviceInfo.serialNumber + 1) else {
195-
print("yubiotp enabled")
189+
guard let deviceInfo, let configuration = deviceInfo.configuration else { completion(false); return }
190+
guard !configuration.isEnabled(.OTP, overTransport: .USB) || SettingsConfig.isOTPOverUSBIgnored(deviceId: deviceInfo.serialNumber) else {
196191
completion(true)
197192
return
198193
}
199-
print("yubiotp disabled")
200194
completion(false)
201195
}
202196
}
203197
}
204198
}
205199

206200
func disableOTP(completion: @escaping (Error?) -> Void) {
207-
print(#function)
208201
connection.smartCardConnection { connection in
209202
connection?.managementSession { session, error in
210-
print(session)
211-
guard let session else { return }
203+
guard let session else { completion(error); return }
212204
session.getDeviceInfo { deviceInfo, error in
213-
print(deviceInfo)
214-
guard let deviceInfo, let configuration = deviceInfo.configuration else { return }
205+
guard let deviceInfo, let configuration = deviceInfo.configuration else { completion(error); return }
215206
configuration.setEnabled(false, application: .OTP, overTransport: .USB)
216207
session.write(configuration, reboot: true) { error in
217-
print(error)
218208
completion(error)
219209
}
220210
}
@@ -223,30 +213,23 @@ extension TokenRequestViewModel {
223213
}
224214

225215
func waitForKeyRemoval(completion: @escaping () -> Void) {
226-
print(#function)
227216
connection.didDisconnect { _, _ in
228-
print("")
229217
completion()
230218
}
231219
}
232220

233-
func ignoreThisKey(handler: @escaping (Error?) -> Void) {
234-
print(#function)
221+
func ignoreThisKey(completion: @escaping (Error?) -> Void) {
235222
connection.smartCardConnection { connection in
236-
print(connection)
237223
connection?.managementSession { session, error in
238-
print(session)
239-
guard let session else { handler(error); return }
224+
guard let session else { completion(error); return }
240225
session.getDeviceInfo { deviceInfo, error in
241-
print(deviceInfo)
242-
guard let deviceInfo else { handler(error); return }
226+
guard let deviceInfo else { completion(error); return }
243227
SettingsConfig.registerUSBCDeviceToIgnore(deviceId: deviceInfo.serialNumber)
244-
handler(nil)
228+
completion(nil)
245229
}
246230
}
247231
}
248232
}
249-
250233
}
251234

252235
@available(iOS 14.0, *)

Authenticator/UI/TokenSession/TokenRequestYubiOTPViewController.swift

+16-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ class TokenRequestYubiOTPViewController: UIViewController {
1818

1919
@IBAction func disableOTP() {
2020
viewModel?.disableOTP { error in
21-
guard error == nil else { return }
22-
UIView.animate(withDuration: 0.5) {
23-
DispatchQueue.main.async {
21+
guard error == nil else {
22+
self.presentError(error)
23+
return
24+
}
25+
DispatchQueue.main.async {
26+
UIView.animate(withDuration: 0.5) {
2427
self.optionsView.alpha = 0
2528
self.completedView.alpha = 1
2629
self.viewModel?.waitForKeyRemoval {
@@ -33,10 +36,19 @@ class TokenRequestYubiOTPViewController: UIViewController {
3336

3437
@IBAction func ignoreThisKey() {
3538
viewModel?.ignoreThisKey { error in
36-
guard error == nil else { return }
39+
guard error == nil else {
40+
self.presentError(error)
41+
return
42+
}
3743
DispatchQueue.main.async {
3844
self.dismiss(animated: true)
3945
}
4046
}
4147
}
48+
49+
private func presentError(_ error: Error?) {
50+
guard let error else { return }
51+
let alert = UIAlertController(title: "Error reading YubiKey", message: "\(error.localizedDescription)\n\nRemove and reinsert your YubiKey.") { self.dismiss(animated: true) }
52+
self.present(alert, animated: true, completion: nil)
53+
}
4254
}

0 commit comments

Comments
 (0)