FIRAuthStoredUserManager.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. shareAuthStateAcrossDevices:(BOOL)shareAuthStateAcrossDevices
  63. projectIdentifier:(NSString *)projectIdentifier
  64. error:(NSError *_Nullable *_Nullable)outError {
  65. NSMutableDictionary *query = [[NSMutableDictionary alloc] init];
  66. query[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
  67. query[(__bridge id)kSecAttrAccessGroup] = accessGroup;
  68. query[(__bridge id)kSecAttrService] = projectIdentifier;
  69. query[(__bridge id)kSecAttrAccount] = kSharedKeychainAccountValue;
  70. if (shareAuthStateAcrossDevices) {
  71. query[(__bridge id)kSecAttrSynchronizable] = (__bridge id)kCFBooleanTrue;
  72. }
  73. NSData *data = [self.keychainServices getItemWithQuery:query error:outError];
  74. // If there's an outError parameter and it's populated, or there's no data, return.
  75. if ((outError && *outError) || !data) {
  76. return nil;
  77. }
  78. #if TARGET_OS_WATCH
  79. NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data
  80. error:outError];
  81. if (outError && *outError) {
  82. return nil;
  83. }
  84. #else
  85. // iOS 12 deprecation
  86. #pragma clang diagnostic push
  87. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  88. NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  89. #pragma clang diagnostic pop
  90. #endif // TARGET_OS_WATCH
  91. FIRUser *user = [unarchiver decodeObjectOfClass:[FIRUser class] forKey:kStoredUserCoderKey];
  92. return user;
  93. }
  94. - (BOOL)setStoredUser:(FIRUser *)user
  95. forAccessGroup:(NSString *)accessGroup
  96. shareAuthStateAcrossDevices:(BOOL)shareAuthStateAcrossDevices
  97. projectIdentifier:(NSString *)projectIdentifier
  98. error:(NSError *_Nullable *_Nullable)outError {
  99. NSMutableDictionary *query = [[NSMutableDictionary alloc] init];
  100. query[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
  101. if (shareAuthStateAcrossDevices) {
  102. query[(__bridge id)kSecAttrAccessible] = (__bridge id)kSecAttrAccessibleAfterFirstUnlock;
  103. } else {
  104. query[(__bridge id)kSecAttrAccessible] =
  105. (__bridge id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly;
  106. }
  107. query[(__bridge id)kSecAttrAccessGroup] = accessGroup;
  108. query[(__bridge id)kSecAttrService] = projectIdentifier;
  109. query[(__bridge id)kSecAttrAccount] = kSharedKeychainAccountValue;
  110. if (shareAuthStateAcrossDevices) {
  111. query[(__bridge id)kSecAttrSynchronizable] = (__bridge id)kCFBooleanTrue;
  112. }
  113. #if TARGET_OS_WATCH
  114. NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initRequiringSecureCoding:false];
  115. #else
  116. NSMutableData *data = [NSMutableData data];
  117. // iOS 12 deprecation
  118. #pragma clang diagnostic push
  119. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  120. NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
  121. #pragma clang diagnostic pop
  122. #endif // TARGET_OS_WATCH
  123. [archiver encodeObject:user forKey:kStoredUserCoderKey];
  124. [archiver finishEncoding];
  125. #if TARGET_OS_WATCH
  126. NSData *data = archiver.encodedData;
  127. #endif // TARGET_OS_WATCH
  128. return [self.keychainServices setItem:data withQuery:query error:outError];
  129. }
  130. - (BOOL)removeStoredUserForAccessGroup:(NSString *)accessGroup
  131. shareAuthStateAcrossDevices:(BOOL)shareAuthStateAcrossDevices
  132. projectIdentifier:(NSString *)projectIdentifier
  133. error:(NSError *_Nullable *_Nullable)outError {
  134. NSMutableDictionary *query = [[NSMutableDictionary alloc] init];
  135. query[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
  136. if (shareAuthStateAcrossDevices) {
  137. query[(__bridge id)kSecAttrAccessible] = (__bridge id)kSecAttrAccessibleAfterFirstUnlock;
  138. } else {
  139. query[(__bridge id)kSecAttrAccessible] =
  140. (__bridge id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly;
  141. }
  142. if (shareAuthStateAcrossDevices) {
  143. query[(__bridge id)kSecAttrSynchronizable] = (__bridge id)kCFBooleanTrue;
  144. }
  145. query[(__bridge id)kSecAttrAccessGroup] = accessGroup;
  146. query[(__bridge id)kSecAttrService] = projectIdentifier;
  147. query[(__bridge id)kSecAttrAccount] = kSharedKeychainAccountValue;
  148. return [self.keychainServices removeItemWithQuery:query error:outError];
  149. }
  150. @end