You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
5.9 KiB

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