설명 없음

FIRInstanceIDCheckinPreferences+Internal.m 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "FIRInstanceIDCheckinPreferences+Internal.h"
  17. #import "FIRInstanceIDCheckinService.h"
  18. #import "FIRInstanceIDUtilities.h"
  19. static NSString *const kCheckinKeychainContentSeparatorString = @"|";
  20. @interface FIRInstanceIDCheckinPreferences ()
  21. @property(nonatomic, readwrite, copy) NSString *deviceID;
  22. @property(nonatomic, readwrite, copy) NSString *secretToken;
  23. @property(nonatomic, readwrite, copy) NSString *digest;
  24. @property(nonatomic, readwrite, copy) NSString *versionInfo;
  25. @property(nonatomic, readwrite, copy) NSString *deviceDataVersion;
  26. @property(nonatomic, readwrite, strong) NSMutableDictionary *gServicesData;
  27. @property(nonatomic, readwrite, assign) int64_t lastCheckinTimestampMillis;
  28. @end
  29. @implementation FIRInstanceIDCheckinPreferences (Internal)
  30. + (FIRInstanceIDCheckinPreferences *)preferencesFromKeychainContents:(NSString *)keychainContent {
  31. NSString *deviceID = [self checkinDeviceIDFromKeychainContent:keychainContent];
  32. NSString *secret = [self checkinSecretFromKeychainContent:keychainContent];
  33. if ([deviceID length] && [secret length]) {
  34. return [[FIRInstanceIDCheckinPreferences alloc] initWithDeviceID:deviceID secretToken:secret];
  35. } else {
  36. return nil;
  37. }
  38. }
  39. - (instancetype)initWithDeviceID:(NSString *)deviceID secretToken:(NSString *)secretToken {
  40. self = [super init];
  41. if (self) {
  42. self.deviceID = [deviceID copy];
  43. self.secretToken = [secretToken copy];
  44. }
  45. return self;
  46. }
  47. - (void)reset {
  48. self.deviceID = nil;
  49. self.secretToken = nil;
  50. self.digest = nil;
  51. self.versionInfo = nil;
  52. self.gServicesData = nil;
  53. self.deviceDataVersion = nil;
  54. self.lastCheckinTimestampMillis = 0;
  55. }
  56. - (void)updateWithCheckinPlistContents:(NSDictionary *)checkinPlistContent {
  57. for (NSString *key in checkinPlistContent) {
  58. if ([kFIRInstanceIDDigestStringKey isEqualToString:key]) {
  59. self.digest = [checkinPlistContent[key] copy];
  60. } else if ([kFIRInstanceIDVersionInfoStringKey isEqualToString:key]) {
  61. self.versionInfo = [checkinPlistContent[key] copy];
  62. } else if ([kFIRInstanceIDLastCheckinTimeKey isEqualToString:key]) {
  63. self.lastCheckinTimestampMillis = [checkinPlistContent[key] longLongValue];
  64. } else if ([kFIRInstanceIDGServicesDictionaryKey isEqualToString:key]) {
  65. self.gServicesData = [checkinPlistContent[key] mutableCopy];
  66. } else if ([kFIRInstanceIDDeviceDataVersionKey isEqualToString:key]) {
  67. self.deviceDataVersion = [checkinPlistContent[key] copy];
  68. }
  69. // Otherwise we have some keys we don't care about
  70. }
  71. }
  72. - (NSString *)checkinKeychainContent {
  73. if ([self.deviceID length] && [self.secretToken length]) {
  74. return [NSString stringWithFormat:@"%@%@%@", self.deviceID,
  75. kCheckinKeychainContentSeparatorString, self.secretToken];
  76. } else {
  77. return nil;
  78. }
  79. }
  80. + (NSString *)checkinDeviceIDFromKeychainContent:(NSString *)keychainContent {
  81. return [self checkinKeychainContent:keychainContent forIndex:0];
  82. }
  83. + (NSString *)checkinSecretFromKeychainContent:(NSString *)keychainContent {
  84. return [self checkinKeychainContent:keychainContent forIndex:1];
  85. }
  86. + (NSString *)checkinKeychainContent:(NSString *)keychainContent forIndex:(int)index {
  87. NSArray *keychainComponents =
  88. [keychainContent componentsSeparatedByString:kCheckinKeychainContentSeparatorString];
  89. if (index >= 0 && index < 2 && [keychainComponents count] == 2) {
  90. return keychainComponents[index];
  91. } else {
  92. return nil;
  93. }
  94. }
  95. @end