FIRMessagingTokenInfo.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 "FirebaseMessaging/Sources/Token/FIRMessagingTokenInfo.h"
  17. #import "FirebaseMessaging/Sources/FIRMessagingConstants.h"
  18. #import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
  19. #import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
  20. /**
  21. * @enum Token Info Dictionary Key Constants
  22. * @discussion The keys that are checked when a token info is
  23. * created from a dictionary. The same keys are used
  24. * when decoding/encoding an archive.
  25. */
  26. /// Specifies a dictonary key whose value represents the authorized entity, or
  27. /// Sender ID for the token.
  28. static NSString *const kFIRInstanceIDAuthorizedEntityKey = @"authorized_entity";
  29. /// Specifies a dictionary key whose value represents the scope of the token,
  30. /// typically "*".
  31. static NSString *const kFIRInstanceIDScopeKey = @"scope";
  32. /// Specifies a dictionary key which represents the token value itself.
  33. static NSString *const kFIRInstanceIDTokenKey = @"token";
  34. /// Specifies a dictionary key which represents the app version associated
  35. /// with the token.
  36. static NSString *const kFIRInstanceIDAppVersionKey = @"app_version";
  37. /// Specifies a dictionary key which represents the GMP App ID associated with
  38. /// the token.
  39. static NSString *const kFIRInstanceIDFirebaseAppIDKey = @"firebase_app_id";
  40. /// Specifies a dictionary key representing an archive for a
  41. /// `FIRInstanceIDAPNSInfo` object.
  42. static NSString *const kFIRInstanceIDAPNSInfoKey = @"apns_info";
  43. /// Specifies a dictionary key representing the "last cached" time for the token.
  44. static NSString *const kFIRInstanceIDCacheTimeKey = @"cache_time";
  45. /// Default interval that token stays fresh.
  46. static const NSTimeInterval kDefaultFetchTokenInterval = 7 * 24 * 60 * 60; // 7 days.
  47. @implementation FIRMessagingTokenInfo
  48. - (instancetype)initWithAuthorizedEntity:(NSString *)authorizedEntity
  49. scope:(NSString *)scope
  50. token:(NSString *)token
  51. appVersion:(NSString *)appVersion
  52. firebaseAppID:(NSString *)firebaseAppID {
  53. self = [super init];
  54. if (self) {
  55. _authorizedEntity = [authorizedEntity copy];
  56. _scope = [scope copy];
  57. _token = [token copy];
  58. _appVersion = [appVersion copy];
  59. _firebaseAppID = [firebaseAppID copy];
  60. }
  61. return self;
  62. }
  63. - (BOOL)isFreshWithIID:(NSString *)IID {
  64. // Last fetch token cache time could be null if token is from legacy storage format. Then token is
  65. // considered not fresh and should be refreshed and overwrite with the latest storage format.
  66. if (!IID) {
  67. return NO;
  68. }
  69. if (!_cacheTime) {
  70. return NO;
  71. }
  72. // Check if it's consistent with IID
  73. if (![self.token hasPrefix:IID]) {
  74. return NO;
  75. }
  76. if ([self hasDenylistedScope]) {
  77. return NO;
  78. }
  79. // Check if app has just been updated to a new version.
  80. NSString *currentAppVersion = FIRMessagingCurrentAppVersion();
  81. if (!_appVersion || ![_appVersion isEqualToString:currentAppVersion]) {
  82. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeTokenManager004,
  83. @"Invalidating cached token for %@ (%@) due to app version change.",
  84. _authorizedEntity, _scope);
  85. return NO;
  86. }
  87. // Check if GMP App ID has changed
  88. NSString *currentFirebaseAppID = FIRMessagingFirebaseAppID();
  89. if (!_firebaseAppID || ![_firebaseAppID isEqualToString:currentFirebaseAppID]) {
  90. FIRMessagingLoggerDebug(
  91. kFIRMessagingMessageCodeTokenInfoFirebaseAppIDChanged,
  92. @"Invalidating cached token due to Firebase App IID change from %@ to %@", _firebaseAppID,
  93. currentFirebaseAppID);
  94. return NO;
  95. }
  96. // Check whether locale has changed, if yes, token needs to be updated with server for locale
  97. // information.
  98. if (FIRMessagingHasLocaleChanged()) {
  99. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeTokenInfoLocaleChanged,
  100. @"Invalidating cached token due to locale change");
  101. return NO;
  102. }
  103. // Locale is not changed, check whether token has been fetched within 7 days.
  104. NSTimeInterval lastFetchTokenTimestamp = [_cacheTime timeIntervalSince1970];
  105. NSTimeInterval currentTimestamp = FIRMessagingCurrentTimestampInSeconds();
  106. NSTimeInterval timeSinceLastFetchToken = currentTimestamp - lastFetchTokenTimestamp;
  107. return (timeSinceLastFetchToken < kDefaultFetchTokenInterval);
  108. }
  109. - (BOOL)hasDenylistedScope {
  110. /// The token with fiam scope is set by old FIAM SDK(s) which will remain in keychain for ever. So
  111. /// we need to remove these tokens to deny its usage.
  112. if ([self.scope isEqualToString:kFIRMessagingFIAMTokenScope]) {
  113. return YES;
  114. }
  115. return NO;
  116. }
  117. - (BOOL)isDefaultToken {
  118. return [self.scope isEqualToString:kFIRMessagingDefaultTokenScope];
  119. }
  120. #pragma mark - NSSecureCoding
  121. + (BOOL)supportsSecureCoding {
  122. return YES;
  123. }
  124. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
  125. // These value cannot be nil
  126. id authorizedEntity = [aDecoder decodeObjectForKey:kFIRInstanceIDAuthorizedEntityKey];
  127. if (![authorizedEntity isKindOfClass:[NSString class]]) {
  128. return nil;
  129. }
  130. id scope = [aDecoder decodeObjectForKey:kFIRInstanceIDScopeKey];
  131. if (![scope isKindOfClass:[NSString class]]) {
  132. return nil;
  133. }
  134. id token = [aDecoder decodeObjectForKey:kFIRInstanceIDTokenKey];
  135. if (![token isKindOfClass:[NSString class]]) {
  136. return nil;
  137. }
  138. // These values are nullable, so only fail the decode if the type does not match
  139. id appVersion = [aDecoder decodeObjectForKey:kFIRInstanceIDAppVersionKey];
  140. if (appVersion && ![appVersion isKindOfClass:[NSString class]]) {
  141. return nil;
  142. }
  143. id firebaseAppID = [aDecoder decodeObjectForKey:kFIRInstanceIDFirebaseAppIDKey];
  144. if (firebaseAppID && ![firebaseAppID isKindOfClass:[NSString class]]) {
  145. return nil;
  146. }
  147. NSSet *classes = [[NSSet alloc] initWithArray:@[ FIRMessagingAPNSInfo.class ]];
  148. FIRMessagingAPNSInfo *rawAPNSInfo = [aDecoder decodeObjectOfClasses:classes
  149. forKey:kFIRInstanceIDAPNSInfoKey];
  150. if (rawAPNSInfo && ![rawAPNSInfo isKindOfClass:[FIRMessagingAPNSInfo class]]) {
  151. return nil;
  152. }
  153. id cacheTime = [aDecoder decodeObjectForKey:kFIRInstanceIDCacheTimeKey];
  154. if (cacheTime && ![cacheTime isKindOfClass:[NSDate class]]) {
  155. return nil;
  156. }
  157. self = [super init];
  158. if (self) {
  159. _authorizedEntity = [authorizedEntity copy];
  160. _scope = [scope copy];
  161. _token = [token copy];
  162. _appVersion = [appVersion copy];
  163. _firebaseAppID = [firebaseAppID copy];
  164. _APNSInfo = [rawAPNSInfo copy];
  165. _cacheTime = cacheTime;
  166. }
  167. return self;
  168. }
  169. - (void)encodeWithCoder:(NSCoder *)aCoder {
  170. [aCoder encodeObject:self.authorizedEntity forKey:kFIRInstanceIDAuthorizedEntityKey];
  171. [aCoder encodeObject:self.scope forKey:kFIRInstanceIDScopeKey];
  172. [aCoder encodeObject:self.token forKey:kFIRInstanceIDTokenKey];
  173. [aCoder encodeObject:self.appVersion forKey:kFIRInstanceIDAppVersionKey];
  174. [aCoder encodeObject:self.firebaseAppID forKey:kFIRInstanceIDFirebaseAppIDKey];
  175. if (self.APNSInfo) {
  176. [aCoder encodeObject:self.APNSInfo forKey:kFIRInstanceIDAPNSInfoKey];
  177. }
  178. [aCoder encodeObject:self.cacheTime forKey:kFIRInstanceIDCacheTimeKey];
  179. }
  180. @end