TOTPService.swift 1.2 KB

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