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.

182 lines
6.0 KiB

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