Açıklama Yok

FIRInstanceIDTokenInfo.m 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "FIRInstanceIDLogger.h"
  18. #import "FIRInstanceIDUtilities.h"
  19. /**
  20. * @enum Token Info Dictionary Key Constants
  21. * @discussion The keys that are checked when a token info is
  22. * created from a dictionary. The same keys are used
  23. * when decoding/encoding an archive.
  24. */
  25. /// Specifies a dictonary key whose value represents the authorized entity, or
  26. /// Sender ID for the token.
  27. static NSString *const kFIRInstanceIDAuthorizedEntityKey = @"authorized_entity";
  28. /// Specifies a dictionary key whose value represents the scope of the token,
  29. /// typically "*".
  30. static NSString *const kFIRInstanceIDScopeKey = @"scope";
  31. /// Specifies a dictionary key which represents the token value itself.
  32. static NSString *const kFIRInstanceIDTokenKey = @"token";
  33. /// Specifies a dictionary key which represents the app version associated
  34. /// with the token.
  35. static NSString *const kFIRInstanceIDAppVersionKey = @"app_version";
  36. /// Specifies a dictionary key which represents the GMP App ID associated with
  37. /// the token.
  38. static NSString *const kFIRInstanceIDFirebaseAppIDKey = @"firebase_app_id";
  39. /// Specifies a dictionary key representing an archive for a
  40. /// `FIRInstanceIDAPNSInfo` object.
  41. static NSString *const kFIRInstanceIDAPNSInfoKey = @"apns_info";
  42. /// Specifies a dictionary key representing the "last cached" time for the token.
  43. static NSString *const kFIRInstanceIDCacheTimeKey = @"cache_time";
  44. /// Default interval that token stays fresh.
  45. const NSTimeInterval kDefaultFetchTokenInterval = 7 * 24 * 60 * 60; // 7 days.
  46. @implementation FIRInstanceIDTokenInfo
  47. - (instancetype)initWithAuthorizedEntity:(NSString *)authorizedEntity
  48. scope:(NSString *)scope
  49. token:(NSString *)token
  50. appVersion:(NSString *)appVersion
  51. firebaseAppID:(NSString *)firebaseAppID {
  52. self = [super init];
  53. if (self) {
  54. _authorizedEntity = [authorizedEntity copy];
  55. _scope = [scope copy];
  56. _token = [token copy];
  57. _appVersion = [appVersion copy];
  58. _firebaseAppID = [firebaseAppID copy];
  59. }
  60. return self;
  61. }
  62. - (BOOL)isFresh {
  63. // Last fetch token cache time could be null if token is from legacy storage format. Then token is
  64. // considered not fresh and should be refreshed and overwrite with the latest storage format.
  65. if (!_cacheTime) {
  66. return NO;
  67. }
  68. // Check if app has just been updated to a new version.
  69. NSString *currentAppVersion = FIRInstanceIDCurrentAppVersion();
  70. if (!_appVersion || ![_appVersion isEqualToString:currentAppVersion]) {
  71. FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeTokenManager004,
  72. @"Invalidating cached token for %@ (%@) due to app version change.",
  73. _authorizedEntity, _scope);
  74. return NO;
  75. }
  76. // Check if GMP App ID has changed
  77. NSString *currentFirebaseAppID = FIRInstanceIDFirebaseAppID();
  78. if (!_firebaseAppID || ![_firebaseAppID isEqualToString:currentFirebaseAppID]) {
  79. FIRInstanceIDLoggerDebug(
  80. kFIRInstanceIDMessageCodeTokenInfoFirebaseAppIDChanged,
  81. @"Invalidating cached token due to Firebase App IID change from %@ to %@", _firebaseAppID,
  82. currentFirebaseAppID);
  83. return NO;
  84. }
  85. // Check whether locale has changed, if yes, token needs to be updated with server for locale
  86. // information.
  87. if (FIRInstanceIDHasLocaleChanged()) {
  88. FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeTokenInfoLocaleChanged,
  89. @"Invalidating cached token due to locale change");
  90. return NO;
  91. }
  92. // Locale is not changed, check whether token has been fetched within 7 days.
  93. NSTimeInterval lastFetchTokenTimestamp = [_cacheTime timeIntervalSince1970];
  94. NSTimeInterval currentTimestamp = FIRInstanceIDCurrentTimestampInSeconds();
  95. NSTimeInterval timeSinceLastFetchToken = currentTimestamp - lastFetchTokenTimestamp;
  96. return (timeSinceLastFetchToken < kDefaultFetchTokenInterval);
  97. }
  98. #pragma mark - NSCoding
  99. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
  100. // These value cannot be nil
  101. id authorizedEntity = [aDecoder decodeObjectForKey:kFIRInstanceIDAuthorizedEntityKey];
  102. if (![authorizedEntity isKindOfClass:[NSString class]]) {
  103. return nil;
  104. }
  105. id scope = [aDecoder decodeObjectForKey:kFIRInstanceIDScopeKey];
  106. if (![scope isKindOfClass:[NSString class]]) {
  107. return nil;
  108. }
  109. id token = [aDecoder decodeObjectForKey:kFIRInstanceIDTokenKey];
  110. if (![token isKindOfClass:[NSString class]]) {
  111. return nil;
  112. }
  113. // These values are nullable, so only fail the decode if the type does not match
  114. id appVersion = [aDecoder decodeObjectForKey:kFIRInstanceIDAppVersionKey];
  115. if (appVersion && ![appVersion isKindOfClass:[NSString class]]) {
  116. return nil;
  117. }
  118. id firebaseAppID = [aDecoder decodeObjectForKey:kFIRInstanceIDFirebaseAppIDKey];
  119. if (firebaseAppID && ![firebaseAppID isKindOfClass:[NSString class]]) {
  120. return nil;
  121. }
  122. id rawAPNSInfo = [aDecoder decodeObjectForKey:kFIRInstanceIDAPNSInfoKey];
  123. if (rawAPNSInfo && ![rawAPNSInfo isKindOfClass:[NSData class]]) {
  124. return nil;
  125. }
  126. FIRInstanceIDAPNSInfo *APNSInfo = nil;
  127. if (rawAPNSInfo) {
  128. // TODO(chliangGoogle: Use the new API and secureCoding protocol.
  129. @try {
  130. #pragma clang diagnostic push
  131. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  132. APNSInfo = [NSKeyedUnarchiver unarchiveObjectWithData:rawAPNSInfo];
  133. #pragma clang diagnostic pop
  134. } @catch (NSException *exception) {
  135. FIRInstanceIDLoggerInfo(kFIRInstanceIDMessageCodeTokenInfoBadAPNSInfo,
  136. @"Could not parse raw APNS Info while parsing archived token info.");
  137. APNSInfo = nil;
  138. } @finally {
  139. }
  140. }
  141. id cacheTime = [aDecoder decodeObjectForKey:kFIRInstanceIDCacheTimeKey];
  142. if (cacheTime && ![cacheTime isKindOfClass:[NSDate class]]) {
  143. return nil;
  144. }
  145. self = [super init];
  146. if (self) {
  147. _authorizedEntity = authorizedEntity;
  148. _scope = scope;
  149. _token = token;
  150. _appVersion = appVersion;
  151. _firebaseAppID = firebaseAppID;
  152. _APNSInfo = APNSInfo;
  153. _cacheTime = cacheTime;
  154. }
  155. return self;
  156. }
  157. - (void)encodeWithCoder:(NSCoder *)aCoder {
  158. [aCoder encodeObject:self.authorizedEntity forKey:kFIRInstanceIDAuthorizedEntityKey];
  159. [aCoder encodeObject:self.scope forKey:kFIRInstanceIDScopeKey];
  160. [aCoder encodeObject:self.token forKey:kFIRInstanceIDTokenKey];
  161. [aCoder encodeObject:self.appVersion forKey:kFIRInstanceIDAppVersionKey];
  162. [aCoder encodeObject:self.firebaseAppID forKey:kFIRInstanceIDFirebaseAppIDKey];
  163. NSData *rawAPNSInfo;
  164. if (self.APNSInfo) {
  165. // TODO(chliangGoogle: Use the new API and secureCoding protocol.
  166. #pragma clang diagnostic push
  167. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  168. rawAPNSInfo = [NSKeyedArchiver archivedDataWithRootObject:self.APNSInfo];
  169. #pragma clang diagnostic pop
  170. [aCoder encodeObject:rawAPNSInfo forKey:kFIRInstanceIDAPNSInfoKey];
  171. }
  172. [aCoder encodeObject:self.cacheTime forKey:kFIRInstanceIDCacheTimeKey];
  173. }
  174. @end