FIRInstanceIDTokenManager.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 "Firebase/InstanceID/FIRInstanceIDTokenManager.h"
  17. #import "Firebase/InstanceID/FIRInstanceIDAuthKeyChain.h"
  18. #import "Firebase/InstanceID/FIRInstanceIDAuthService.h"
  19. #import "Firebase/InstanceID/FIRInstanceIDConstants.h"
  20. #import "Firebase/InstanceID/FIRInstanceIDDefines.h"
  21. #import "Firebase/InstanceID/FIRInstanceIDLogger.h"
  22. #import "Firebase/InstanceID/FIRInstanceIDStore.h"
  23. #import "Firebase/InstanceID/FIRInstanceIDTokenDeleteOperation.h"
  24. #import "Firebase/InstanceID/FIRInstanceIDTokenFetchOperation.h"
  25. #import "Firebase/InstanceID/FIRInstanceIDTokenInfo.h"
  26. #import "Firebase/InstanceID/FIRInstanceIDTokenOperation.h"
  27. #import "Firebase/InstanceID/NSError+FIRInstanceID.h"
  28. #import "Firebase/InstanceID/Private/FIRInstanceIDCheckinPreferences.h"
  29. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  30. @interface FIRInstanceIDTokenManager () <FIRInstanceIDStoreDelegate>
  31. @property(nonatomic, readwrite, strong) FIRInstanceIDStore *instanceIDStore;
  32. @property(nonatomic, readwrite, strong) FIRInstanceIDAuthService *authService;
  33. @property(nonatomic, readonly, strong) NSOperationQueue *tokenOperations;
  34. @property(nonatomic, readwrite, strong) FIRInstanceIDAPNSInfo *currentAPNSInfo;
  35. @end
  36. @implementation FIRInstanceIDTokenManager
  37. - (instancetype)init {
  38. self = [super init];
  39. if (self) {
  40. _instanceIDStore = [[FIRInstanceIDStore alloc] initWithDelegate:self];
  41. _authService = [[FIRInstanceIDAuthService alloc] initWithStore:_instanceIDStore];
  42. [self configureTokenOperations];
  43. }
  44. return self;
  45. }
  46. - (void)dealloc {
  47. [self stopAllTokenOperations];
  48. }
  49. - (void)configureTokenOperations {
  50. _tokenOperations = [[NSOperationQueue alloc] init];
  51. _tokenOperations.name = @"com.google.iid-token-operations";
  52. // For now, restrict the operations to be serial, because in some cases (like if the
  53. // authorized entity and scope are the same), order matters.
  54. // If we have to deal with several different token requests simultaneously, it would be a good
  55. // idea to add some better intelligence around this (performing unrelated token operations
  56. // simultaneously, etc.).
  57. _tokenOperations.maxConcurrentOperationCount = 1;
  58. if ([_tokenOperations respondsToSelector:@selector(qualityOfService)]) {
  59. _tokenOperations.qualityOfService = NSOperationQualityOfServiceUtility;
  60. }
  61. }
  62. - (void)fetchNewTokenWithAuthorizedEntity:(NSString *)authorizedEntity
  63. scope:(NSString *)scope
  64. instanceID:(NSString *)instanceID
  65. options:(NSDictionary *)options
  66. handler:(FIRInstanceIDTokenHandler)handler {
  67. FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeTokenManager000,
  68. @"Fetch new token for authorizedEntity: %@, scope: %@", authorizedEntity,
  69. scope);
  70. FIRInstanceIDTokenFetchOperation *operation =
  71. [self createFetchOperationWithAuthorizedEntity:authorizedEntity
  72. scope:scope
  73. options:options
  74. instanceID:instanceID];
  75. FIRInstanceID_WEAKIFY(self);
  76. FIRInstanceIDTokenOperationCompletion completion =
  77. ^(FIRInstanceIDTokenOperationResult result, NSString *_Nullable token,
  78. NSError *_Nullable error) {
  79. FIRInstanceID_STRONGIFY(self);
  80. if (error) {
  81. if (handler) {
  82. handler(nil, error);
  83. }
  84. return;
  85. }
  86. NSString *firebaseAppID = options[kFIRInstanceIDTokenOptionsFirebaseAppIDKey];
  87. FIRInstanceIDTokenInfo *tokenInfo = [[FIRInstanceIDTokenInfo alloc]
  88. initWithAuthorizedEntity:authorizedEntity
  89. scope:scope
  90. token:token
  91. appVersion:FIRInstanceIDCurrentAppVersion()
  92. firebaseAppID:firebaseAppID];
  93. tokenInfo.APNSInfo = [[FIRInstanceIDAPNSInfo alloc] initWithTokenOptionsDictionary:options];
  94. [self.instanceIDStore
  95. saveTokenInfo:tokenInfo
  96. handler:^(NSError *error) {
  97. if (!error) {
  98. // Do not send the token back in case the save was unsuccessful. Since with
  99. // the new asychronous fetch mechanism this can lead to infinite loops, for
  100. // example, we will return a valid token even though we weren't able to store
  101. // it in our cache. The first token will lead to a onTokenRefresh callback
  102. // wherein the user again calls `getToken` but since we weren't able to save
  103. // it we won't hit the cache but hit the server again leading to an infinite
  104. // loop.
  105. FIRInstanceIDLoggerDebug(
  106. kFIRInstanceIDMessageCodeTokenManager001,
  107. @"Token fetch successful, token: %@, authorizedEntity: %@, scope:%@",
  108. token, authorizedEntity, scope);
  109. if (handler) {
  110. handler(token, nil);
  111. }
  112. } else {
  113. if (handler) {
  114. handler(nil, error);
  115. }
  116. }
  117. }];
  118. };
  119. // Add completion handler, and ensure it's called on the main queue
  120. [operation addCompletionHandler:^(FIRInstanceIDTokenOperationResult result,
  121. NSString *_Nullable token, NSError *_Nullable error) {
  122. dispatch_async(dispatch_get_main_queue(), ^{
  123. completion(result, token, error);
  124. });
  125. }];
  126. [self.tokenOperations addOperation:operation];
  127. }
  128. - (FIRInstanceIDTokenInfo *)cachedTokenInfoWithAuthorizedEntity:(NSString *)authorizedEntity
  129. scope:(NSString *)scope {
  130. return [self.instanceIDStore tokenInfoWithAuthorizedEntity:authorizedEntity scope:scope];
  131. }
  132. - (void)deleteTokenWithAuthorizedEntity:(NSString *)authorizedEntity
  133. scope:(NSString *)scope
  134. instanceID:(NSString *)instanceID
  135. handler:(FIRInstanceIDDeleteTokenHandler)handler {
  136. if ([self.instanceIDStore tokenInfoWithAuthorizedEntity:authorizedEntity scope:scope]) {
  137. [self.instanceIDStore removeCachedTokenWithAuthorizedEntity:authorizedEntity scope:scope];
  138. }
  139. // Does not matter if we cannot find it in the cache. Still make an effort to unregister
  140. // from the server.
  141. FIRInstanceIDCheckinPreferences *checkinPreferences = self.authService.checkinPreferences;
  142. FIRInstanceIDTokenDeleteOperation *operation =
  143. [self createDeleteOperationWithAuthorizedEntity:authorizedEntity
  144. scope:scope
  145. checkinPreferences:checkinPreferences
  146. instanceID:instanceID
  147. action:FIRInstanceIDTokenActionDeleteToken];
  148. if (handler) {
  149. [operation addCompletionHandler:^(FIRInstanceIDTokenOperationResult result,
  150. NSString *_Nullable token, NSError *_Nullable error) {
  151. dispatch_async(dispatch_get_main_queue(), ^{
  152. handler(error);
  153. });
  154. }];
  155. }
  156. [self.tokenOperations addOperation:operation];
  157. }
  158. - (void)deleteAllTokensWithInstanceID:(NSString *)instanceID
  159. handler:(FIRInstanceIDDeleteHandler)handler {
  160. // delete all tokens
  161. FIRInstanceIDCheckinPreferences *checkinPreferences = self.authService.checkinPreferences;
  162. if (!checkinPreferences) {
  163. // The checkin is already deleted. No need to trigger the token delete operation as client no
  164. // longer has the checkin information for server to delete.
  165. dispatch_async(dispatch_get_main_queue(), ^{
  166. handler(nil);
  167. });
  168. return;
  169. }
  170. FIRInstanceIDTokenDeleteOperation *operation =
  171. [self createDeleteOperationWithAuthorizedEntity:kFIRInstanceIDKeychainWildcardIdentifier
  172. scope:kFIRInstanceIDKeychainWildcardIdentifier
  173. checkinPreferences:checkinPreferences
  174. instanceID:instanceID
  175. action:FIRInstanceIDTokenActionDeleteTokenAndIID];
  176. if (handler) {
  177. [operation addCompletionHandler:^(FIRInstanceIDTokenOperationResult result,
  178. NSString *_Nullable token, NSError *_Nullable error) {
  179. dispatch_async(dispatch_get_main_queue(), ^{
  180. handler(error);
  181. });
  182. }];
  183. }
  184. [self.tokenOperations addOperation:operation];
  185. }
  186. - (void)deleteAllTokensLocallyWithHandler:(void (^)(NSError *error))handler {
  187. [self.instanceIDStore removeAllCachedTokensWithHandler:handler];
  188. }
  189. - (void)stopAllTokenOperations {
  190. [self.authService stopCheckinRequest];
  191. [self.tokenOperations cancelAllOperations];
  192. }
  193. #pragma mark - FIRInstanceIDStoreDelegate
  194. - (void)store:(FIRInstanceIDStore *)store
  195. didDeleteFCMScopedTokensForCheckin:(FIRInstanceIDCheckinPreferences *)checkin {
  196. // Make a best effort try to delete the old client related state on the FCM server. This is
  197. // required to delete old pubusb registrations which weren't cleared when the app was deleted.
  198. //
  199. // This is only a one time effort. If this call fails the client would still receive duplicate
  200. // pubsub notifications if he is again subscribed to the same topic.
  201. //
  202. // The client state should be cleared on the server for the provided checkin preferences.
  203. FIRInstanceIDTokenDeleteOperation *operation =
  204. [self createDeleteOperationWithAuthorizedEntity:nil
  205. scope:nil
  206. checkinPreferences:checkin
  207. instanceID:nil
  208. action:FIRInstanceIDTokenActionDeleteToken];
  209. [operation addCompletionHandler:^(FIRInstanceIDTokenOperationResult result,
  210. NSString *_Nullable token, NSError *_Nullable error) {
  211. if (error) {
  212. FIRInstanceIDMessageCode code =
  213. kFIRInstanceIDMessageCodeTokenManagerErrorDeletingFCMTokensOnAppReset;
  214. FIRInstanceIDLoggerDebug(code, @"Failed to delete GCM server registrations on app reset.");
  215. } else {
  216. FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeTokenManagerDeletedFCMTokensOnAppReset,
  217. @"Successfully deleted GCM server registrations on app reset");
  218. }
  219. }];
  220. [self.tokenOperations addOperation:operation];
  221. }
  222. #pragma mark - Unit Testing Stub Helpers
  223. // We really have this method so that we can more easily stub it out for unit testing
  224. - (FIRInstanceIDTokenFetchOperation *)
  225. createFetchOperationWithAuthorizedEntity:(NSString *)authorizedEntity
  226. scope:(NSString *)scope
  227. options:(NSDictionary<NSString *, NSString *> *)options
  228. instanceID:(NSString *)instanceID {
  229. FIRInstanceIDCheckinPreferences *checkinPreferences = self.authService.checkinPreferences;
  230. FIRInstanceIDTokenFetchOperation *operation =
  231. [[FIRInstanceIDTokenFetchOperation alloc] initWithAuthorizedEntity:authorizedEntity
  232. scope:scope
  233. options:options
  234. checkinPreferences:checkinPreferences
  235. instanceID:instanceID];
  236. return operation;
  237. }
  238. // We really have this method so that we can more easily stub it out for unit testing
  239. - (FIRInstanceIDTokenDeleteOperation *)
  240. createDeleteOperationWithAuthorizedEntity:(NSString *)authorizedEntity
  241. scope:(NSString *)scope
  242. checkinPreferences:(FIRInstanceIDCheckinPreferences *)checkinPreferences
  243. instanceID:(NSString *)instanceID
  244. action:(FIRInstanceIDTokenAction)action {
  245. FIRInstanceIDTokenDeleteOperation *operation =
  246. [[FIRInstanceIDTokenDeleteOperation alloc] initWithAuthorizedEntity:authorizedEntity
  247. scope:scope
  248. checkinPreferences:checkinPreferences
  249. instanceID:instanceID
  250. action:action];
  251. return operation;
  252. }
  253. #pragma mark - Invalidating Cached Tokens
  254. - (BOOL)checkTokenRefreshPolicyWithIID:(NSString *)IID {
  255. // We know at least one cached token exists.
  256. BOOL shouldFetchDefaultToken = NO;
  257. NSArray<FIRInstanceIDTokenInfo *> *tokenInfos = [self.instanceIDStore cachedTokenInfos];
  258. NSMutableArray<FIRInstanceIDTokenInfo *> *tokenInfosToDelete =
  259. [NSMutableArray arrayWithCapacity:tokenInfos.count];
  260. for (FIRInstanceIDTokenInfo *tokenInfo in tokenInfos) {
  261. if ([tokenInfo isFreshWithIID:IID]) {
  262. // Token is fresh and in right format, do nothing
  263. continue;
  264. }
  265. if ([tokenInfo isDefaultToken]) {
  266. // Default token is expired, do not mark for deletion. Fetch directly from server to
  267. // replace the current one.
  268. shouldFetchDefaultToken = YES;
  269. } else {
  270. // Non-default token is expired, mark for deletion.
  271. [tokenInfosToDelete addObject:tokenInfo];
  272. }
  273. FIRInstanceIDLoggerDebug(
  274. kFIRInstanceIDMessageCodeTokenManagerInvalidateStaleToken,
  275. @"Invalidating cached token for %@ (%@) due to token is no longer fresh.",
  276. tokenInfo.authorizedEntity, tokenInfo.scope);
  277. }
  278. for (FIRInstanceIDTokenInfo *tokenInfoToDelete in tokenInfosToDelete) {
  279. [self.instanceIDStore removeCachedTokenWithAuthorizedEntity:tokenInfoToDelete.authorizedEntity
  280. scope:tokenInfoToDelete.scope];
  281. }
  282. return shouldFetchDefaultToken;
  283. }
  284. - (NSArray<FIRInstanceIDTokenInfo *> *)updateTokensToAPNSDeviceToken:(NSData *)deviceToken
  285. isSandbox:(BOOL)isSandbox {
  286. // Each cached IID token that is missing an APNSInfo, or has an APNSInfo associated should be
  287. // checked and invalidated if needed.
  288. FIRInstanceIDAPNSInfo *APNSInfo = [[FIRInstanceIDAPNSInfo alloc] initWithDeviceToken:deviceToken
  289. isSandbox:isSandbox];
  290. if ([self.currentAPNSInfo isEqualToAPNSInfo:APNSInfo]) {
  291. return @[];
  292. }
  293. self.currentAPNSInfo = APNSInfo;
  294. NSArray<FIRInstanceIDTokenInfo *> *tokenInfos = [self.instanceIDStore cachedTokenInfos];
  295. NSMutableArray<FIRInstanceIDTokenInfo *> *tokenInfosToDelete =
  296. [NSMutableArray arrayWithCapacity:tokenInfos.count];
  297. for (FIRInstanceIDTokenInfo *cachedTokenInfo in tokenInfos) {
  298. // Check if the cached APNSInfo is nil, or if it is an old APNSInfo.
  299. if (!cachedTokenInfo.APNSInfo ||
  300. ![cachedTokenInfo.APNSInfo isEqualToAPNSInfo:self.currentAPNSInfo]) {
  301. // Mark for invalidation.
  302. [tokenInfosToDelete addObject:cachedTokenInfo];
  303. }
  304. }
  305. for (FIRInstanceIDTokenInfo *tokenInfoToDelete in tokenInfosToDelete) {
  306. FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeTokenManagerAPNSChangedTokenInvalidated,
  307. @"Invalidating cached token for %@ (%@) due to APNs token change.",
  308. tokenInfoToDelete.authorizedEntity, tokenInfoToDelete.scope);
  309. [self.instanceIDStore removeCachedTokenWithAuthorizedEntity:tokenInfoToDelete.authorizedEntity
  310. scope:tokenInfoToDelete.scope];
  311. }
  312. return tokenInfosToDelete;
  313. }
  314. - (void)saveDefaultToken:(NSString *)defaultToken withOptions:(NSDictionary *)tokenOptions {
  315. FIROptions *options = FIRApp.defaultApp.options;
  316. FIRInstanceIDTokenInfo *tokenInfo =
  317. [[FIRInstanceIDTokenInfo alloc] initWithAuthorizedEntity:options.GCMSenderID
  318. scope:@"*"
  319. token:defaultToken
  320. appVersion:FIRInstanceIDCurrentAppVersion()
  321. firebaseAppID:options.googleAppID];
  322. tokenInfo.APNSInfo = [[FIRInstanceIDAPNSInfo alloc] initWithTokenOptionsDictionary:tokenOptions];
  323. [self.instanceIDStore saveTokenInfoInCacheOnly:tokenInfo];
  324. }
  325. @end