FIRAuthNotificationManager.m 6.4 KB

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