FIRInstanceIDFakeKeychain.m 3.3 KB

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 "FIRInstanceIDFakeKeychain.h"
  17. static NSString *const kFakeKeychainErrorDomain = @"com.google.iid";
  18. @interface FIRInstanceIDFakeKeychain ()
  19. @property(nonatomic, readwrite, strong) NSMutableDictionary *data;
  20. @end
  21. @implementation FIRInstanceIDFakeKeychain
  22. - (instancetype)init {
  23. self = [super init];
  24. if (self) {
  25. _data = [NSMutableDictionary dictionary];
  26. }
  27. return self;
  28. }
  29. - (NSArray<NSData *> *)itemsMatchingService:(NSString *)service account:(NSString *)account {
  30. if (self.cannotReadFromKeychain) {
  31. return @[];
  32. }
  33. NSMutableArray<NSData *> *results = [NSMutableArray array];
  34. BOOL accountIsWildcard = [account isEqualToString:kFIRInstanceIDKeychainWildcardIdentifier];
  35. BOOL serviceIsWildcard = [service isEqualToString:kFIRInstanceIDKeychainWildcardIdentifier];
  36. for (NSString *accountKey in [self.data allKeys]) {
  37. if (!accountIsWildcard && ![accountKey isEqualToString:account]) {
  38. continue;
  39. }
  40. NSDictionary *services = self.data[accountKey];
  41. for (NSString *serviceKey in [services allKeys]) {
  42. if (!serviceIsWildcard && ![serviceKey isEqualToString:service]) {
  43. continue;
  44. }
  45. NSData *item = self.data[accountKey][serviceKey];
  46. [results addObject:item];
  47. }
  48. }
  49. return results;
  50. }
  51. - (NSData *)dataForService:(NSString *)service account:(NSString *)account {
  52. if (self.cannotReadFromKeychain) {
  53. return nil;
  54. }
  55. return self.data[account][service];
  56. }
  57. - (void)removeItemsMatchingService:(NSString *)service
  58. account:(NSString *)account
  59. handler:(void (^)(NSError *error))handler {
  60. if (self.cannotWriteToKeychain) {
  61. if (handler) {
  62. handler([NSError errorWithDomain:kFakeKeychainErrorDomain code:1001 userInfo:nil]);
  63. }
  64. return;
  65. }
  66. if ([account isEqualToString:kFIRInstanceIDKeychainWildcardIdentifier]) {
  67. // Remove all account keys.
  68. [self.data removeAllObjects];
  69. } else {
  70. if ([service isEqualToString:kFIRInstanceIDKeychainWildcardIdentifier]) {
  71. // Remove all service keys for this account key.
  72. [self.data[account] removeAllObjects];
  73. } else {
  74. [self.data[account] removeObjectForKey:service];
  75. }
  76. }
  77. if (handler) {
  78. handler(nil);
  79. }
  80. }
  81. - (void)setData:(NSData *)data
  82. forService:(NSString *)service
  83. accessibility:(nullable CFTypeRef)accessibility
  84. account:(NSString *)account
  85. handler:(void (^)(NSError *error))handler {
  86. if (self.cannotWriteToKeychain) {
  87. if (handler) {
  88. handler([NSError errorWithDomain:kFakeKeychainErrorDomain code:1001 userInfo:nil]);
  89. }
  90. return;
  91. }
  92. if (!self.data[account]) {
  93. self.data[account] = [NSMutableDictionary dictionary];
  94. }
  95. self.data[account][service] = data;
  96. if (handler) {
  97. handler(nil);
  98. }
  99. }
  100. @end