TOTPService.swift 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // LoopFollow
  2. // TOTPService.swift
  3. import Foundation
  4. /// Service class for managing TOTP code usage and blocking logic
  5. class TOTPService {
  6. static let shared = TOTPService()
  7. private init() {}
  8. /// Checks if the current TOTP code is blocked (already used)
  9. /// - Parameter qrCodeURL: The QR code URL to extract the current TOTP from
  10. /// - Returns: True if the TOTP is blocked, false otherwise
  11. func isTOTPBlocked(qrCodeURL: String) -> Bool {
  12. guard let currentTOTP = TOTPGenerator.extractOTPFromURL(qrCodeURL) else {
  13. return false
  14. }
  15. // Check if the current TOTP code equals the last sent TOTP code
  16. return currentTOTP == Observable.shared.lastSentTOTP.value
  17. }
  18. /// Marks the current TOTP code as used
  19. /// - Parameter qrCodeURL: The QR code URL to extract the current TOTP from
  20. func markTOTPAsUsed(qrCodeURL: String) {
  21. if let currentTOTP = TOTPGenerator.extractOTPFromURL(qrCodeURL) {
  22. Observable.shared.lastSentTOTP.set(currentTOTP)
  23. }
  24. }
  25. /// Resets the TOTP usage tracking (called when a new TOTP period starts)
  26. func resetTOTPUsage() {
  27. Observable.shared.lastSentTOTP.set(nil)
  28. }
  29. }