FIRAuthNotificationManager.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 <FirebaseCore/FIRLogger.h>
  19. #import "FirebaseAuth/Sources/Auth/FIRAuthGlobalWorkQueue.h"
  20. #import "FirebaseAuth/Sources/SystemService/FIRAuthAppCredential.h"
  21. #import "FirebaseAuth/Sources/SystemService/FIRAuthAppCredentialManager.h"
  22. #import "FirebaseAuth/Sources/SystemService/FIRAuthNotificationManager.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. // iOS 10 deprecation
  105. #pragma clang diagnostic push
  106. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  107. [self->_application.delegate application:self->_application
  108. didReceiveRemoteNotification:proberNotification];
  109. #pragma clang diagnostic pop
  110. #endif
  111. } else {
  112. FIRLogWarning(kFIRLoggerAuth, @"I-AUT000015",
  113. @"The UIApplicationDelegate must handle remote notification for phone number "
  114. @"authentication to work.");
  115. }
  116. });
  117. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_timeout * NSEC_PER_SEC)),
  118. FIRAuthGlobalWorkQueue(), ^{
  119. [self callBack];
  120. });
  121. }
  122. - (BOOL)canHandleNotification:(NSDictionary *)notification {
  123. NSDictionary *data = notification[kNotificationDataKey];
  124. if ([data isKindOfClass:[NSString class]]) {
  125. // Deserialize in case the data is a JSON string.
  126. NSData *JSONData = [((NSString *)data) dataUsingEncoding:NSUTF8StringEncoding];
  127. data = [NSJSONSerialization JSONObjectWithData:JSONData options:0 error:NULL];
  128. }
  129. if (![data isKindOfClass:[NSDictionary class]]) {
  130. return NO;
  131. }
  132. if (data[kNotificationProberKey]) {
  133. if (!_pendingCallbacks) {
  134. // The prober notification probably comes from another instance, so pass it along.
  135. return NO;
  136. }
  137. _isNotificationBeingForwarded = YES;
  138. [self callBack];
  139. return YES;
  140. }
  141. NSString *receipt = data[kNotificationReceiptKey];
  142. if (![receipt isKindOfClass:[NSString class]]) {
  143. return NO;
  144. }
  145. NSString *secret = data[kNotificationSecretKey];
  146. if (![receipt isKindOfClass:[NSString class]]) {
  147. return NO;
  148. }
  149. return [_appCredentialManager canFinishVerificationWithReceipt:receipt secret:secret];
  150. }
  151. #pragma mark - Internal methods
  152. /** @fn callBack
  153. @brief Calls back all pending callbacks with the result of notification forwarding check.
  154. */
  155. - (void)callBack {
  156. if (!_pendingCallbacks) {
  157. return;
  158. }
  159. NSArray<FIRAuthNotificationForwardingCallback> *allCallbacks = _pendingCallbacks;
  160. _pendingCallbacks = nil;
  161. for (FIRAuthNotificationForwardingCallback callback in allCallbacks) {
  162. callback(_isNotificationBeingForwarded);
  163. }
  164. };
  165. @end
  166. NS_ASSUME_NONNULL_END
  167. #endif