FIRAuthStoredUserManager.m 5.8 KB

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