AuthNotificationManager.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  21. class AuthNotificationManager: NSObject {
  22. /** @var kNotificationKey
  23. @brief The key to locate payload data in the remote notification.
  24. */
  25. private let kNotificationDataKey = "com.google.firebase.auth"
  26. /** @var kNotificationReceiptKey
  27. @brief The key for the receipt in the remote notification payload data.
  28. */
  29. private let kNotificationReceiptKey = "receipt"
  30. /** @var kNotificationSecretKey
  31. @brief The key for the secret in the remote notification payload data.
  32. */
  33. private let kNotificationSecretKey = "secret"
  34. /** @var kNotificationProberKey
  35. @brief The key for marking the prober in the remote notification payload data.
  36. */
  37. private let kNotificationProberKey = "warning"
  38. /** @var kProbingTimeout
  39. @brief Timeout for probing whether the app delegate forwards the remote notification to us.
  40. */
  41. private let kProbingTimeout = 1.0
  42. /** @var _application
  43. @brief The application.
  44. */
  45. private let application: UIApplication
  46. /** @var _appCredentialManager
  47. @brief The object to handle app credentials delivered via notification.
  48. */
  49. private let appCredentialManager: AuthAppCredentialManager
  50. /** @var _hasCheckedNotificationForwarding
  51. @brief Whether notification forwarding has been checked or not.
  52. */
  53. private var hasCheckedNotificationForwarding: Bool = false
  54. /** @var _isNotificationBeingForwarded
  55. @brief Whether or not notification is being forwarded
  56. */
  57. private var isNotificationBeingForwarded: Bool = false
  58. /** @property timeout
  59. @brief The timeout for checking for notification forwarding.
  60. @remarks Only tests should access this property.
  61. */
  62. let timeout: TimeInterval
  63. /** @property immediateCallbackForTestFaking
  64. @brief Disable callback waiting for tests.
  65. @remarks Only tests should access this property.
  66. */
  67. var immediateCallbackForTestFaking: (() -> Bool)?
  68. /** @var _pendingCallbacks
  69. @brief All pending callbacks while a check is being performed.
  70. */
  71. private var pendingCallbacks: [(Bool) -> Void]?
  72. /** @fn initWithApplication:appCredentialManager:
  73. @brief Initializes the instance.
  74. @param application The application.
  75. @param appCredentialManager The object to handle app credentials delivered via notification.
  76. @return The initialized instance.
  77. */
  78. init(withApplication application: UIApplication,
  79. appCredentialManager: AuthAppCredentialManager) {
  80. self.application = application
  81. self.appCredentialManager = appCredentialManager
  82. timeout = kProbingTimeout
  83. }
  84. /** @fn checkNotificationForwardingWithCallback:
  85. @brief Checks whether or not remote notifications are being forwarded to this class.
  86. @param callback The block to be called either immediately or in future once a result
  87. is available.
  88. */
  89. func checkNotificationForwardingInternal(withCallback callback: @escaping (Bool) -> Void) {
  90. if pendingCallbacks != nil {
  91. pendingCallbacks?.append(callback)
  92. return
  93. }
  94. if let getValueFunc = immediateCallbackForTestFaking {
  95. callback(getValueFunc())
  96. return
  97. }
  98. if hasCheckedNotificationForwarding {
  99. callback(isNotificationBeingForwarded)
  100. return
  101. }
  102. hasCheckedNotificationForwarding = true
  103. pendingCallbacks = [callback]
  104. DispatchQueue.main.async {
  105. let proberNotification = [self.kNotificationDataKey: [self.kNotificationProberKey:
  106. "This fake notification should be forwarded to Firebase Auth."]]
  107. if let delegate = self.application.delegate,
  108. delegate
  109. .responds(to: #selector(UIApplicationDelegate
  110. .application(_:didReceiveRemoteNotification:fetchCompletionHandler:))) {
  111. delegate.application?(self.application,
  112. didReceiveRemoteNotification: proberNotification) { _ in
  113. }
  114. } else if let delegate = self.application.delegate,
  115. delegate
  116. .responds(to: #selector(UIApplicationDelegate
  117. .application(_:didReceiveRemoteNotification:))) {
  118. delegate.application?(self.application,
  119. didReceiveRemoteNotification: proberNotification)
  120. } else {
  121. AuthLog.logWarning(
  122. code: "I-AUT000015",
  123. message: "The UIApplicationDelegate must handle " +
  124. "remote notification for phone number authentication to work."
  125. )
  126. }
  127. kAuthGlobalWorkQueue.asyncAfter(deadline: .now() + .seconds(Int(self.timeout))) {
  128. self.callback()
  129. }
  130. }
  131. }
  132. func checkNotificationForwarding() async -> Bool {
  133. return await withCheckedContinuation { continuation in
  134. checkNotificationForwardingInternal { value in
  135. continuation.resume(returning: value)
  136. }
  137. }
  138. }
  139. /** @fn canHandleNotification:
  140. @brief Attempts to handle the remote notification.
  141. @param notification The notification in question.
  142. @return Whether or the notification has been handled.
  143. */
  144. func canHandle(notification: [AnyHashable: Any]) -> Bool {
  145. var stringDictionary: [String: Any]?
  146. let data = notification[kNotificationDataKey]
  147. if let jsonString = data as? String {
  148. // Deserialize in case the data is a JSON string.
  149. guard let jsonData = jsonString.data(using: .utf8),
  150. let dictionary = try? JSONSerialization.jsonObject(with: jsonData) as? [String: Any]
  151. else {
  152. return false
  153. }
  154. stringDictionary = dictionary
  155. }
  156. guard let dictionary = stringDictionary ?? data as? [String: Any] else {
  157. return false
  158. }
  159. if dictionary[kNotificationProberKey] != nil {
  160. if pendingCallbacks == nil {
  161. // The prober notification probably comes from another instance, so pass it along.
  162. return false
  163. }
  164. isNotificationBeingForwarded = true
  165. callback()
  166. return true
  167. }
  168. guard let receipt = dictionary[kNotificationReceiptKey] as? String,
  169. let secret = dictionary[kNotificationSecretKey] as? String else {
  170. return false
  171. }
  172. return appCredentialManager.canFinishVerification(withReceipt: receipt, secret: secret)
  173. }
  174. // MARK: Internal methods
  175. private func callback() {
  176. guard let pendingCallbacks else {
  177. return
  178. }
  179. self.pendingCallbacks = nil
  180. for callback in pendingCallbacks {
  181. callback(isNotificationBeingForwarded)
  182. }
  183. }
  184. }
  185. #endif