Skip to content

Commit

Permalink
Clear error message on Receiver QR code scan in OTP scan story.
Browse files Browse the repository at this point in the history
  • Loading branch information
tijme committed Jan 23, 2022
1 parent abceb0a commit e434846
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Raivo/Controllers/Main/MainScanPasswordViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ class MainScanPasswordViewController: UIViewController, AVCaptureMetadataOutputO
}
}

guard ReceiverHelper.shared.isValid(contents) else {
log.verbose("The scanned QR code is not a valid OTP (it is a receiver instead).")
return BannerHelper.shared.error("Invalid QR code", "Please scan your receiver in the settings screen", wrapper: view) {
self.currentlyCheckingToken = false
}
}

guard SeedValueValidator.isValid(contents) else {
log.verbose("The scanned QR code is not a valid OTP.")
return BannerHelper.shared.error("Invalid QR code", "The QR code is not a valid OTP", wrapper: view) {
Expand Down
14 changes: 13 additions & 1 deletion Raivo/Helpers/ReceiverHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ReceiverHelper {
///
/// - Parameter qrcode: The given QR-code containing a device name and APNS token
/// - Returns: The resulting receiver
public func getReceiverFromQRCode(qrcode: String) throws -> Receiver {
public func getReceiverFromQRCode(_ qrcode: String) throws -> Receiver {
guard let uri = URLComponents(string: qrcode) else {
throw ValidationError.invalidFormat("QR-code does not contain a valid URL.")
}
Expand Down Expand Up @@ -54,6 +54,18 @@ class ReceiverHelper {
return receiver
}

/// Validate a `Receiver` QR code
///
/// - Parameter qrcode: The given QR-code containing a device name and APNS token
/// - Returns: Positive if the given QR code is valid
public func isValid(_ qrcode: String) -> Bool {
guard let _ = try? getReceiverFromQRCode(qrcode) else {
return true
}

return false
}

/// Check if there are any MacOS receivers currently registered
///
/// - Returns: Positive if there are any receivers
Expand Down
2 changes: 1 addition & 1 deletion Raivo/Views/SwiftUI/MainReceiversView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ struct MainReceiversView: View {
var receiver: Receiver? = nil

do {
receiver = try ReceiverHelper.shared.getReceiverFromQRCode(qrcode: code.string)
receiver = try ReceiverHelper.shared.getReceiverFromQRCode(code.string)
} catch let error {
log.error(error.localizedDescription)
return BannerHelper.shared.done("Error", error.localizedDescription, duration: 3.0)
Expand Down

0 comments on commit e434846

Please sign in to comment.