Nav apraksta

FIRAuthStoredUserManager.m 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "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. NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  70. FIRUser *user = [unarchiver decodeObjectOfClass:[FIRUser class] forKey:kStoredUserCoderKey];
  71. return user;
  72. }
  73. - (BOOL)setStoredUser:(FIRUser *)user
  74. forAccessGroup:(NSString *)accessGroup
  75. projectIdentifier:(NSString *)projectIdentifier
  76. error:(NSError *_Nullable *_Nullable)outError {
  77. NSMutableDictionary *query = [[NSMutableDictionary alloc] init];
  78. query[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
  79. query[(__bridge id)kSecAttrAccessible] = (__bridge id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly;
  80. query[(__bridge id)kSecAttrAccessGroup] = accessGroup;
  81. query[(__bridge id)kSecAttrService] = projectIdentifier;
  82. query[(__bridge id)kSecAttrAccount] = kSharedKeychainAccountValue;
  83. NSMutableData *data = [NSMutableData data];
  84. NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
  85. [archiver encodeObject:user forKey:kStoredUserCoderKey];
  86. [archiver finishEncoding];
  87. return [self.keychainServices setItem:data withQuery:query error:outError];
  88. }
  89. - (BOOL)removeStoredUserForAccessGroup:(NSString *)accessGroup
  90. projectIdentifier:(NSString *)projectIdentifier
  91. error:(NSError *_Nullable *_Nullable)outError {
  92. NSMutableDictionary *query = [[NSMutableDictionary alloc] init];
  93. query[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
  94. query[(__bridge id)kSecAttrAccessible] = (__bridge id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly;
  95. query[(__bridge id)kSecAttrAccessGroup] = accessGroup;
  96. query[(__bridge id)kSecAttrService] = projectIdentifier;
  97. query[(__bridge id)kSecAttrAccount] = kSharedKeychainAccountValue;
  98. return [self.keychainServices removeItemWithQuery:query error:outError];
  99. }
  100. @end