FIRAuthNotificationManager.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <TargetConditionals.h>
  17. #if !TARGET_OS_OSX && !TARGET_OS_WATCH
  18. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  19. #import "FirebaseAuth/Sources/Auth/FIRAuthGlobalWorkQueue.h"
  20. #import "FirebaseAuth/Sources/Auth/FIRAuth_Internal.h"
  21. #import "FirebaseAuth/Sources/SystemService/FIRAuthAppCredential.h"
  22. #import "FirebaseAuth/Sources/SystemService/FIRAuthAppCredentialManager.h"
  23. #import "FirebaseAuth/Sources/SystemService/FIRAuthNotificationManager.h"
  24. NS_ASSUME_NONNULL_BEGIN
  25. /** @var kNotificationKey
  26. @brief The key to locate payload data in the remote notification.
  27. */
  28. static NSString *const kNotificationDataKey = @"com.google.firebase.auth";
  29. /** @var kNotificationReceiptKey
  30. @brief The key for the receipt in the remote notification payload data.
  31. */
  32. static NSString *const kNotificationReceiptKey = @"receipt";
  33. /** @var kNotificationSecretKey
  34. @brief The key for the secret in the remote notification payload data.
  35. */
  36. static NSString *const kNotificationSecretKey = @"secret";
  37. /** @var kNotificationProberKey
  38. @brief The key for marking the prober in the remote notification payload data.
  39. */
  40. static NSString *const kNotificationProberKey = @"warning";
  41. /** @var kProbingTimeout
  42. @brief Timeout for probing whether the app delegate forwards the remote notification to us.
  43. */
  44. static const NSTimeInterval kProbingTimeout = 1;
  45. @implementation FIRAuthNotificationManager {
  46. /** @var _application
  47. @brief The application.
  48. */
  49. UIApplication *_application;
  50. /** @var _appCredentialManager
  51. @brief The object to handle app credentials delivered via notification.
  52. */
  53. FIRAuthAppCredentialManager *_appCredentialManager;
  54. /** @var _hasCheckedNotificationForwarding
  55. @brief Whether notification forwarding has been checked or not.
  56. */
  57. BOOL _hasCheckedNotificationForwarding;
  58. /** @var _isNotificationBeingForwarded
  59. @brief Whether or not notification is being forwarded
  60. */
  61. BOOL _isNotificationBeingForwarded;
  62. /** @var _pendingCallbacks
  63. @brief All pending callbacks while a check is being performed.
  64. */
  65. NSMutableArray<FIRAuthNotificationForwardingCallback> *_pendingCallbacks;
  66. }
  67. - (instancetype)initWithApplication:(UIApplication *)application
  68. appCredentialManager:(FIRAuthAppCredentialManager *)appCredentialManager {
  69. self = [super init];
  70. if (self) {
  71. _application = application;
  72. _appCredentialManager = appCredentialManager;
  73. _timeout = kProbingTimeout;
  74. }
  75. return self;
  76. }
  77. - (void)checkNotificationForwardingWithCallback:(FIRAuthNotificationForwardingCallback)callback {
  78. if (_pendingCallbacks) {
  79. [_pendingCallbacks addObject:callback];
  80. return;
  81. }
  82. if (_hasCheckedNotificationForwarding) {
  83. callback(_isNotificationBeingForwarded);
  84. return;
  85. }
  86. _hasCheckedNotificationForwarding = YES;
  87. _pendingCallbacks =
  88. [[NSMutableArray<FIRAuthNotificationForwardingCallback> alloc] initWithObjects:callback, nil];
  89. // Must be dispatched to the main queue since UIApplication isn't thread-safe.
  90. dispatch_async(dispatch_get_main_queue(), ^{
  91. NSDictionary *proberNotification = @{
  92. kNotificationDataKey : @{
  93. kNotificationProberKey : @"This fake notification should be forwarded to Firebase Auth."
  94. }
  95. };
  96. if ([self->_application.delegate
  97. respondsToSelector:@selector(application:
  98. didReceiveRemoteNotification:fetchCompletionHandler:)]) {
  99. [self->_application.delegate application:self->_application
  100. didReceiveRemoteNotification:proberNotification
  101. fetchCompletionHandler:^(UIBackgroundFetchResult result){
  102. }];
  103. #if !TARGET_OS_TV
  104. } else if ([self->_application.delegate
  105. respondsToSelector:@selector(application:didReceiveRemoteNotification:)]) {
  106. // iOS 10 deprecation
  107. #pragma clang diagnostic push
  108. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  109. [self->_application.delegate application:self->_application
  110. didReceiveRemoteNotification:proberNotification];
  111. #pragma clang diagnostic pop
  112. #endif
  113. } else {
  114. FIRLogWarning(kFIRLoggerAuth, @"I-AUT000015",
  115. @"The UIApplicationDelegate must handle remote notification for phone number "
  116. @"authentication to work.");
  117. }
  118. });
  119. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_timeout * NSEC_PER_SEC)),
  120. FIRAuthGlobalWorkQueue(), ^{
  121. [self callBack];
  122. });
  123. }
  124. - (BOOL)canHandleNotification:(NSDictionary *)notification {
  125. NSDictionary *data = notification[kNotificationDataKey];
  126. if ([data isKindOfClass:[NSString class]]) {
  127. // Deserialize in case the data is a JSON string.
  128. NSData *JSONData = [((NSString *)data) dataUsingEncoding:NSUTF8StringEncoding];
  129. data = [NSJSONSerialization JSONObjectWithData:JSONData options:0 error:NULL];
  130. }
  131. if (![data isKindOfClass:[NSDictionary class]]) {
  132. return NO;
  133. }
  134. if (data[kNotificationProberKey]) {
  135. if (!_pendingCallbacks) {
  136. // The prober notification probably comes from another instance, so pass it along.
  137. return NO;
  138. }
  139. _isNotificationBeingForwarded = YES;
  140. [self callBack];
  141. return YES;
  142. }
  143. NSString *receipt = data[kNotificationReceiptKey];
  144. if (![receipt isKindOfClass:[NSString class]]) {
  145. return NO;
  146. }
  147. NSString *secret = data[kNotificationSecretKey];
  148. if (![receipt isKindOfClass:[NSString class]]) {
  149. return NO;
  150. }
  151. return [_appCredentialManager canFinishVerificationWithReceipt:receipt secret:secret];
  152. }
  153. #pragma mark - Internal methods
  154. /** @fn callBack
  155. @brief Calls back all pending callbacks with the result of notification forwarding check.
  156. */
  157. - (void)callBack {
  158. if (!_pendingCallbacks) {
  159. return;
  160. }
  161. NSArray<FIRAuthNotificationForwardingCallback> *allCallbacks = _pendingCallbacks;
  162. _pendingCallbacks = nil;
  163. for (FIRAuthNotificationForwardingCallback callback in allCallbacks) {
  164. callback(_isNotificationBeingForwarded);
  165. }
  166. };
  167. @end
  168. NS_ASSUME_NONNULL_END
  169. #endif