FIRInstallations.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright 2019 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 "FirebaseInstallations/Source/Library/Public/FirebaseInstallations/FIRInstallations.h"
  17. #if __has_include(<FBLPromises/FBLPromises.h>)
  18. #import <FBLPromises/FBLPromises.h>
  19. #else
  20. #import "FBLPromises.h"
  21. #endif
  22. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  23. #import "FirebaseInstallations/Source/Library/FIRInstallationsAuthTokenResultInternal.h"
  24. #import "FirebaseInstallations/Source/Library/Errors/FIRInstallationsErrorUtil.h"
  25. #import "FirebaseInstallations/Source/Library/FIRInstallationsItem.h"
  26. #import "FirebaseInstallations/Source/Library/FIRInstallationsLogger.h"
  27. #import "FirebaseInstallations/Source/Library/InstallationsIDController/FIRInstallationsIDController.h"
  28. #import "FirebaseInstallations/Source/Library/InstallationsStore/FIRInstallationsStoredAuthToken.h"
  29. NS_ASSUME_NONNULL_BEGIN
  30. static const NSUInteger kExpectedAPIKeyLength = 39;
  31. @protocol FIRInstallationsInstanceProvider <FIRLibrary>
  32. @end
  33. @interface FIRInstallations () <FIRInstallationsInstanceProvider>
  34. @property(nonatomic, readonly) FIROptions *appOptions;
  35. @property(nonatomic, readonly) NSString *appName;
  36. @property(nonatomic, readonly) FIRInstallationsIDController *installationsIDController;
  37. @end
  38. @implementation FIRInstallations
  39. #pragma mark - Firebase component
  40. + (void)load {
  41. [FIRApp registerInternalLibrary:(Class<FIRLibrary>)self withName:@"fire-install"];
  42. }
  43. + (nonnull NSArray<FIRComponent *> *)componentsToRegister {
  44. FIRComponentCreationBlock creationBlock =
  45. ^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  46. *isCacheable = YES;
  47. FIRInstallations *installations = [[FIRInstallations alloc] initWithApp:container.app];
  48. return installations;
  49. };
  50. FIRComponent *installationsProvider =
  51. [FIRComponent componentWithProtocol:@protocol(FIRInstallationsInstanceProvider)
  52. instantiationTiming:FIRInstantiationTimingAlwaysEager
  53. creationBlock:creationBlock];
  54. return @[ installationsProvider ];
  55. }
  56. - (instancetype)initWithApp:(FIRApp *)app {
  57. FIRInstallationsIDController *IDController =
  58. [[FIRInstallationsIDController alloc] initWithApp:app];
  59. // `prefetchAuthToken` is disabled due to b/156746574.
  60. return [self initWithAppOptions:app.options
  61. appName:app.name
  62. installationsIDController:IDController
  63. prefetchAuthToken:NO];
  64. }
  65. /// This designated initializer can be exposed for testing.
  66. - (instancetype)initWithAppOptions:(FIROptions *)appOptions
  67. appName:(NSString *)appName
  68. installationsIDController:(FIRInstallationsIDController *)installationsIDController
  69. prefetchAuthToken:(BOOL)prefetchAuthToken {
  70. self = [super init];
  71. if (self) {
  72. [[self class] validateAppOptions:appOptions appName:appName];
  73. [[self class] assertCompatibleIIDVersion];
  74. _appOptions = [appOptions copy];
  75. _appName = [appName copy];
  76. _installationsIDController = installationsIDController;
  77. // Pre-fetch auth token.
  78. if (prefetchAuthToken) {
  79. [self authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  80. NSError *_Nullable error){
  81. }];
  82. }
  83. }
  84. return self;
  85. }
  86. + (void)validateAppOptions:(FIROptions *)appOptions appName:(NSString *)appName {
  87. NSMutableArray *missingFields = [NSMutableArray array];
  88. if (appName.length < 1) {
  89. [missingFields addObject:@"`FirebaseApp.name`"];
  90. }
  91. if (appOptions.APIKey.length < 1) {
  92. [missingFields addObject:@"`FirebaseOptions.APIKey`"];
  93. }
  94. if (appOptions.googleAppID.length < 1) {
  95. [missingFields addObject:@"`FirebaseOptions.googleAppID`"];
  96. }
  97. if (appOptions.projectID.length < 1) {
  98. [missingFields addObject:@"`FirebaseOptions.projectID`"];
  99. }
  100. if (missingFields.count > 0) {
  101. [NSException
  102. raise:kFirebaseInstallationsErrorDomain
  103. format:
  104. @"%@[%@] Could not configure Firebase Installations due to invalid FirebaseApp "
  105. @"options. The following parameters are nil or empty: %@. If you use "
  106. @"GoogleServices-Info.plist please download the most recent version from the Firebase "
  107. @"Console. If you configure Firebase in code, please make sure you specify all "
  108. @"required parameters.",
  109. kFIRLoggerInstallations, kFIRInstallationsMessageCodeInvalidFirebaseAppOptions,
  110. [missingFields componentsJoinedByString:@", "]];
  111. }
  112. [self validateAPIKey:appOptions.APIKey];
  113. }
  114. + (void)validateAPIKey:(nullable NSString *)APIKey {
  115. NSMutableArray<NSString *> *validationIssues = [[NSMutableArray alloc] init];
  116. if (APIKey.length != kExpectedAPIKeyLength) {
  117. [validationIssues addObject:[NSString stringWithFormat:@"API Key length must be %lu characters",
  118. (unsigned long)kExpectedAPIKeyLength]];
  119. }
  120. if (![[APIKey substringToIndex:1] isEqualToString:@"A"]) {
  121. [validationIssues addObject:@"API Key must start with `A`"];
  122. }
  123. NSMutableCharacterSet *allowedCharacters = [NSMutableCharacterSet alphanumericCharacterSet];
  124. [allowedCharacters
  125. formUnionWithCharacterSet:[NSCharacterSet characterSetWithCharactersInString:@"-_"]];
  126. NSCharacterSet *characters = [NSCharacterSet characterSetWithCharactersInString:APIKey];
  127. if (![allowedCharacters isSupersetOfSet:characters]) {
  128. [validationIssues addObject:@"API Key must contain only base64 url-safe characters characters"];
  129. }
  130. if (validationIssues.count > 0) {
  131. [NSException
  132. raise:kFirebaseInstallationsErrorDomain
  133. format:
  134. @"%@[%@] Could not configure Firebase Installations due to invalid FirebaseApp "
  135. @"options. `FirebaseOptions.APIKey` doesn't match the expected format: %@. If you use "
  136. @"GoogleServices-Info.plist please download the most recent version from the Firebase "
  137. @"Console. If you configure Firebase in code, please make sure you specify all "
  138. @"required parameters.",
  139. kFIRLoggerInstallations, kFIRInstallationsMessageCodeInvalidFirebaseAppOptions,
  140. [validationIssues componentsJoinedByString:@", "]];
  141. }
  142. }
  143. #pragma mark - Public
  144. + (FIRInstallations *)installations {
  145. FIRApp *defaultApp = [FIRApp defaultApp];
  146. if (!defaultApp) {
  147. [NSException raise:kFirebaseInstallationsErrorDomain
  148. format:@"The default FirebaseApp instance must be configured before the default"
  149. @"FirebaseApp instance can be initialized. One way to ensure this is to "
  150. @"call `FirebaseApp.configure()` in the App Delegate's "
  151. @"`application(_:didFinishLaunchingWithOptions:)` "
  152. @"(or the `@main` struct's initializer in SwiftUI)."];
  153. }
  154. return [self installationsWithApp:defaultApp];
  155. }
  156. + (FIRInstallations *)installationsWithApp:(FIRApp *)app {
  157. id<FIRInstallationsInstanceProvider> installations =
  158. FIR_COMPONENT(FIRInstallationsInstanceProvider, app.container);
  159. return (FIRInstallations *)installations;
  160. }
  161. - (void)installationIDWithCompletion:(FIRInstallationsIDHandler)completion {
  162. [self.installationsIDController getInstallationItem]
  163. .then(^id(FIRInstallationsItem *installation) {
  164. completion(installation.firebaseInstallationID, nil);
  165. return nil;
  166. })
  167. .catch(^(NSError *error) {
  168. completion(nil, [FIRInstallationsErrorUtil publicDomainErrorWithError:error]);
  169. });
  170. }
  171. - (void)authTokenWithCompletion:(FIRInstallationsTokenHandler)completion {
  172. [self authTokenForcingRefresh:NO completion:completion];
  173. }
  174. - (void)authTokenForcingRefresh:(BOOL)forceRefresh
  175. completion:(FIRInstallationsTokenHandler)completion {
  176. [self.installationsIDController getAuthTokenForcingRefresh:forceRefresh]
  177. .then(^FIRInstallationsAuthTokenResult *(FIRInstallationsItem *installation) {
  178. FIRInstallationsAuthTokenResult *result = [[FIRInstallationsAuthTokenResult alloc]
  179. initWithToken:installation.authToken.token
  180. expirationDate:installation.authToken.expirationDate];
  181. return result;
  182. })
  183. .then(^id(FIRInstallationsAuthTokenResult *token) {
  184. completion(token, nil);
  185. return nil;
  186. })
  187. .catch(^void(NSError *error) {
  188. completion(nil, [FIRInstallationsErrorUtil publicDomainErrorWithError:error]);
  189. });
  190. }
  191. - (void)deleteWithCompletion:(void (^)(NSError *__nullable error))completion {
  192. [self.installationsIDController deleteInstallation]
  193. .then(^id(id result) {
  194. completion(nil);
  195. return nil;
  196. })
  197. .catch(^void(NSError *error) {
  198. completion([FIRInstallationsErrorUtil publicDomainErrorWithError:error]);
  199. });
  200. }
  201. #pragma mark - IID version compatibility
  202. + (void)assertCompatibleIIDVersion {
  203. // We use this flag to disable IID compatibility exception for unit tests.
  204. #ifdef FIR_INSTALLATIONS_ALLOWS_INCOMPATIBLE_IID_VERSION
  205. return;
  206. #else
  207. if (![self isIIDVersionCompatible]) {
  208. [NSException
  209. raise:kFirebaseInstallationsErrorDomain
  210. format:@"Firebase Instance ID is not compatible with Firebase 8.x+. Please remove the "
  211. @"dependency from the app. See the documentation at "
  212. @"https://firebase.google.com/docs/cloud-messaging/ios/"
  213. @"client#fetching-the-current-registration-token."];
  214. }
  215. #endif
  216. }
  217. + (BOOL)isIIDVersionCompatible {
  218. Class IIDClass = NSClassFromString(@"FIRInstanceID");
  219. if (IIDClass == nil) {
  220. // It is OK if there is no IID at all.
  221. return YES;
  222. }
  223. // We expect a compatible version having the method `+[FIRInstanceID usesFIS]` defined.
  224. BOOL isCompatibleVersion = [IIDClass respondsToSelector:NSSelectorFromString(@"usesFIS")];
  225. return isCompatibleVersion;
  226. }
  227. #pragma mark - Force Category Linking
  228. extern void FIRInclude_FIRInstallationsItem_RegisterInstallationAPI_Category(void);
  229. /// Does nothing when called, and not meant to be called.
  230. ///
  231. /// This method forces the linker to include categories even if
  232. /// users do not include the '-ObjC' linker flag in their project.
  233. + (void)noop {
  234. FIRInclude_FIRInstallationsItem_RegisterInstallationAPI_Category();
  235. }
  236. @end
  237. NS_ASSUME_NONNULL_END