FIRAuthAPNSTokenManager.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 <GoogleUtilities/GULAppEnvironmentUtil.h>
  19. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  20. #import "FirebaseAuth/Sources/Auth/FIRAuthGlobalWorkQueue.h"
  21. #import "FirebaseAuth/Sources/Auth/FIRAuth_Internal.h"
  22. #import "FirebaseAuth/Sources/SystemService/FIRAuthAPNSToken.h"
  23. #import "FirebaseAuth/Sources/SystemService/FIRAuthAPNSTokenManager.h"
  24. NS_ASSUME_NONNULL_BEGIN
  25. /** @var kRegistrationTimeout
  26. @brief Timeout for registration for remote notification.
  27. @remarks Once we start to handle `application:didFailToRegisterForRemoteNotificationsWithError:`
  28. we probably don't have to use timeout at all.
  29. */
  30. static const NSTimeInterval kRegistrationTimeout = 5;
  31. /** @var kLegacyRegistrationTimeout
  32. @brief Timeout for registration for remote notification on iOS 7.
  33. */
  34. static const NSTimeInterval kLegacyRegistrationTimeout = 30;
  35. @implementation FIRAuthAPNSTokenManager {
  36. /** @var _application
  37. @brief The @c UIApplication to request the token from.
  38. */
  39. UIApplication *_application;
  40. /** @var _pendingCallbacks
  41. @brief The list of all pending callbacks for the APNs token.
  42. */
  43. NSMutableArray<FIRAuthAPNSTokenCallback> *_pendingCallbacks;
  44. }
  45. - (instancetype)initWithApplication:(UIApplication *)application {
  46. self = [super init];
  47. if (self) {
  48. _application = application;
  49. _timeout = [_application respondsToSelector:@selector(registerForRemoteNotifications)]
  50. ? kRegistrationTimeout
  51. : kLegacyRegistrationTimeout;
  52. }
  53. return self;
  54. }
  55. - (void)getTokenWithCallback:(FIRAuthAPNSTokenCallback)callback {
  56. if (_token) {
  57. callback(_token, nil);
  58. return;
  59. }
  60. if (_pendingCallbacks) {
  61. [_pendingCallbacks addObject:callback];
  62. return;
  63. }
  64. _pendingCallbacks =
  65. [[NSMutableArray<FIRAuthAPNSTokenCallback> alloc] initWithObjects:callback, nil];
  66. dispatch_async(dispatch_get_main_queue(), ^{
  67. if ([self->_application respondsToSelector:@selector(registerForRemoteNotifications)]) {
  68. [self->_application registerForRemoteNotifications];
  69. } else {
  70. #pragma clang diagnostic push
  71. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  72. #if TARGET_OS_IOS
  73. [self->_application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert];
  74. #endif // TARGET_OS_IOS
  75. #pragma clang diagnostic pop
  76. }
  77. });
  78. NSArray<FIRAuthAPNSTokenCallback> *applicableCallbacks = _pendingCallbacks;
  79. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_timeout * NSEC_PER_SEC)),
  80. FIRAuthGlobalWorkQueue(), ^{
  81. // Only cancel if the pending callbacks remain the same, i.e., not triggered yet.
  82. if (applicableCallbacks == self->_pendingCallbacks) {
  83. [self callBackWithToken:nil error:nil];
  84. }
  85. });
  86. }
  87. - (void)setToken:(nullable FIRAuthAPNSToken *)token {
  88. if (!token) {
  89. _token = nil;
  90. return;
  91. }
  92. if (token.type == FIRAuthAPNSTokenTypeUnknown) {
  93. static FIRAuthAPNSTokenType detectedTokenType = FIRAuthAPNSTokenTypeUnknown;
  94. if (detectedTokenType == FIRAuthAPNSTokenTypeUnknown) {
  95. detectedTokenType =
  96. [[self class] isProductionApp] ? FIRAuthAPNSTokenTypeProd : FIRAuthAPNSTokenTypeSandbox;
  97. }
  98. token = [[FIRAuthAPNSToken alloc] initWithData:token.data type:detectedTokenType];
  99. }
  100. _token = token;
  101. [self callBackWithToken:token error:nil];
  102. }
  103. - (void)cancelWithError:(NSError *)error {
  104. [self callBackWithToken:nil error:error];
  105. }
  106. #pragma mark - Internal methods
  107. /** @fn callBack
  108. @brief Calls back all pending callbacks with APNs token or error.
  109. @param token The APNs token if one is available.
  110. @param error The error occurred, if any.
  111. */
  112. - (void)callBackWithToken:(nullable FIRAuthAPNSToken *)token error:(nullable NSError *)error {
  113. if (!_pendingCallbacks) {
  114. return;
  115. }
  116. NSArray<FIRAuthAPNSTokenCallback> *allCallbacks = _pendingCallbacks;
  117. _pendingCallbacks = nil;
  118. for (FIRAuthAPNSTokenCallback callback in allCallbacks) {
  119. callback(token, error);
  120. }
  121. };
  122. /** @fn isProductionApp
  123. @brief Whether or not the app has production (versus sandbox) provisioning profile.
  124. @remarks This method is adapted from @c FIRInstanceID .
  125. */
  126. + (BOOL)isProductionApp {
  127. const BOOL defaultAppTypeProd = YES;
  128. NSError *error = nil;
  129. if ([GULAppEnvironmentUtil isSimulator]) {
  130. FIRLogInfo(kFIRLoggerAuth, @"I-AUT000006", @"Assuming prod APNs token type on simulator.");
  131. return defaultAppTypeProd;
  132. }
  133. // Apps distributed via AppStore or TestFlight use the Production APNS certificates.
  134. if ([GULAppEnvironmentUtil isFromAppStore]) {
  135. return defaultAppTypeProd;
  136. }
  137. NSString *path = [[[NSBundle mainBundle] bundlePath]
  138. stringByAppendingPathComponent:@"embedded.mobileprovision"];
  139. if ([GULAppEnvironmentUtil isAppStoreReceiptSandbox] && !path.length) {
  140. // Distributed via TestFlight
  141. return defaultAppTypeProd;
  142. }
  143. NSMutableData *profileData = [NSMutableData dataWithContentsOfFile:path options:0 error:&error];
  144. if (!profileData.length || error) {
  145. FIRLogInfo(kFIRLoggerAuth, @"I-AUT000007", @"Error while reading embedded mobileprovision %@",
  146. error);
  147. return defaultAppTypeProd;
  148. }
  149. // The "embedded.mobileprovision" sometimes contains characters with value 0, which signals the
  150. // end of a c-string and halts the ASCII parser, or with value > 127, which violates strict 7-bit
  151. // ASCII. Replace any 0s or invalid characters in the input.
  152. uint8_t *profileBytes = (uint8_t *)profileData.bytes;
  153. for (int i = 0; i < profileData.length; i++) {
  154. uint8_t currentByte = profileBytes[i];
  155. if (!currentByte || currentByte > 127) {
  156. profileBytes[i] = '.';
  157. }
  158. }
  159. NSString *embeddedProfile = [[NSString alloc] initWithBytesNoCopy:profileBytes
  160. length:profileData.length
  161. encoding:NSASCIIStringEncoding
  162. freeWhenDone:NO];
  163. if (error || !embeddedProfile.length) {
  164. FIRLogInfo(kFIRLoggerAuth, @"I-AUT000008", @"Error while reading embedded mobileprovision %@",
  165. error);
  166. return defaultAppTypeProd;
  167. }
  168. NSScanner *scanner = [NSScanner scannerWithString:embeddedProfile];
  169. NSString *plistContents;
  170. if ([scanner scanUpToString:@"<plist" intoString:nil]) {
  171. if ([scanner scanUpToString:@"</plist>" intoString:&plistContents]) {
  172. plistContents = [plistContents stringByAppendingString:@"</plist>"];
  173. }
  174. }
  175. if (!plistContents.length) {
  176. return defaultAppTypeProd;
  177. }
  178. NSData *data = [plistContents dataUsingEncoding:NSUTF8StringEncoding];
  179. if (!data.length) {
  180. FIRLogInfo(kFIRLoggerAuth, @"I-AUT000009",
  181. @"Couldn't read plist fetched from embedded mobileprovision");
  182. return defaultAppTypeProd;
  183. }
  184. NSError *plistMapError;
  185. id plistData = [NSPropertyListSerialization propertyListWithData:data
  186. options:NSPropertyListImmutable
  187. format:nil
  188. error:&plistMapError];
  189. if (plistMapError || ![plistData isKindOfClass:[NSDictionary class]]) {
  190. FIRLogInfo(kFIRLoggerAuth, @"I-AUT000010", @"Error while converting assumed plist to dict %@",
  191. plistMapError.localizedDescription);
  192. return defaultAppTypeProd;
  193. }
  194. NSDictionary *plistMap = (NSDictionary *)plistData;
  195. if ([plistMap valueForKeyPath:@"ProvisionedDevices"]) {
  196. FIRLogInfo(kFIRLoggerAuth, @"I-AUT000011",
  197. @"Provisioning profile has specifically provisioned devices, "
  198. @"most likely a Dev profile.");
  199. }
  200. NSString *apsEnvironment = [plistMap valueForKeyPath:@"Entitlements.aps-environment"];
  201. FIRLogDebug(kFIRLoggerAuth, @"I-AUT000012", @"APNS Environment in profile: %@", apsEnvironment);
  202. // No aps-environment in the profile.
  203. if (!apsEnvironment.length) {
  204. FIRLogInfo(kFIRLoggerAuth, @"I-AUT000013",
  205. @"No aps-environment set. If testing on a device APNS is not "
  206. @"correctly configured. Please recheck your provisioning profiles.");
  207. return defaultAppTypeProd;
  208. }
  209. if ([apsEnvironment isEqualToString:@"development"]) {
  210. return NO;
  211. }
  212. return defaultAppTypeProd;
  213. }
  214. @end
  215. NS_ASSUME_NONNULL_END
  216. #endif