FIRAuthStoredUserManager.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "FirebaseAuth/Sources/SystemService/FIRAuthStoredUserManager.h"
  17. /** @var kUserAccessGroupKey
  18. @brief Key of user access group stored in user defaults. Used for retrieve the user access
  19. group at launch.
  20. */
  21. static NSString *kStoredUserAccessGroupKey = @"firebase_auth_stored_user_access_group";
  22. /** @var kSharedKeychainAccountValue
  23. @brief Default value for kSecAttrAccount of shared keychain items.
  24. */
  25. static NSString *kSharedKeychainAccountValue = @"firebase_auth_firebase_user";
  26. /** @var kStoredUserCoderKey
  27. @brief The key to encode and decode the stored user.
  28. */
  29. static NSString *kStoredUserCoderKey = @"firebase_auth_stored_user_coder_key";
  30. @implementation FIRAuthStoredUserManager
  31. #pragma mark - Initializers
  32. - (instancetype)initWithServiceName:(NSString *)serviceName {
  33. self = [super init];
  34. if (self) {
  35. _keychainServices = [[FIRAuthKeychainServices alloc] initWithService:serviceName];
  36. _userDefaults = [[FIRAuthUserDefaults alloc] initWithService:serviceName];
  37. }
  38. return self;
  39. }
  40. #pragma mark - User Access Group
  41. - (NSString *_Nullable)getStoredUserAccessGroupWithError:(NSError *_Nullable *_Nullable)outError {
  42. NSData *data = [self.userDefaults dataForKey:kStoredUserAccessGroupKey error:outError];
  43. if (data) {
  44. NSString *userAccessGroup = [NSString stringWithUTF8String:data.bytes];
  45. return userAccessGroup;
  46. } else {
  47. return nil;
  48. }
  49. }
  50. - (BOOL)setStoredUserAccessGroup:(NSString *_Nullable)accessGroup
  51. error:(NSError *_Nullable *_Nullable)outError {
  52. NSData *data = [accessGroup dataUsingEncoding:NSUTF8StringEncoding];
  53. if (!data) {
  54. return [self.userDefaults removeDataForKey:kStoredUserAccessGroupKey error:outError];
  55. } else {
  56. return [self.userDefaults setData:data forKey:kStoredUserAccessGroupKey error:outError];
  57. }
  58. }
  59. #pragma mark - User for Access Group
  60. - (FIRUser *)getStoredUserForAccessGroup:(NSString *)accessGroup
  61. projectIdentifier:(NSString *)projectIdentifier
  62. error:(NSError *_Nullable *_Nullable)outError {
  63. NSMutableDictionary *query = [[NSMutableDictionary alloc] init];
  64. query[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
  65. query[(__bridge id)kSecAttrAccessGroup] = accessGroup;
  66. query[(__bridge id)kSecAttrService] = projectIdentifier;
  67. query[(__bridge id)kSecAttrAccount] = kSharedKeychainAccountValue;
  68. NSData *data = [self.keychainServices getItemWithQuery:query error:outError];
  69. // iOS 12 deprecation
  70. #pragma clang diagnostic push
  71. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  72. NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  73. #pragma clang diagnostic pop
  74. FIRUser *user = [unarchiver decodeObjectOfClass:[FIRUser class] forKey:kStoredUserCoderKey];
  75. return user;
  76. }
  77. - (BOOL)setStoredUser:(FIRUser *)user
  78. forAccessGroup:(NSString *)accessGroup
  79. projectIdentifier:(NSString *)projectIdentifier
  80. error:(NSError *_Nullable *_Nullable)outError {
  81. NSMutableDictionary *query = [[NSMutableDictionary alloc] init];
  82. query[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
  83. query[(__bridge id)kSecAttrAccessible] =
  84. (__bridge id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly;
  85. query[(__bridge id)kSecAttrAccessGroup] = accessGroup;
  86. query[(__bridge id)kSecAttrService] = projectIdentifier;
  87. query[(__bridge id)kSecAttrAccount] = kSharedKeychainAccountValue;
  88. NSMutableData *data = [NSMutableData data];
  89. // iOS 12 deprecation
  90. #pragma clang diagnostic push
  91. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  92. NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
  93. #pragma clang diagnostic pop
  94. [archiver encodeObject:user forKey:kStoredUserCoderKey];
  95. [archiver finishEncoding];
  96. return [self.keychainServices setItem:data withQuery:query error:outError];
  97. }
  98. - (BOOL)removeStoredUserForAccessGroup:(NSString *)accessGroup
  99. projectIdentifier:(NSString *)projectIdentifier
  100. error:(NSError *_Nullable *_Nullable)outError {
  101. NSMutableDictionary *query = [[NSMutableDictionary alloc] init];
  102. query[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
  103. query[(__bridge id)kSecAttrAccessible] =
  104. (__bridge id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly;
  105. query[(__bridge id)kSecAttrAccessGroup] = accessGroup;
  106. query[(__bridge id)kSecAttrService] = projectIdentifier;
  107. query[(__bridge id)kSecAttrAccount] = kSharedKeychainAccountValue;
  108. return [self.keychainServices removeItemWithQuery:query error:outError];
  109. }
  110. @end