FIRMessagingCheckinPreferences.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/FIRMessagingCheckinPreferences.h"
  17. #import <GoogleUtilities/GULUserDefaults.h>
  18. #import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
  19. #import "FirebaseMessaging/Sources/Token/FIRMessagingCheckinService.h"
  20. const NSTimeInterval kFIRMessagingDefaultCheckinInterval = 7 * 24 * 60 * 60; // 7 days.
  21. static NSString *const kCheckinKeychainContentSeparatorString = @"|";
  22. @interface FIRMessagingCheckinPreferences ()
  23. @property(nonatomic, readwrite, copy) NSString *deviceID;
  24. @property(nonatomic, readwrite, copy) NSString *secretToken;
  25. @property(nonatomic, readwrite, copy) NSString *digest;
  26. @property(nonatomic, readwrite, copy) NSString *versionInfo;
  27. @property(nonatomic, readwrite, copy) NSString *deviceDataVersion;
  28. @property(nonatomic, readwrite, strong) NSMutableDictionary *gServicesData;
  29. @property(nonatomic, readwrite, assign) int64_t lastCheckinTimestampMillis;
  30. // This flag indicates that we have already saved the above deviceID and secret
  31. // to our keychain and hence we don't need to save again. This is helpful since
  32. // on checkin refresh we can avoid writing to the Keychain which can sometimes
  33. // be very buggy. For info check this https://forums.developer.apple.com/thread/4743
  34. @property(nonatomic, readwrite, assign) BOOL hasPreCachedAuthCredentials;
  35. @end
  36. @implementation FIRMessagingCheckinPreferences
  37. + (FIRMessagingCheckinPreferences *)preferencesFromKeychainContents:(NSString *)keychainContent {
  38. NSString *deviceID = [self checkinDeviceIDFromKeychainContent:keychainContent];
  39. NSString *secret = [self checkinSecretFromKeychainContent:keychainContent];
  40. if ([deviceID length] && [secret length]) {
  41. return [[FIRMessagingCheckinPreferences alloc] initWithDeviceID:deviceID secretToken:secret];
  42. } else {
  43. return nil;
  44. }
  45. }
  46. - (instancetype)initWithDeviceID:(NSString *)deviceID secretToken:(NSString *)secretToken {
  47. self = [super init];
  48. if (self) {
  49. self.deviceID = [deviceID copy];
  50. self.secretToken = [secretToken copy];
  51. }
  52. return self;
  53. }
  54. - (void)reset {
  55. self.deviceID = nil;
  56. self.secretToken = nil;
  57. self.digest = nil;
  58. self.versionInfo = nil;
  59. self.gServicesData = nil;
  60. self.deviceDataVersion = nil;
  61. self.lastCheckinTimestampMillis = 0;
  62. }
  63. - (NSDictionary *)checkinPlistContents {
  64. NSMutableDictionary *checkinPlistContents = [NSMutableDictionary dictionary];
  65. checkinPlistContents[kFIRMessagingDigestStringKey] = self.digest ?: @"";
  66. checkinPlistContents[kFIRMessagingVersionInfoStringKey] = self.versionInfo ?: @"";
  67. checkinPlistContents[kFIRMessagingDeviceDataVersionKey] = self.deviceDataVersion ?: @"";
  68. checkinPlistContents[kFIRMessagingLastCheckinTimeKey] = @(self.lastCheckinTimestampMillis);
  69. checkinPlistContents[kFIRMessagingGServicesDictionaryKey] =
  70. [self.gServicesData count] ? self.gServicesData : @{};
  71. return checkinPlistContents;
  72. }
  73. - (BOOL)hasCheckinInfo {
  74. return (self.deviceID.length && self.secretToken.length);
  75. }
  76. - (BOOL)hasValidCheckinInfo {
  77. int64_t currentTimestampInMillis = FIRMessagingCurrentTimestampInMilliseconds();
  78. int64_t timeSinceLastCheckinInMillis = currentTimestampInMillis - self.lastCheckinTimestampMillis;
  79. BOOL hasCheckinInfo = [self hasCheckinInfo];
  80. NSString *lastLocale = [[GULUserDefaults standardUserDefaults]
  81. stringForKey:kFIRMessagingInstanceIDUserDefaultsKeyLocale];
  82. // If it's app's first time open and checkin is already fetched and no locale information is
  83. // stored, then checkin info is valid. We should not checkin again because locale is considered
  84. // "changed".
  85. if (hasCheckinInfo && !lastLocale) {
  86. NSString *currentLocale = FIRMessagingCurrentLocale();
  87. [[GULUserDefaults standardUserDefaults] setObject:currentLocale
  88. forKey:kFIRMessagingInstanceIDUserDefaultsKeyLocale];
  89. return YES;
  90. }
  91. // If locale has changed, checkin info is no longer valid.
  92. // Also update locale information if changed. (Only do it here not in token refresh)
  93. if (FIRMessagingHasLocaleChanged()) {
  94. NSString *currentLocale = FIRMessagingCurrentLocale();
  95. [[GULUserDefaults standardUserDefaults] setObject:currentLocale
  96. forKey:kFIRMessagingInstanceIDUserDefaultsKeyLocale];
  97. return NO;
  98. }
  99. return (hasCheckinInfo &&
  100. (timeSinceLastCheckinInMillis / 1000.0 < kFIRMessagingDefaultCheckinInterval));
  101. }
  102. - (void)setHasPreCachedAuthCredentials:(BOOL)hasPreCachedAuthCredentials {
  103. _hasPreCachedAuthCredentials = hasPreCachedAuthCredentials;
  104. }
  105. - (NSString *)checkinKeychainContent {
  106. if ([self.deviceID length] && [self.secretToken length]) {
  107. return [NSString stringWithFormat:@"%@%@%@", self.deviceID,
  108. kCheckinKeychainContentSeparatorString, self.secretToken];
  109. } else {
  110. return nil;
  111. }
  112. }
  113. - (void)updateWithCheckinPlistContents:(NSDictionary *)checkinPlistContent {
  114. for (NSString *key in checkinPlistContent) {
  115. if ([kFIRMessagingDigestStringKey isEqualToString:key]) {
  116. self.digest = [checkinPlistContent[key] copy];
  117. } else if ([kFIRMessagingVersionInfoStringKey isEqualToString:key]) {
  118. self.versionInfo = [checkinPlistContent[key] copy];
  119. } else if ([kFIRMessagingLastCheckinTimeKey isEqualToString:key]) {
  120. self.lastCheckinTimestampMillis = [checkinPlistContent[key] longLongValue];
  121. } else if ([kFIRMessagingGServicesDictionaryKey isEqualToString:key]) {
  122. self.gServicesData = [checkinPlistContent[key] mutableCopy];
  123. } else if ([kFIRMessagingDeviceDataVersionKey isEqualToString:key]) {
  124. self.deviceDataVersion = [checkinPlistContent[key] copy];
  125. }
  126. // Otherwise we have some keys we don't care about
  127. }
  128. }
  129. + (NSString *)checkinDeviceIDFromKeychainContent:(NSString *)keychainContent {
  130. return [self checkinKeychainContent:keychainContent forIndex:0];
  131. }
  132. + (NSString *)checkinSecretFromKeychainContent:(NSString *)keychainContent {
  133. return [self checkinKeychainContent:keychainContent forIndex:1];
  134. }
  135. + (NSString *)checkinKeychainContent:(NSString *)keychainContent forIndex:(int)index {
  136. NSArray *keychainComponents =
  137. [keychainContent componentsSeparatedByString:kCheckinKeychainContentSeparatorString];
  138. if (index >= 0 && index < 2 && [keychainComponents count] == 2) {
  139. return keychainComponents[index];
  140. } else {
  141. return nil;
  142. }
  143. }
  144. @end