FIRAuthNotificationManager.m 5.9 KB

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