Nenhuma descrição

FIRInstanceIDTokenInfo.m 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "FIRInstanceIDTokenInfo.h"
  17. #import "FIRInstanceIDConstants.h"
  18. #import "FIRInstanceIDLogger.h"
  19. #import "FIRInstanceIDUtilities.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. const NSTimeInterval kDefaultFetchTokenInterval = 7 * 24 * 60 * 60; // 7 days.
  47. @implementation FIRInstanceIDTokenInfo
  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. // Check if app has just been updated to a new version.
  77. NSString *currentAppVersion = FIRInstanceIDCurrentAppVersion();
  78. if (!_appVersion || ![_appVersion isEqualToString:currentAppVersion]) {
  79. FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeTokenManager004,
  80. @"Invalidating cached token for %@ (%@) due to app version change.",
  81. _authorizedEntity, _scope);
  82. return NO;
  83. }
  84. // Check if GMP App ID has changed
  85. NSString *currentFirebaseAppID = FIRInstanceIDFirebaseAppID();
  86. if (!_firebaseAppID || ![_firebaseAppID isEqualToString:currentFirebaseAppID]) {
  87. FIRInstanceIDLoggerDebug(
  88. kFIRInstanceIDMessageCodeTokenInfoFirebaseAppIDChanged,
  89. @"Invalidating cached token due to Firebase App IID change from %@ to %@", _firebaseAppID,
  90. currentFirebaseAppID);
  91. return NO;
  92. }
  93. // Check whether locale has changed, if yes, token needs to be updated with server for locale
  94. // information.
  95. if (FIRInstanceIDHasLocaleChanged()) {
  96. FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeTokenInfoLocaleChanged,
  97. @"Invalidating cached token due to locale change");
  98. return NO;
  99. }
  100. // Locale is not changed, check whether token has been fetched within 7 days.
  101. NSTimeInterval lastFetchTokenTimestamp = [_cacheTime timeIntervalSince1970];
  102. NSTimeInterval currentTimestamp = FIRInstanceIDCurrentTimestampInSeconds();
  103. NSTimeInterval timeSinceLastFetchToken = currentTimestamp - lastFetchTokenTimestamp;
  104. return (timeSinceLastFetchToken < kDefaultFetchTokenInterval);
  105. }
  106. - (BOOL)isDefaultToken {
  107. return [self.scope isEqualToString:kFIRInstanceIDDefaultTokenScope];
  108. }
  109. #pragma mark - NSCoding
  110. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
  111. // These value cannot be nil
  112. id authorizedEntity = [aDecoder decodeObjectForKey:kFIRInstanceIDAuthorizedEntityKey];
  113. if (![authorizedEntity isKindOfClass:[NSString class]]) {
  114. return nil;
  115. }
  116. id scope = [aDecoder decodeObjectForKey:kFIRInstanceIDScopeKey];
  117. if (![scope isKindOfClass:[NSString class]]) {
  118. return nil;
  119. }
  120. id token = [aDecoder decodeObjectForKey:kFIRInstanceIDTokenKey];
  121. if (![token isKindOfClass:[NSString class]]) {
  122. return nil;
  123. }
  124. // These values are nullable, so only fail the decode if the type does not match
  125. id appVersion = [aDecoder decodeObjectForKey:kFIRInstanceIDAppVersionKey];
  126. if (appVersion && ![appVersion isKindOfClass:[NSString class]]) {
  127. return nil;
  128. }
  129. id firebaseAppID = [aDecoder decodeObjectForKey:kFIRInstanceIDFirebaseAppIDKey];
  130. if (firebaseAppID && ![firebaseAppID isKindOfClass:[NSString class]]) {
  131. return nil;
  132. }
  133. id rawAPNSInfo = [aDecoder decodeObjectForKey:kFIRInstanceIDAPNSInfoKey];
  134. if (rawAPNSInfo && ![rawAPNSInfo isKindOfClass:[NSData class]]) {
  135. return nil;
  136. }
  137. FIRInstanceIDAPNSInfo *APNSInfo = nil;
  138. if (rawAPNSInfo) {
  139. // TODO(chliangGoogle: Use the new API and secureCoding protocol.
  140. @try {
  141. #pragma clang diagnostic push
  142. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  143. APNSInfo = [NSKeyedUnarchiver unarchiveObjectWithData:rawAPNSInfo];
  144. #pragma clang diagnostic pop
  145. } @catch (NSException *exception) {
  146. FIRInstanceIDLoggerInfo(kFIRInstanceIDMessageCodeTokenInfoBadAPNSInfo,
  147. @"Could not parse raw APNS Info while parsing archived token info.");
  148. APNSInfo = nil;
  149. } @finally {
  150. }
  151. }
  152. id cacheTime = [aDecoder decodeObjectForKey:kFIRInstanceIDCacheTimeKey];
  153. if (cacheTime && ![cacheTime isKindOfClass:[NSDate class]]) {
  154. return nil;
  155. }
  156. self = [super init];
  157. if (self) {
  158. _authorizedEntity = authorizedEntity;
  159. _scope = scope;
  160. _token = token;
  161. _appVersion = appVersion;
  162. _firebaseAppID = firebaseAppID;
  163. _APNSInfo = APNSInfo;
  164. _cacheTime = cacheTime;
  165. }
  166. return self;
  167. }
  168. - (void)encodeWithCoder:(NSCoder *)aCoder {
  169. [aCoder encodeObject:self.authorizedEntity forKey:kFIRInstanceIDAuthorizedEntityKey];
  170. [aCoder encodeObject:self.scope forKey:kFIRInstanceIDScopeKey];
  171. [aCoder encodeObject:self.token forKey:kFIRInstanceIDTokenKey];
  172. [aCoder encodeObject:self.appVersion forKey:kFIRInstanceIDAppVersionKey];
  173. [aCoder encodeObject:self.firebaseAppID forKey:kFIRInstanceIDFirebaseAppIDKey];
  174. NSData *rawAPNSInfo;
  175. if (self.APNSInfo) {
  176. // TODO(chliangGoogle: Use the new API and secureCoding protocol.
  177. #pragma clang diagnostic push
  178. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  179. rawAPNSInfo = [NSKeyedArchiver archivedDataWithRootObject:self.APNSInfo];
  180. #pragma clang diagnostic pop
  181. [aCoder encodeObject:rawAPNSInfo forKey:kFIRInstanceIDAPNSInfoKey];
  182. }
  183. [aCoder encodeObject:self.cacheTime forKey:kFIRInstanceIDCacheTimeKey];
  184. }
  185. @end