FIRInstallationsIIDCheckinStore.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "FIRInstallationsIIDCheckinStore.h"
  17. #if __has_include(<FBLPromises/FBLPromises.h>)
  18. #import <FBLPromises/FBLPromises.h>
  19. #else
  20. #import "FBLPromises.h"
  21. #endif
  22. #import "FIRInstallationsErrorUtil.h"
  23. #import "FIRInstallationsKeychainUtils.h"
  24. #import "FIRInstallationsStoredIIDCheckin.h"
  25. NSString *const kFIRInstallationsIIDCheckinKeychainGeneric = @"com.google.iid";
  26. NSString *const kFIRFIRInstallationsIIDCheckinKeychainService = @"com.google.iid.checkin";
  27. @implementation FIRInstallationsIIDCheckinStore
  28. - (FBLPromise<FIRInstallationsStoredIIDCheckin *> *)existingCheckin {
  29. return [[FBLPromise onQueue:dispatch_get_global_queue(QOS_CLASS_UTILITY, 0)
  30. do:^id _Nullable {
  31. return [self IIDCheckinData];
  32. }] onQueue:dispatch_get_global_queue(QOS_CLASS_UTILITY, 0)
  33. then:^id _Nullable(NSData *_Nullable keychainData) {
  34. return [self IIDCheckinWithData:keychainData];
  35. }];
  36. }
  37. - (FBLPromise<FIRInstallationsStoredIIDCheckin *> *)IIDCheckinWithData:(NSData *)data {
  38. FBLPromise<FIRInstallationsStoredIIDCheckin *> *resultPromise = [FBLPromise pendingPromise];
  39. NSString *checkinKeychainContent = [[NSString alloc] initWithData:data
  40. encoding:NSUTF8StringEncoding];
  41. NSArray<NSString *> *checkinComponents =
  42. [checkinKeychainContent componentsSeparatedByString:@"|"];
  43. if (checkinComponents.count < 2) {
  44. [resultPromise reject:[FIRInstallationsErrorUtil corruptedIIDCheckingData]];
  45. return resultPromise;
  46. }
  47. NSString *deviceID = checkinComponents[0];
  48. NSString *secret = checkinComponents[1];
  49. if (deviceID.length < 1 || secret.length < 1) {
  50. [resultPromise reject:[FIRInstallationsErrorUtil corruptedIIDCheckingData]];
  51. return resultPromise;
  52. }
  53. __auto_type checkin = [[FIRInstallationsStoredIIDCheckin alloc] initWithDeviceID:deviceID
  54. secretToken:secret];
  55. [resultPromise fulfill:checkin];
  56. return resultPromise;
  57. }
  58. - (FBLPromise<NSData *> *)IIDCheckinData {
  59. FBLPromise<NSData *> *resultPromise = [FBLPromise pendingPromise];
  60. NSMutableDictionary *keychainQuery = [self IIDCheckinDataKeychainQuery];
  61. NSError *error;
  62. NSData *data = [FIRInstallationsKeychainUtils getItemWithQuery:keychainQuery error:&error];
  63. if (data) {
  64. [resultPromise fulfill:data];
  65. return resultPromise;
  66. } else if (error) {
  67. [resultPromise reject:error];
  68. return resultPromise;
  69. } else {
  70. [resultPromise reject:[FIRInstallationsErrorUtil corruptedIIDCheckingData]];
  71. return resultPromise;
  72. }
  73. }
  74. - (NSMutableDictionary *)IIDCheckinDataKeychainQuery {
  75. NSDictionary *query = @{(__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword};
  76. NSMutableDictionary *finalQuery = [NSMutableDictionary dictionaryWithDictionary:query];
  77. finalQuery[(__bridge NSString *)kSecAttrGeneric] = kFIRInstallationsIIDCheckinKeychainGeneric;
  78. NSString *account = [self IIDAppIdentifier];
  79. if ([account length]) {
  80. finalQuery[(__bridge NSString *)kSecAttrAccount] = account;
  81. }
  82. finalQuery[(__bridge NSString *)kSecAttrService] = kFIRFIRInstallationsIIDCheckinKeychainService;
  83. return finalQuery;
  84. }
  85. - (NSString *)IIDAppIdentifier {
  86. return [[NSBundle mainBundle] bundleIdentifier] ?: @"";
  87. }
  88. @end