AuthNotificationManager.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if !os(macOS) && !os(watchOS)
  15. import Foundation
  16. import UIKit
  17. /** @class FIRAuthAppCredential
  18. @brief A class represents a credential that proves the identity of the app.
  19. */
  20. @objc(FIRAuthNotificationManager) public class AuthNotificationManager: NSObject {
  21. /** @var kNotificationKey
  22. @brief The key to locate payload data in the remote notification.
  23. */
  24. private let kNotificationDataKey = "com.google.firebase.auth"
  25. /** @var kNotificationReceiptKey
  26. @brief The key for the receipt in the remote notification payload data.
  27. */
  28. private let kNotificationReceiptKey = "receipt"
  29. /** @var kNotificationSecretKey
  30. @brief The key for the secret in the remote notification payload data.
  31. */
  32. private let kNotificationSecretKey = "secret"
  33. /** @var kNotificationProberKey
  34. @brief The key for marking the prober in the remote notification payload data.
  35. */
  36. private let kNotificationProberKey = "warning"
  37. /** @var kProbingTimeout
  38. @brief Timeout for probing whether the app delegate forwards the remote notification to us.
  39. */
  40. private let kProbingTimeout = 1.0
  41. /** @var _application
  42. @brief The application.
  43. */
  44. private let application: Application
  45. /** @var _appCredentialManager
  46. @brief The object to handle app credentials delivered via notification.
  47. */
  48. private let appCredentialManager: AuthAppCredentialManager
  49. /** @var _hasCheckedNotificationForwarding
  50. @brief Whether notification forwarding has been checked or not.
  51. */
  52. private var hasCheckedNotificationForwarding: Bool = false
  53. /** @var _isNotificationBeingForwarded
  54. @brief Whether or not notification is being forwarded
  55. */
  56. private var isNotificationBeingForwarded: Bool = false
  57. /** @property timeout
  58. @brief The timeout for checking for notification forwarding.
  59. @remarks Only tests should access this property.
  60. */
  61. @objc public let timeout: TimeInterval
  62. /** @property immediateCallbackForTestFaking
  63. @brief Disable callback waiting for tests.
  64. @remarks Only tests should access this property.
  65. */
  66. var immediateCallbackForTestFaking: (() -> Bool)?
  67. /** @var _pendingCallbacks
  68. @brief All pending callbacks while a check is being performed.
  69. */
  70. private var pendingCallbacks: [(Bool) -> Void]?
  71. /** @fn initWithApplication:appCredentialManager:
  72. @brief Initializes the instance.
  73. @param application The application.
  74. @param appCredentialManager The object to handle app credentials delivered via notification.
  75. @return The initialized instance.
  76. */
  77. @objc public init(withApplication application: Application,
  78. appCredentialManager: AuthAppCredentialManager) {
  79. self.application = application
  80. self.appCredentialManager = appCredentialManager
  81. timeout = kProbingTimeout
  82. }
  83. /** @fn checkNotificationForwardingWithCallback:
  84. @brief Checks whether or not remote notifications are being forwarded to this class.
  85. @param callback The block to be called either immediately or in future once a result
  86. is available.
  87. */
  88. @objc public func checkNotificationForwarding(withCallback callback: @escaping (Bool) -> Void) {
  89. if pendingCallbacks != nil {
  90. pendingCallbacks?.append(callback)
  91. return
  92. }
  93. if let getValueFunc = immediateCallbackForTestFaking {
  94. callback(getValueFunc())
  95. return
  96. }
  97. if hasCheckedNotificationForwarding {
  98. callback(isNotificationBeingForwarded)
  99. return
  100. }
  101. hasCheckedNotificationForwarding = true
  102. pendingCallbacks = [callback]
  103. DispatchQueue.main.async {
  104. let proberNotification = [self.kNotificationDataKey: [self.kNotificationProberKey:
  105. "This fake notification should be forwarded to Firebase Auth."]]
  106. if let delegate = self.application.delegate {
  107. delegate.application!(
  108. UIApplication.shared,
  109. didReceiveRemoteNotification: proberNotification
  110. ) { _ in
  111. }
  112. } else {
  113. AuthLog.logWarning(
  114. code: "I-AUT000015",
  115. message: "The UIApplicationDelegate must handle " +
  116. "remote notification for phone number authentication to work."
  117. )
  118. }
  119. kAuthGlobalWorkQueue.asyncAfter(deadline: .now() + .seconds(Int(self.timeout))) {
  120. self.callback()
  121. }
  122. }
  123. }
  124. /** @fn canHandleNotification:
  125. @brief Attempts to handle the remote notification.
  126. @param notification The notification in question.
  127. @return Whether or the notification has been handled.
  128. */
  129. @objc(canHandleNotification:) public func canHandle(notification: [AnyHashable: Any]) -> Bool {
  130. var stringDictionary: [String: Any]?
  131. let data = notification[kNotificationDataKey]
  132. if let jsonString = data as? String {
  133. // Deserialize in case the data is a JSON string.
  134. guard let jsonData = jsonString.data(using: .utf8),
  135. let dictionary = try? JSONSerialization.jsonObject(with: jsonData) as? [String: Any]
  136. else {
  137. return false
  138. }
  139. stringDictionary = dictionary
  140. }
  141. guard let dictionary = stringDictionary ?? data as? [String: Any] else {
  142. return false
  143. }
  144. if dictionary[kNotificationProberKey] != nil {
  145. if pendingCallbacks == nil {
  146. // The prober notification probably comes from another instance, so pass it along.
  147. return false
  148. }
  149. isNotificationBeingForwarded = true
  150. callback()
  151. return true
  152. }
  153. guard let receipt = dictionary[kNotificationReceiptKey] as? String,
  154. let secret = dictionary[kNotificationSecretKey] as? String else {
  155. return false
  156. }
  157. return appCredentialManager.canFinishVerification(withReceipt: receipt, secret: secret)
  158. }
  159. // MARK: Internal methods
  160. private func callback() {
  161. guard let pendingCallbacks else {
  162. return
  163. }
  164. self.pendingCallbacks = nil
  165. for callback in pendingCallbacks {
  166. callback(isNotificationBeingForwarded)
  167. }
  168. }
  169. }
  170. // Protocol for UIApplication to enable unit testing
  171. @objc public protocol ApplicationDelegate {
  172. @objc optional func application(_ application: Application,
  173. didReceiveRemoteNotification userInfo: [AnyHashable: Any],
  174. fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult)
  175. -> Void)
  176. }
  177. @objc public protocol Application {
  178. var delegate: UIApplicationDelegate? { get set }
  179. }
  180. extension UIApplication: Application {}
  181. #endif