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.

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