FIRMessagingCheckinStore.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/FIRMessagingCheckinStore.h"
  17. #import "FirebaseMessaging/Sources/FIRMessagingCode.h"
  18. #import "FirebaseMessaging/Sources/FIRMessagingConstants.h"
  19. #import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
  20. #import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
  21. #import "FirebaseMessaging/Sources/NSError+FIRMessaging.h"
  22. #import "FirebaseMessaging/Sources/Token/FIRMessagingAuthKeychain.h"
  23. #import "FirebaseMessaging/Sources/Token/FIRMessagingBackupExcludedPlist.h"
  24. #import "FirebaseMessaging/Sources/Token/FIRMessagingCheckinPreferences.h"
  25. #import "FirebaseMessaging/Sources/Token/FIRMessagingCheckinService.h"
  26. // NOTE: These values should be in sync with what InstanceID saves in as.
  27. static NSString *const kCheckinFileName = @"g-checkin";
  28. static NSString *const kFIRMessagingCheckinKeychainGeneric = @"com.google.iid";
  29. NSString *const kFIRMessagingCheckinKeychainService = @"com.google.iid.checkin";
  30. @interface FIRMessagingCheckinStore ()
  31. @property(nonatomic, readwrite, strong) FIRMessagingBackupExcludedPlist *plist;
  32. @property(nonatomic, readwrite, strong) FIRMessagingAuthKeychain *keychain;
  33. // Checkin will store items under
  34. // Keychain account: <app bundle id>,
  35. // Keychain service: |kFIRMessagingCheckinKeychainService|
  36. @property(nonatomic, readonly) NSString *bundleIdentifierForKeychainAccount;
  37. @end
  38. @implementation FIRMessagingCheckinStore
  39. - (instancetype)init {
  40. self = [super init];
  41. if (self) {
  42. _plist = [[FIRMessagingBackupExcludedPlist alloc]
  43. initWithPlistFile:kCheckinFileName
  44. subDirectory:kFIRMessagingInstanceIDSubDirectoryName];
  45. _keychain =
  46. [[FIRMessagingAuthKeychain alloc] initWithIdentifier:kFIRMessagingCheckinKeychainGeneric];
  47. }
  48. return self;
  49. }
  50. - (BOOL)hasCheckinPlist {
  51. return [self.plist doesFileExist];
  52. }
  53. - (NSString *)bundleIdentifierForKeychainAccount {
  54. static NSString *bundleIdentifier;
  55. static dispatch_once_t onceToken;
  56. dispatch_once(&onceToken, ^{
  57. bundleIdentifier = FIRMessagingAppIdentifier();
  58. });
  59. return bundleIdentifier;
  60. }
  61. - (void)saveCheckinPreferences:(FIRMessagingCheckinPreferences *)preferences
  62. handler:(void (^)(NSError *error))handler {
  63. NSDictionary *checkinPlistContents = [preferences checkinPlistContents];
  64. NSString *checkinKeychainContent = [preferences checkinKeychainContent];
  65. if (![checkinKeychainContent length]) {
  66. NSString *failureReason = @"Failed to get checkin keychain content from memory.";
  67. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeCheckinStore000, @"%@", failureReason);
  68. if (handler) {
  69. handler([NSError messagingErrorWithCode:kFIRMessagingErrorCodeRegistrarFailedToCheckIn
  70. failureReason:failureReason]);
  71. }
  72. return;
  73. }
  74. if (![checkinPlistContents count]) {
  75. NSString *failureReason = @"Failed to get checkin plist contents from memory.";
  76. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeCheckinStore001, @"%@", failureReason);
  77. if (handler) {
  78. handler([NSError messagingErrorWithCode:kFIRMessagingErrorCodeRegistrarFailedToCheckIn
  79. failureReason:failureReason]);
  80. }
  81. return;
  82. }
  83. // Save all other checkin preferences in a plist
  84. NSError *error;
  85. if (![self.plist writeDictionary:checkinPlistContents error:&error]) {
  86. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeCheckinStore003,
  87. @"Failed to save checkin plist contents."
  88. @"Will delete auth credentials");
  89. [self.keychain removeItemsMatchingService:kFIRMessagingCheckinKeychainService
  90. account:self.bundleIdentifierForKeychainAccount
  91. handler:nil];
  92. if (handler) {
  93. handler(error);
  94. }
  95. return;
  96. }
  97. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeCheckinStoreCheckinPlistSaved,
  98. @"Checkin plist file is saved");
  99. // Save the deviceID and secret in the Keychain
  100. if (!preferences.hasPreCachedAuthCredentials) {
  101. NSData *data = [checkinKeychainContent dataUsingEncoding:NSUTF8StringEncoding];
  102. [self.keychain setData:data
  103. forService:kFIRMessagingCheckinKeychainService
  104. account:self.bundleIdentifierForKeychainAccount
  105. handler:^(NSError *error) {
  106. if (error) {
  107. if (handler) {
  108. handler(error);
  109. }
  110. return;
  111. }
  112. if (handler) {
  113. handler(nil);
  114. }
  115. }];
  116. } else {
  117. handler(nil);
  118. }
  119. }
  120. - (void)removeCheckinPreferencesWithHandler:(void (^)(NSError *error))handler {
  121. // Delete the checkin preferences plist first to avoid delay.
  122. NSError *deletePlistError;
  123. if (![self.plist deleteFile:&deletePlistError]) {
  124. handler(deletePlistError);
  125. return;
  126. }
  127. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeCheckinStoreCheckinPlistDeleted,
  128. @"Deleted checkin plist file.");
  129. // Remove deviceID and secret from Keychain
  130. [self.keychain removeItemsMatchingService:kFIRMessagingCheckinKeychainService
  131. account:self.bundleIdentifierForKeychainAccount
  132. handler:^(NSError *error) {
  133. handler(error);
  134. }];
  135. }
  136. - (FIRMessagingCheckinPreferences *)cachedCheckinPreferences {
  137. // Query the keychain for deviceID and secret
  138. NSData *item = [self.keychain dataForService:kFIRMessagingCheckinKeychainService
  139. account:self.bundleIdentifierForKeychainAccount];
  140. // Check info found in keychain
  141. NSString *checkinKeychainContent = [[NSString alloc] initWithData:item
  142. encoding:NSUTF8StringEncoding];
  143. FIRMessagingCheckinPreferences *checkinPreferences = [FIRMessagingCheckinPreferences
  144. preferencesFromKeychainContents:[checkinKeychainContent copy]];
  145. NSDictionary *checkinPlistContents = [self.plist contentAsDictionary];
  146. NSString *plistDeviceAuthID = checkinPlistContents[kFIRMessagingDeviceAuthIdKey];
  147. NSString *plistSecretToken = checkinPlistContents[kFIRMessagingSecretTokenKey];
  148. // If deviceID and secret not found in the keychain verify that we don't have them in the
  149. // checkin preferences plist.
  150. if (![checkinPreferences.deviceID length] && ![checkinPreferences.secretToken length]) {
  151. if ([plistDeviceAuthID length] && [plistSecretToken length]) {
  152. // Couldn't find checkin credentials in keychain but found them in the plist.
  153. checkinPreferences =
  154. [[FIRMessagingCheckinPreferences alloc] initWithDeviceID:plistDeviceAuthID
  155. secretToken:plistSecretToken];
  156. } else {
  157. // Couldn't find checkin credentials in keychain nor plist
  158. return nil;
  159. }
  160. }
  161. [checkinPreferences updateWithCheckinPlistContents:checkinPlistContents];
  162. return checkinPreferences;
  163. }
  164. @end