FIRInstallationsIDController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 <FirebaseCore/FIRAppInternal.h>
  23. #import "FIRInstallationsAPIService.h"
  24. #import "FIRInstallationsErrorUtil.h"
  25. #import "FIRInstallationsIIDStore.h"
  26. #import "FIRInstallationsItem.h"
  27. #import "FIRInstallationsLogger.h"
  28. #import "FIRInstallationsSingleOperationPromiseCache.h"
  29. #import "FIRInstallationsStore.h"
  30. #import "FIRInstallationsStoredAuthToken.h"
  31. #import "FIRSecureStorage.h"
  32. const NSNotificationName FIRInstallationIDDidChangeNotification =
  33. @"FIRInstallationIDDidChangeNotification";
  34. NSString *const kFIRInstallationIDDidChangeNotificationAppNameKey =
  35. @"FIRInstallationIDDidChangeNotification";
  36. NSTimeInterval const kFIRInstallationsTokenExpirationThreshold = 60 * 60; // 1 hour.
  37. @interface FIRInstallationsIDController ()
  38. @property(nonatomic, readonly) NSString *appID;
  39. @property(nonatomic, readonly) NSString *appName;
  40. @property(nonatomic, readonly) FIRInstallationsStore *installationsStore;
  41. @property(nonatomic, readonly) FIRInstallationsIIDStore *IIDStore;
  42. @property(nonatomic, readonly) FIRInstallationsAPIService *APIService;
  43. @property(nonatomic, readonly) FIRInstallationsSingleOperationPromiseCache<FIRInstallationsItem *>
  44. *getInstallationPromiseCache;
  45. @property(nonatomic, readonly)
  46. FIRInstallationsSingleOperationPromiseCache<FIRInstallationsItem *> *authTokenPromiseCache;
  47. @property(nonatomic, readonly) FIRInstallationsSingleOperationPromiseCache<FIRInstallationsItem *>
  48. *authTokenForcingRefreshPromiseCache;
  49. @property(nonatomic, readonly)
  50. FIRInstallationsSingleOperationPromiseCache<NSNull *> *deleteInstallationPromiseCache;
  51. @end
  52. @implementation FIRInstallationsIDController
  53. - (instancetype)initWithGoogleAppID:(NSString *)appID
  54. appName:(NSString *)appName
  55. APIKey:(NSString *)APIKey
  56. projectID:(NSString *)projectID {
  57. FIRSecureStorage *secureStorage = [[FIRSecureStorage alloc] init];
  58. FIRInstallationsStore *installationsStore =
  59. [[FIRInstallationsStore alloc] initWithSecureStorage:secureStorage accessGroup:nil];
  60. FIRInstallationsAPIService *apiService =
  61. [[FIRInstallationsAPIService alloc] initWithAPIKey:APIKey projectID:projectID];
  62. FIRInstallationsIIDStore *IIDStore = [[FIRInstallationsIIDStore alloc] init];
  63. return [self initWithGoogleAppID:appID
  64. appName:appName
  65. installationsStore:installationsStore
  66. APIService:apiService
  67. IIDStore:IIDStore];
  68. }
  69. /// The initializer is supposed to be used by tests to inject `installationsStore`.
  70. - (instancetype)initWithGoogleAppID:(NSString *)appID
  71. appName:(NSString *)appName
  72. installationsStore:(FIRInstallationsStore *)installationsStore
  73. APIService:(FIRInstallationsAPIService *)APIService
  74. IIDStore:(FIRInstallationsIIDStore *)IIDStore {
  75. self = [super init];
  76. if (self) {
  77. _appID = appID;
  78. _appName = appName;
  79. _installationsStore = installationsStore;
  80. _APIService = APIService;
  81. _IIDStore = IIDStore;
  82. __weak FIRInstallationsIDController *weakSelf = self;
  83. _getInstallationPromiseCache = [[FIRInstallationsSingleOperationPromiseCache alloc]
  84. initWithNewOperationHandler:^FBLPromise *_Nonnull {
  85. FIRInstallationsIDController *strongSelf = weakSelf;
  86. return [strongSelf createGetInstallationItemPromise];
  87. }];
  88. _authTokenPromiseCache = [[FIRInstallationsSingleOperationPromiseCache alloc]
  89. initWithNewOperationHandler:^FBLPromise *_Nonnull {
  90. FIRInstallationsIDController *strongSelf = weakSelf;
  91. return [strongSelf installationWithValidAuthTokenForcingRefresh:NO];
  92. }];
  93. _authTokenForcingRefreshPromiseCache = [[FIRInstallationsSingleOperationPromiseCache alloc]
  94. initWithNewOperationHandler:^FBLPromise *_Nonnull {
  95. FIRInstallationsIDController *strongSelf = weakSelf;
  96. return [strongSelf installationWithValidAuthTokenForcingRefresh:YES];
  97. }];
  98. _deleteInstallationPromiseCache = [[FIRInstallationsSingleOperationPromiseCache alloc]
  99. initWithNewOperationHandler:^FBLPromise *_Nonnull {
  100. FIRInstallationsIDController *strongSelf = weakSelf;
  101. return [strongSelf createDeleteInstallationPromise];
  102. }];
  103. }
  104. return self;
  105. }
  106. #pragma mark - Get Installation.
  107. - (FBLPromise<FIRInstallationsItem *> *)getInstallationItem {
  108. return [self.getInstallationPromiseCache getExistingPendingOrCreateNewPromise];
  109. }
  110. - (FBLPromise<FIRInstallationsItem *> *)createGetInstallationItemPromise {
  111. FIRLogDebug(kFIRLoggerInstallations,
  112. kFIRInstallationsMessageCodeNewGetInstallationOperationCreated, @"%s, appName: %@",
  113. __PRETTY_FUNCTION__, self.appName);
  114. FBLPromise<FIRInstallationsItem *> *installationItemPromise =
  115. [self getStoredInstallation].recover(^id(NSError *error) {
  116. return [self createAndSaveFID];
  117. });
  118. // Initiate registration process on success if needed, but return the installation without waiting
  119. // for it.
  120. installationItemPromise.then(^id(FIRInstallationsItem *installation) {
  121. [self getAuthTokenForcingRefresh:NO];
  122. return nil;
  123. });
  124. return installationItemPromise;
  125. }
  126. - (FBLPromise<FIRInstallationsItem *> *)getStoredInstallation {
  127. return [self.installationsStore installationForAppID:self.appID appName:self.appName].validate(
  128. ^BOOL(FIRInstallationsItem *installation) {
  129. BOOL isValid = NO;
  130. switch (installation.registrationStatus) {
  131. case FIRInstallationStatusUnregistered:
  132. case FIRInstallationStatusRegistered:
  133. isValid = YES;
  134. break;
  135. case FIRInstallationStatusUnknown:
  136. isValid = NO;
  137. break;
  138. }
  139. return isValid;
  140. });
  141. }
  142. - (FBLPromise<FIRInstallationsItem *> *)createAndSaveFID {
  143. return [self migrateOrGenerateFID]
  144. .then(^FBLPromise<FIRInstallationsItem *> *(NSString *FID) {
  145. return [self createAndSaveInstallationWithFID:FID];
  146. })
  147. .then(^FIRInstallationsItem *(FIRInstallationsItem *installation) {
  148. [self postFIDDidChangeNotification];
  149. return installation;
  150. });
  151. }
  152. - (FBLPromise<FIRInstallationsItem *> *)createAndSaveInstallationWithFID:(NSString *)FID {
  153. FIRInstallationsItem *installation = [[FIRInstallationsItem alloc] initWithAppID:self.appID
  154. firebaseAppName:self.appName];
  155. installation.firebaseInstallationID = FID;
  156. installation.registrationStatus = FIRInstallationStatusUnregistered;
  157. return [self.installationsStore saveInstallation:installation].then(^id(NSNull *result) {
  158. return installation;
  159. });
  160. }
  161. - (FBLPromise<NSString *> *)migrateOrGenerateFID {
  162. if (![self isDefaultApp]) {
  163. // Existing IID should be used only for default FirebaseApp.
  164. return [FBLPromise resolvedWith:[FIRInstallationsItem generateFID]];
  165. }
  166. return [self.IIDStore existingIID].recover(^NSString *(NSError *error) {
  167. return [FIRInstallationsItem generateFID];
  168. });
  169. }
  170. #pragma mark - FID registration
  171. - (FBLPromise<FIRInstallationsItem *> *)registerInstallationIfNeeded:
  172. (FIRInstallationsItem *)installation {
  173. switch (installation.registrationStatus) {
  174. case FIRInstallationStatusRegistered:
  175. // Already registered. Do nothing.
  176. return [FBLPromise resolvedWith:installation];
  177. case FIRInstallationStatusUnknown:
  178. case FIRInstallationStatusUnregistered:
  179. // Registration required. Proceed.
  180. break;
  181. }
  182. return [self.APIService registerInstallation:installation]
  183. .then(^id(FIRInstallationsItem *registeredInstallation) {
  184. // Expected successful result: @[FIRInstallationsItem *registeredInstallation, NSNull]
  185. return [FBLPromise all:@[
  186. registeredInstallation, [self.installationsStore saveInstallation:registeredInstallation]
  187. ]];
  188. })
  189. .then(^FIRInstallationsItem *(NSArray *result) {
  190. FIRInstallationsItem *registeredInstallation = result.firstObject;
  191. // Server may respond with a different FID if the sent one cannot be accepted.
  192. if (![registeredInstallation.firebaseInstallationID
  193. isEqualToString:installation.firebaseInstallationID]) {
  194. [self postFIDDidChangeNotification];
  195. }
  196. return registeredInstallation;
  197. });
  198. }
  199. #pragma mark - Auth Token
  200. - (FBLPromise<FIRInstallationsItem *> *)getAuthTokenForcingRefresh:(BOOL)forceRefresh {
  201. if (forceRefresh) {
  202. return [self.authTokenForcingRefreshPromiseCache getExistingPendingOrCreateNewPromise];
  203. } else {
  204. return [self.authTokenPromiseCache getExistingPendingOrCreateNewPromise];
  205. }
  206. }
  207. - (FBLPromise<FIRInstallationsItem *> *)installationWithValidAuthTokenForcingRefresh:
  208. (BOOL)forceRefresh {
  209. FIRLogDebug(kFIRLoggerInstallations, kFIRInstallationsMessageCodeNewGetAuthTokenOperationCreated,
  210. @"-[FIRInstallationsIDController installationWithValidAuthTokenForcingRefresh:%@], "
  211. @"appName: %@",
  212. @(forceRefresh), self.appName);
  213. return [self getInstallationItem]
  214. .then(^FBLPromise<FIRInstallationsItem *> *(FIRInstallationsItem *installation) {
  215. return [self registerInstallationIfNeeded:installation];
  216. })
  217. .then(^id(FIRInstallationsItem *registeredInstallation) {
  218. BOOL isTokenExpiredOrExpiresSoon =
  219. [registeredInstallation.authToken.expirationDate timeIntervalSinceDate:[NSDate date]] <
  220. kFIRInstallationsTokenExpirationThreshold;
  221. if (forceRefresh || isTokenExpiredOrExpiresSoon) {
  222. return [self refreshAuthTokenForInstallation:registeredInstallation];
  223. } else {
  224. return registeredInstallation;
  225. }
  226. })
  227. .catch(^void(NSError *error){
  228. // TODO: Handle the errors.
  229. });
  230. }
  231. - (FBLPromise<FIRInstallationsItem *> *)refreshAuthTokenForInstallation:
  232. (FIRInstallationsItem *)installation {
  233. return [FBLPromise attempts:1
  234. delay:1
  235. condition:^BOOL(NSInteger remainingAttempts, NSError *_Nonnull error) {
  236. return [FIRInstallationsErrorUtil isAPIError:error withHTTPCode:500];
  237. }
  238. retry:^id _Nullable {
  239. return [self.APIService refreshAuthTokenForInstallation:installation];
  240. }];
  241. }
  242. #pragma mark - Delete FID
  243. - (FBLPromise<NSNull *> *)deleteInstallation {
  244. return [self.deleteInstallationPromiseCache getExistingPendingOrCreateNewPromise];
  245. }
  246. - (FBLPromise<NSNull *> *)createDeleteInstallationPromise {
  247. FIRLogDebug(kFIRLoggerInstallations,
  248. kFIRInstallationsMessageCodeNewDeleteInstallationOperationCreated, @"%s, appName: %@",
  249. __PRETTY_FUNCTION__, self.appName);
  250. // Check for ongoing requests first, if there is no a request, then check local storage for
  251. // existing installation.
  252. FBLPromise<FIRInstallationsItem *> *currentInstallationPromise =
  253. [self mostRecentInstallationOperation] ?: [self getStoredInstallation];
  254. return currentInstallationPromise
  255. .then(^id(FIRInstallationsItem *installation) {
  256. return [self sendDeleteInstallationRequestIfNeeded:installation];
  257. })
  258. .then(^id(FIRInstallationsItem *installation) {
  259. // Remove the installation from the local storage.
  260. return [self.installationsStore removeInstallationForAppID:installation.appID
  261. appName:installation.firebaseAppName];
  262. })
  263. .then(^FBLPromise<NSNull *> *(NSNull *result) {
  264. return [self deleteExistingIIDIfNeeded];
  265. })
  266. .then(^NSNull *(NSNull *result) {
  267. [self postFIDDidChangeNotification];
  268. return result;
  269. });
  270. }
  271. - (FBLPromise<FIRInstallationsItem *> *)sendDeleteInstallationRequestIfNeeded:
  272. (FIRInstallationsItem *)installation {
  273. switch (installation.registrationStatus) {
  274. case FIRInstallationStatusUnknown:
  275. case FIRInstallationStatusUnregistered:
  276. // The installation is not registered, so it is safe to be deleted as is, so return early.
  277. return [FBLPromise resolvedWith:installation];
  278. break;
  279. case FIRInstallationStatusRegistered:
  280. // Proceed to de-register the installation on the server.
  281. break;
  282. }
  283. return [self.APIService deleteInstallation:installation].recover(^id(NSError *APIError) {
  284. if ([FIRInstallationsErrorUtil isAPIError:APIError withHTTPCode:404]) {
  285. // The installation was not found on the server.
  286. // Return success.
  287. return installation;
  288. } else {
  289. // Re-throw the error otherwise.
  290. return APIError;
  291. }
  292. });
  293. }
  294. - (FBLPromise<NSNull *> *)deleteExistingIIDIfNeeded {
  295. if ([self isDefaultApp]) {
  296. return [self.IIDStore deleteExistingIID];
  297. } else {
  298. return [FBLPromise resolvedWith:[NSNull null]];
  299. }
  300. }
  301. - (nullable FBLPromise<FIRInstallationsItem *> *)mostRecentInstallationOperation {
  302. return [self.authTokenForcingRefreshPromiseCache getExistingPendingPromise]
  303. ?: [self.authTokenPromiseCache getExistingPendingPromise]
  304. ?: [self.getInstallationPromiseCache getExistingPendingPromise];
  305. }
  306. #pragma mark - Notifications
  307. - (void)postFIDDidChangeNotification {
  308. [[NSNotificationCenter defaultCenter]
  309. postNotificationName:FIRInstallationIDDidChangeNotification
  310. object:nil
  311. userInfo:@{kFIRInstallationIDDidChangeNotificationAppNameKey : self.appName}];
  312. }
  313. #pragma mark - Default App
  314. - (BOOL)isDefaultApp {
  315. return [self.appName isEqualToString:kFIRDefaultAppName];
  316. }
  317. @end