FIRAuthNotificationManager.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #include <TargetConditionals.h>
  17. #if !TARGET_OS_OSX
  18. #import "FIRAuthNotificationManager.h"
  19. #import <FirebaseCore/FIRLogger.h>
  20. #import "FIRAuthAppCredential.h"
  21. #import "FIRAuthAppCredentialManager.h"
  22. #import "FIRAuthGlobalWorkQueue.h"
  23. NS_ASSUME_NONNULL_BEGIN
  24. /** @var kNotificationKey
  25. @brief The key to locate payload data in the remote notification.
  26. */
  27. static NSString *const kNotificationDataKey = @"com.google.firebase.auth";
  28. /** @var kNotificationReceiptKey
  29. @brief The key for the receipt in the remote notification payload data.
  30. */
  31. static NSString *const kNotificationReceiptKey = @"receipt";
  32. /** @var kNotificationSecretKey
  33. @brief The key for the secret in the remote notification payload data.
  34. */
  35. static NSString *const kNotificationSecretKey = @"secret";
  36. /** @var kNotificationProberKey
  37. @brief The key for marking the prober in the remote notification payload data.
  38. */
  39. static NSString *const kNotificationProberKey = @"warning";
  40. /** @var kProbingTimeout
  41. @brief Timeout for probing whether the app delegate forwards the remote notification to us.
  42. */
  43. static const NSTimeInterval kProbingTimeout = 1;
  44. @implementation FIRAuthNotificationManager {
  45. /** @var _application
  46. @brief The application.
  47. */
  48. UIApplication *_application;
  49. /** @var _appCredentialManager
  50. @brief The object to handle app credentials delivered via notification.
  51. */
  52. FIRAuthAppCredentialManager *_appCredentialManager;
  53. /** @var _hasCheckedNotificationForwarding
  54. @brief Whether notification forwarding has been checked or not.
  55. */
  56. BOOL _hasCheckedNotificationForwarding;
  57. /** @var _isNotificationBeingForwarded
  58. @brief Whether or not notification is being forwarded
  59. */
  60. BOOL _isNotificationBeingForwarded;
  61. /** @var _pendingCallbacks
  62. @brief All pending callbacks while a check is being performed.
  63. */
  64. NSMutableArray<FIRAuthNotificationForwardingCallback> *_pendingCallbacks;
  65. }
  66. - (instancetype)initWithApplication:(UIApplication *)application
  67. appCredentialManager:(FIRAuthAppCredentialManager *)appCredentialManager {
  68. self = [super init];
  69. if (self) {
  70. _application = application;
  71. _appCredentialManager = appCredentialManager;
  72. _timeout = kProbingTimeout;
  73. }
  74. return self;
  75. }
  76. - (void)checkNotificationForwardingWithCallback:(FIRAuthNotificationForwardingCallback)callback {
  77. if (_pendingCallbacks) {
  78. [_pendingCallbacks addObject:callback];
  79. return;
  80. }
  81. if (_hasCheckedNotificationForwarding) {
  82. callback(_isNotificationBeingForwarded);
  83. return;
  84. }
  85. _hasCheckedNotificationForwarding = YES;
  86. _pendingCallbacks =
  87. [[NSMutableArray<FIRAuthNotificationForwardingCallback> alloc] initWithObjects:callback, nil];
  88. dispatch_async(dispatch_get_main_queue(), ^{
  89. NSDictionary *proberNotification = @{
  90. kNotificationDataKey : @{
  91. kNotificationProberKey : @"This fake notification should be forwarded to Firebase Auth."
  92. }
  93. };
  94. if ([self->_application.delegate
  95. respondsToSelector:@selector(application:
  96. didReceiveRemoteNotification:fetchCompletionHandler:)]) {
  97. [self->_application.delegate application:self->_application
  98. didReceiveRemoteNotification:proberNotification
  99. fetchCompletionHandler:^(UIBackgroundFetchResult result){
  100. }];
  101. #if !TARGET_OS_TV
  102. } else if ([self->_application.delegate
  103. respondsToSelector:@selector(application:didReceiveRemoteNotification:)]) {
  104. [self->_application.delegate application:self->_application
  105. didReceiveRemoteNotification:proberNotification];
  106. #endif
  107. } else {
  108. FIRLogWarning(kFIRLoggerAuth, @"I-AUT000015",
  109. @"The UIApplicationDelegate must handle remote notification for phone number "
  110. @"authentication to work.");
  111. }
  112. });
  113. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_timeout * NSEC_PER_SEC)),
  114. FIRAuthGlobalWorkQueue(), ^{
  115. [self callBack];
  116. });
  117. }
  118. - (BOOL)canHandleNotification:(NSDictionary *)notification {
  119. NSDictionary *data = notification[kNotificationDataKey];
  120. if ([data isKindOfClass:[NSString class]]) {
  121. // Deserialize in case the data is a JSON string.
  122. NSData *JSONData = [((NSString *)data) dataUsingEncoding:NSUTF8StringEncoding];
  123. data = [NSJSONSerialization JSONObjectWithData:JSONData options:0 error:NULL];
  124. }
  125. if (![data isKindOfClass:[NSDictionary class]]) {
  126. return NO;
  127. }
  128. if (data[kNotificationProberKey]) {
  129. if (!_pendingCallbacks) {
  130. // The prober notification probably comes from another instance, so pass it along.
  131. return NO;
  132. }
  133. _isNotificationBeingForwarded = YES;
  134. [self callBack];
  135. return YES;
  136. }
  137. NSString *receipt = data[kNotificationReceiptKey];
  138. if (![receipt isKindOfClass:[NSString class]]) {
  139. return NO;
  140. }
  141. NSString *secret = data[kNotificationSecretKey];
  142. if (![receipt isKindOfClass:[NSString class]]) {
  143. return NO;
  144. }
  145. return [_appCredentialManager canFinishVerificationWithReceipt:receipt secret:secret];
  146. }
  147. #pragma mark - Internal methods
  148. /** @fn callBack
  149. @brief Calls back all pending callbacks with the result of notification forwarding check.
  150. */
  151. - (void)callBack {
  152. if (!_pendingCallbacks) {
  153. return;
  154. }
  155. NSArray<FIRAuthNotificationForwardingCallback> *allCallbacks = _pendingCallbacks;
  156. _pendingCallbacks = nil;
  157. for (FIRAuthNotificationForwardingCallback callback in allCallbacks) {
  158. callback(_isNotificationBeingForwarded);
  159. }
  160. };
  161. @end
  162. NS_ASSUME_NONNULL_END
  163. #endif