GIDKeychainHelperFake.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2025 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "GIDKeychainHelperFake.h"
  15. static NSString *const kAccountName = @"OAuthTest";
  16. @interface GIDKeychainHelperFake ()
  17. @property(nonatomic, copy) NSMutableDictionary<NSString *, NSData *> *passwordStore;
  18. @end
  19. @implementation GIDKeychainHelperFake
  20. - (instancetype)initWithKeychainAttributes:(NSSet<GTMKeychainAttribute *> *)keychainAttributes {
  21. self = [super init];
  22. if (self) {
  23. _keychainAttributes = keychainAttributes;
  24. _accountName = kAccountName;
  25. _passwordStore = [NSMutableDictionary dictionary];
  26. }
  27. return self;
  28. }
  29. - (NSDictionary<NSString *,id> * _Nonnull)keychainQueryForService:(NSString * _Nonnull)service {
  30. [NSException raise:@"Not Implemented" format:@"This method is not implemented"];
  31. }
  32. - (NSData * _Nullable)passwordDataForService:(NSString * _Nonnull)service
  33. error:(NSError * _Nullable __autoreleasing * _Nullable)error {
  34. if (service.length == 0) {
  35. *error = [NSError errorWithDomain:@"GTMAppAuthKeychainErrorDomain"
  36. code:GTMKeychainStoreErrorCodeNoService
  37. userInfo:nil];
  38. return;
  39. }
  40. NSString *passwordKey = [service stringByAppendingString:self.accountName];
  41. NSData *passwordData = self.passwordStore[passwordKey];
  42. if (!passwordData) {
  43. *error = [NSError errorWithDomain:@"GTMAppAuthKeychainErrorDomain"
  44. code:GTMKeychainStoreErrorCodePasswordNotFound
  45. userInfo:nil];
  46. return;
  47. }
  48. return passwordData;
  49. }
  50. - (NSString * _Nullable)passwordForService:(NSString * _Nonnull)service
  51. error:(NSError * _Nullable __autoreleasing * _Nullable)error {
  52. NSData *passwordData = [self passwordDataForService:service error:error];
  53. NSString *passwordString = [[NSString alloc] initWithData:passwordData
  54. encoding:NSUTF8StringEncoding];
  55. return passwordString;
  56. }
  57. - (BOOL)removePasswordForService:(NSString * _Nonnull)service
  58. error:(NSError * _Nullable __autoreleasing * _Nullable)error {
  59. if (service.length == 0) {
  60. *error = [NSError errorWithDomain:@"GTMAppAuthKeychainErrorDomain"
  61. code:GTMKeychainStoreErrorCodeNoService
  62. userInfo:nil];
  63. return;
  64. }
  65. NSString *passwordKey = [service stringByAppendingString:self.accountName];
  66. [self.passwordStore removeObjectForKey:passwordKey];
  67. return self.passwordStore[passwordKey] != nil;
  68. }
  69. - (BOOL)setPassword:(NSString * _Nonnull)password
  70. forService:(NSString * _Nonnull)service
  71. accessibility:(CFTypeRef _Nonnull)accessibility
  72. error:(NSError * _Nullable __autoreleasing * _Nullable)error {
  73. NSData *passwordData = [password dataUsingEncoding:NSUTF8StringEncoding];
  74. if (!passwordData) {
  75. *error = [NSError errorWithDomain:@"GTMAppAuthKeychainErrorDomain"
  76. code:GTMKeychainStoreErrorCodeUnexpectedPasswordData
  77. userInfo:nil];
  78. return NO;
  79. }
  80. return [self setPasswordWithData:passwordData
  81. forService:service
  82. accessibility:accessibility
  83. error:error];
  84. }
  85. - (BOOL)setPassword:(NSString * _Nonnull)password
  86. forService:(NSString * _Nonnull)service
  87. error:(NSError * _Nullable __autoreleasing * _Nullable)error {
  88. NSData *passwordData = [password dataUsingEncoding:NSUTF8StringEncoding];
  89. if (!passwordData) {
  90. *error = [NSError errorWithDomain:@"GTMAppAuthKeychainErrorDomain"
  91. code:GTMKeychainStoreErrorCodeUnexpectedPasswordData
  92. userInfo:nil];
  93. return NO;
  94. }
  95. return [self setPasswordWithData:passwordData forService:service accessibility:nil error:error];
  96. }
  97. - (BOOL)setPasswordWithData:(NSData * _Nonnull)data
  98. forService:(NSString * _Nonnull)service
  99. accessibility:(CFTypeRef _Nullable)accessibility
  100. error:(NSError * _Nullable __autoreleasing * _Nullable)error {
  101. if (service.length == 0) {
  102. *error = [NSError errorWithDomain:@"GTMAppAuthKeychainErrorDomain"
  103. code:GTMKeychainStoreErrorCodeNoService
  104. userInfo:nil];
  105. return;
  106. }
  107. NSString *passwordKey = [service stringByAppendingString:self.accountName];
  108. [self.passwordStore setValue:data forKey:passwordKey];
  109. if (self.passwordStore[passwordKey] != nil) {
  110. return YES;
  111. } else {
  112. *error = [NSError errorWithDomain:@"GTMAppAuthKeychainErrorDomain"
  113. code:GTMKeychainStoreErrorCodeFailedToSetPassword
  114. userInfo:nil];
  115. return NO;
  116. }
  117. }
  118. @end