FIRInstallationsIDController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 "FIRInstallationsIDController.h"
  17. #if __has_include(<FBLPromises/FBLPromises.h>)
  18. #import <FBLPromises/FBLPromises.h>
  19. #else
  20. #import "FBLPromises.h"
  21. #endif
  22. #import <GoogleUtilities/GULKeychainStorage.h>
  23. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  24. #import "FIRInstallationsAPIService.h"
  25. #import "FIRInstallationsErrorUtil.h"
  26. #import "FIRInstallationsIIDStore.h"
  27. #import "FIRInstallationsIIDTokenStore.h"
  28. #import "FIRInstallationsItem.h"
  29. #import "FIRInstallationsLogger.h"
  30. #import "FIRInstallationsSingleOperationPromiseCache.h"
  31. #import "FIRInstallationsStore.h"
  32. #import "FIRInstallationsHTTPError.h"
  33. #import "FIRInstallationsStoredAuthToken.h"
  34. const NSNotificationName FIRInstallationIDDidChangeNotification =
  35. @"FIRInstallationIDDidChangeNotification";
  36. NSString *const kFIRInstallationIDDidChangeNotificationAppNameKey =
  37. @"FIRInstallationIDDidChangeNotification";
  38. NSTimeInterval const kFIRInstallationsTokenExpirationThreshold = 60 * 60; // 1 hour.
  39. static NSString *const kKeychainService = @"com.firebase.FIRInstallations.installations";
  40. @interface FIRInstallationsIDController ()
  41. @property(nonatomic, readonly) NSString *appID;
  42. @property(nonatomic, readonly) NSString *appName;
  43. @property(nonatomic, readonly) FIRInstallationsStore *installationsStore;
  44. @property(nonatomic, readonly) FIRInstallationsIIDStore *IIDStore;
  45. @property(nonatomic, readonly) FIRInstallationsIIDTokenStore *IIDTokenStore;
  46. @property(nonatomic, readonly) FIRInstallationsAPIService *APIService;
  47. @property(nonatomic, readonly) FIRInstallationsSingleOperationPromiseCache<FIRInstallationsItem *>
  48. *getInstallationPromiseCache;
  49. @property(nonatomic, readonly)
  50. FIRInstallationsSingleOperationPromiseCache<FIRInstallationsItem *> *authTokenPromiseCache;
  51. @property(nonatomic, readonly) FIRInstallationsSingleOperationPromiseCache<FIRInstallationsItem *>
  52. *authTokenForcingRefreshPromiseCache;
  53. @property(nonatomic, readonly)
  54. FIRInstallationsSingleOperationPromiseCache<NSNull *> *deleteInstallationPromiseCache;
  55. @end
  56. @implementation FIRInstallationsIDController
  57. - (instancetype)initWithGoogleAppID:(NSString *)appID
  58. appName:(NSString *)appName
  59. APIKey:(NSString *)APIKey
  60. projectID:(NSString *)projectID
  61. GCMSenderID:(NSString *)GCMSenderID
  62. accessGroup:(nullable NSString *)accessGroup {
  63. NSString *serviceName = [FIRInstallationsIDController keychainServiceWithAppID:appID];
  64. GULKeychainStorage *secureStorage = [[GULKeychainStorage alloc] initWithService:serviceName];
  65. FIRInstallationsStore *installationsStore =
  66. [[FIRInstallationsStore alloc] initWithSecureStorage:secureStorage accessGroup:accessGroup];
  67. // Use `GCMSenderID` as project identifier when `projectID` is not available.
  68. NSString *APIServiceProjectID = (projectID.length > 0) ? projectID : GCMSenderID;
  69. FIRInstallationsAPIService *apiService =
  70. [[FIRInstallationsAPIService alloc] initWithAPIKey:APIKey projectID:APIServiceProjectID];
  71. FIRInstallationsIIDStore *IIDStore = [[FIRInstallationsIIDStore alloc] init];
  72. FIRInstallationsIIDTokenStore *IIDCheckingStore =
  73. [[FIRInstallationsIIDTokenStore alloc] initWithGCMSenderID:GCMSenderID];
  74. return [self initWithGoogleAppID:appID
  75. appName:appName
  76. installationsStore:installationsStore
  77. APIService:apiService
  78. IIDStore:IIDStore
  79. IIDTokenStore:IIDCheckingStore];
  80. }
  81. /// The initializer is supposed to be used by tests to inject `installationsStore`.
  82. - (instancetype)initWithGoogleAppID:(NSString *)appID
  83. appName:(NSString *)appName
  84. installationsStore:(FIRInstallationsStore *)installationsStore
  85. APIService:(FIRInstallationsAPIService *)APIService
  86. IIDStore:(FIRInstallationsIIDStore *)IIDStore
  87. IIDTokenStore:(FIRInstallationsIIDTokenStore *)IIDTokenStore {
  88. self = [super init];
  89. if (self) {
  90. _appID = appID;
  91. _appName = appName;
  92. _installationsStore = installationsStore;
  93. _APIService = APIService;
  94. _IIDStore = IIDStore;
  95. _IIDTokenStore = IIDTokenStore;
  96. __weak FIRInstallationsIDController *weakSelf = self;
  97. _getInstallationPromiseCache = [[FIRInstallationsSingleOperationPromiseCache alloc]
  98. initWithNewOperationHandler:^FBLPromise *_Nonnull {
  99. FIRInstallationsIDController *strongSelf = weakSelf;
  100. return [strongSelf createGetInstallationItemPromise];
  101. }];
  102. _authTokenPromiseCache = [[FIRInstallationsSingleOperationPromiseCache alloc]
  103. initWithNewOperationHandler:^FBLPromise *_Nonnull {
  104. FIRInstallationsIDController *strongSelf = weakSelf;
  105. return [strongSelf installationWithValidAuthTokenForcingRefresh:NO];
  106. }];
  107. _authTokenForcingRefreshPromiseCache = [[FIRInstallationsSingleOperationPromiseCache alloc]
  108. initWithNewOperationHandler:^FBLPromise *_Nonnull {
  109. FIRInstallationsIDController *strongSelf = weakSelf;
  110. return [strongSelf installationWithValidAuthTokenForcingRefresh:YES];
  111. }];
  112. _deleteInstallationPromiseCache = [[FIRInstallationsSingleOperationPromiseCache alloc]
  113. initWithNewOperationHandler:^FBLPromise *_Nonnull {
  114. FIRInstallationsIDController *strongSelf = weakSelf;
  115. return [strongSelf createDeleteInstallationPromise];
  116. }];
  117. }
  118. return self;
  119. }
  120. #pragma mark - Get Installation.
  121. - (FBLPromise<FIRInstallationsItem *> *)getInstallationItem {
  122. return [self.getInstallationPromiseCache getExistingPendingOrCreateNewPromise];
  123. }
  124. - (FBLPromise<FIRInstallationsItem *> *)createGetInstallationItemPromise {
  125. FIRLogDebug(kFIRLoggerInstallations,
  126. kFIRInstallationsMessageCodeNewGetInstallationOperationCreated, @"%s, appName: %@",
  127. __PRETTY_FUNCTION__, self.appName);
  128. FBLPromise<FIRInstallationsItem *> *installationItemPromise =
  129. [self getStoredInstallation].recover(^id(NSError *error) {
  130. return [self createAndSaveFID];
  131. });
  132. // Initiate registration process on success if needed, but return the installation without waiting
  133. // for it.
  134. installationItemPromise.then(^id(FIRInstallationsItem *installation) {
  135. [self getAuthTokenForcingRefresh:NO];
  136. return nil;
  137. });
  138. return installationItemPromise;
  139. }
  140. - (FBLPromise<FIRInstallationsItem *> *)getStoredInstallation {
  141. return [self.installationsStore installationForAppID:self.appID appName:self.appName].validate(
  142. ^BOOL(FIRInstallationsItem *installation) {
  143. BOOL isValid = NO;
  144. switch (installation.registrationStatus) {
  145. case FIRInstallationStatusUnregistered:
  146. case FIRInstallationStatusRegistered:
  147. isValid = YES;
  148. break;
  149. case FIRInstallationStatusUnknown:
  150. isValid = NO;
  151. break;
  152. }
  153. return isValid;
  154. });
  155. }
  156. - (FBLPromise<FIRInstallationsItem *> *)createAndSaveFID {
  157. return [self migrateOrGenerateInstallation]
  158. .then(^FBLPromise<FIRInstallationsItem *> *(FIRInstallationsItem *installation) {
  159. return [self saveInstallation:installation];
  160. })
  161. .then(^FIRInstallationsItem *(FIRInstallationsItem *installation) {
  162. [self postFIDDidChangeNotification];
  163. return installation;
  164. });
  165. }
  166. - (FBLPromise<FIRInstallationsItem *> *)saveInstallation:(FIRInstallationsItem *)installation {
  167. return [self.installationsStore saveInstallation:installation].then(
  168. ^FIRInstallationsItem *(NSNull *result) {
  169. return installation;
  170. });
  171. }
  172. /**
  173. * Tries to migrate IID data stored by FirebaseInstanceID SDK or generates a new Installation ID if
  174. * not found.
  175. */
  176. - (FBLPromise<FIRInstallationsItem *> *)migrateOrGenerateInstallation {
  177. if (![self isDefaultApp]) {
  178. // Existing IID should be used only for default FirebaseApp.
  179. FIRInstallationsItem *installation =
  180. [self createInstallationWithFID:[FIRInstallationsItem generateFID] IIDDefaultToken:nil];
  181. return [FBLPromise resolvedWith:installation];
  182. }
  183. return [[[FBLPromise
  184. all:@[ [self.IIDStore existingIID], [self.IIDTokenStore existingIIDDefaultToken] ]]
  185. then:^id _Nullable(NSArray *_Nullable results) {
  186. NSString *existingIID = results[0];
  187. NSString *IIDDefaultToken = results[1];
  188. return [self createInstallationWithFID:existingIID IIDDefaultToken:IIDDefaultToken];
  189. }] recover:^id _Nullable(NSError *_Nonnull error) {
  190. return [self createInstallationWithFID:[FIRInstallationsItem generateFID] IIDDefaultToken:nil];
  191. }];
  192. }
  193. - (FIRInstallationsItem *)createInstallationWithFID:(NSString *)FID
  194. IIDDefaultToken:(nullable NSString *)IIDDefaultToken {
  195. FIRInstallationsItem *installation = [[FIRInstallationsItem alloc] initWithAppID:self.appID
  196. firebaseAppName:self.appName];
  197. installation.firebaseInstallationID = FID;
  198. installation.IIDDefaultToken = IIDDefaultToken;
  199. installation.registrationStatus = FIRInstallationStatusUnregistered;
  200. return installation;
  201. }
  202. #pragma mark - FID registration
  203. - (FBLPromise<FIRInstallationsItem *> *)registerInstallationIfNeeded:
  204. (FIRInstallationsItem *)installation {
  205. switch (installation.registrationStatus) {
  206. case FIRInstallationStatusRegistered:
  207. // Already registered. Do nothing.
  208. return [FBLPromise resolvedWith:installation];
  209. case FIRInstallationStatusUnknown:
  210. case FIRInstallationStatusUnregistered:
  211. // Registration required. Proceed.
  212. break;
  213. }
  214. return [self.APIService registerInstallation:installation]
  215. .catch(^(NSError *_Nonnull error) {
  216. if ([self doesRegistrationErrorRequireConfigChange:error]) {
  217. FIRLogError(kFIRLoggerInstallations,
  218. kFIRInstallationsMessageCodeInvalidFirebaseConfiguration,
  219. @"Firebase Installation registration failed for app with name: %@, error:\n"
  220. @"%@\nPlease make sure you use valid GoogleService-Info.plist",
  221. self.appName, error.userInfo[NSLocalizedFailureReasonErrorKey]);
  222. }
  223. })
  224. .then(^id(FIRInstallationsItem *registeredInstallation) {
  225. return [self saveInstallation:registeredInstallation];
  226. })
  227. .then(^FIRInstallationsItem *(FIRInstallationsItem *registeredInstallation) {
  228. // Server may respond with a different FID if the sent one cannot be accepted.
  229. if (![registeredInstallation.firebaseInstallationID
  230. isEqualToString:installation.firebaseInstallationID]) {
  231. [self postFIDDidChangeNotification];
  232. }
  233. return registeredInstallation;
  234. });
  235. }
  236. - (BOOL)doesRegistrationErrorRequireConfigChange:(NSError *)error {
  237. FIRInstallationsHTTPError *HTTPError = (FIRInstallationsHTTPError *)error;
  238. if (![HTTPError isKindOfClass:[FIRInstallationsHTTPError class]]) {
  239. return NO;
  240. }
  241. switch (HTTPError.HTTPResponse.statusCode) {
  242. // These are the errors that require Firebase configuration change.
  243. case FIRInstallationsRegistrationHTTPCodeInvalidArgument:
  244. case FIRInstallationsRegistrationHTTPCodeInvalidAPIKey:
  245. case FIRInstallationsRegistrationHTTPCodeAPIKeyToProjectIDMismatch:
  246. case FIRInstallationsRegistrationHTTPCodeProjectNotFound:
  247. return YES;
  248. default:
  249. return NO;
  250. }
  251. }
  252. #pragma mark - Auth Token
  253. - (FBLPromise<FIRInstallationsItem *> *)getAuthTokenForcingRefresh:(BOOL)forceRefresh {
  254. if (forceRefresh || [self.authTokenForcingRefreshPromiseCache getExistingPendingPromise] != nil) {
  255. return [self.authTokenForcingRefreshPromiseCache getExistingPendingOrCreateNewPromise];
  256. } else {
  257. return [self.authTokenPromiseCache getExistingPendingOrCreateNewPromise];
  258. }
  259. }
  260. - (FBLPromise<FIRInstallationsItem *> *)installationWithValidAuthTokenForcingRefresh:
  261. (BOOL)forceRefresh {
  262. FIRLogDebug(kFIRLoggerInstallations, kFIRInstallationsMessageCodeNewGetAuthTokenOperationCreated,
  263. @"-[FIRInstallationsIDController installationWithValidAuthTokenForcingRefresh:%@], "
  264. @"appName: %@",
  265. @(forceRefresh), self.appName);
  266. return [self getInstallationItem]
  267. .then(^FBLPromise<FIRInstallationsItem *> *(FIRInstallationsItem *installation) {
  268. return [self registerInstallationIfNeeded:installation];
  269. })
  270. .then(^id(FIRInstallationsItem *registeredInstallation) {
  271. BOOL isTokenExpiredOrExpiresSoon =
  272. [registeredInstallation.authToken.expirationDate timeIntervalSinceDate:[NSDate date]] <
  273. kFIRInstallationsTokenExpirationThreshold;
  274. if (forceRefresh || isTokenExpiredOrExpiresSoon) {
  275. return [self refreshAuthTokenForInstallation:registeredInstallation];
  276. } else {
  277. return registeredInstallation;
  278. }
  279. })
  280. .recover(^id(NSError *error) {
  281. return [self regenerateFIDOnRefreshTokenErrorIfNeeded:error];
  282. });
  283. }
  284. - (FBLPromise<FIRInstallationsItem *> *)refreshAuthTokenForInstallation:
  285. (FIRInstallationsItem *)installation {
  286. return [[self.APIService refreshAuthTokenForInstallation:installation]
  287. then:^id _Nullable(FIRInstallationsItem *_Nullable refreshedInstallation) {
  288. return [self saveInstallation:refreshedInstallation];
  289. }];
  290. }
  291. - (id)regenerateFIDOnRefreshTokenErrorIfNeeded:(NSError *)error {
  292. if (![error isKindOfClass:[FIRInstallationsHTTPError class]]) {
  293. // No recovery possible. Return the same error.
  294. return error;
  295. }
  296. FIRInstallationsHTTPError *HTTPError = (FIRInstallationsHTTPError *)error;
  297. switch (HTTPError.HTTPResponse.statusCode) {
  298. case FIRInstallationsAuthTokenHTTPCodeInvalidAuthentication:
  299. case FIRInstallationsAuthTokenHTTPCodeFIDNotFound:
  300. // The stored installation was damaged or blocked by the server.
  301. // Delete the stored installation then generate and register a new one.
  302. return [self getInstallationItem]
  303. .then(^FBLPromise<NSNull *> *(FIRInstallationsItem *installation) {
  304. return [self deleteInstallationLocally:installation];
  305. })
  306. .then(^FBLPromise<FIRInstallationsItem *> *(id result) {
  307. return [self installationWithValidAuthTokenForcingRefresh:NO];
  308. });
  309. default:
  310. // No recovery possible. Return the same error.
  311. return error;
  312. }
  313. }
  314. #pragma mark - Delete FID
  315. - (FBLPromise<NSNull *> *)deleteInstallation {
  316. return [self.deleteInstallationPromiseCache getExistingPendingOrCreateNewPromise];
  317. }
  318. - (FBLPromise<NSNull *> *)createDeleteInstallationPromise {
  319. FIRLogDebug(kFIRLoggerInstallations,
  320. kFIRInstallationsMessageCodeNewDeleteInstallationOperationCreated, @"%s, appName: %@",
  321. __PRETTY_FUNCTION__, self.appName);
  322. // Check for ongoing requests first, if there is no a request, then check local storage for
  323. // existing installation.
  324. FBLPromise<FIRInstallationsItem *> *currentInstallationPromise =
  325. [self mostRecentInstallationOperation] ?: [self getStoredInstallation];
  326. return currentInstallationPromise
  327. .then(^id(FIRInstallationsItem *installation) {
  328. return [self sendDeleteInstallationRequestIfNeeded:installation];
  329. })
  330. .then(^id(FIRInstallationsItem *installation) {
  331. // Remove the installation from the local storage.
  332. return [self deleteInstallationLocally:installation];
  333. });
  334. }
  335. - (FBLPromise<NSNull *> *)deleteInstallationLocally:(FIRInstallationsItem *)installation {
  336. return [self.installationsStore removeInstallationForAppID:installation.appID
  337. appName:installation.firebaseAppName]
  338. .then(^FBLPromise<NSNull *> *(NSNull *result) {
  339. return [self deleteExistingIIDIfNeeded];
  340. })
  341. .then(^NSNull *(NSNull *result) {
  342. [self postFIDDidChangeNotification];
  343. return result;
  344. });
  345. }
  346. - (FBLPromise<FIRInstallationsItem *> *)sendDeleteInstallationRequestIfNeeded:
  347. (FIRInstallationsItem *)installation {
  348. switch (installation.registrationStatus) {
  349. case FIRInstallationStatusUnknown:
  350. case FIRInstallationStatusUnregistered:
  351. // The installation is not registered, so it is safe to be deleted as is, so return early.
  352. return [FBLPromise resolvedWith:installation];
  353. break;
  354. case FIRInstallationStatusRegistered:
  355. // Proceed to de-register the installation on the server.
  356. break;
  357. }
  358. return [self.APIService deleteInstallation:installation].recover(^id(NSError *APIError) {
  359. if ([FIRInstallationsErrorUtil isAPIError:APIError withHTTPCode:404]) {
  360. // The installation was not found on the server.
  361. // Return success.
  362. return installation;
  363. } else {
  364. // Re-throw the error otherwise.
  365. return APIError;
  366. }
  367. });
  368. }
  369. - (FBLPromise<NSNull *> *)deleteExistingIIDIfNeeded {
  370. if ([self isDefaultApp]) {
  371. return [self.IIDStore deleteExistingIID];
  372. } else {
  373. return [FBLPromise resolvedWith:[NSNull null]];
  374. }
  375. }
  376. - (nullable FBLPromise<FIRInstallationsItem *> *)mostRecentInstallationOperation {
  377. return [self.authTokenForcingRefreshPromiseCache getExistingPendingPromise]
  378. ?: [self.authTokenPromiseCache getExistingPendingPromise]
  379. ?: [self.getInstallationPromiseCache getExistingPendingPromise];
  380. }
  381. #pragma mark - Notifications
  382. - (void)postFIDDidChangeNotification {
  383. [[NSNotificationCenter defaultCenter]
  384. postNotificationName:FIRInstallationIDDidChangeNotification
  385. object:nil
  386. userInfo:@{kFIRInstallationIDDidChangeNotificationAppNameKey : self.appName}];
  387. }
  388. #pragma mark - Default App
  389. - (BOOL)isDefaultApp {
  390. return [self.appName isEqualToString:kFIRDefaultAppName];
  391. }
  392. #pragma mark - Keychain
  393. + (NSString *)keychainServiceWithAppID:(NSString *)appID {
  394. #if TARGET_OS_MACCATALYST || TARGET_OS_OSX
  395. // We need to keep service name unique per application on macOS.
  396. // Applications on macOS may request access to Keychain items stored by other applications. It
  397. // means that when the app looks up for a relevant Keychain item in the service scope it will
  398. // request user password to grant access to the Keychain if there are other Keychain items from
  399. // other applications stored under the same Keychain Service.
  400. return [kKeychainService stringByAppendingFormat:@".%@", appID];
  401. #else
  402. // Use a constant Keychain service for non-macOS because:
  403. // 1. Keychain items cannot be shared between apps until configured specifically so the service
  404. // name collisions are not a concern
  405. // 2. We don't want to change the service name to avoid doing a migration.
  406. return kKeychainService;
  407. #endif
  408. }
  409. @end