FIRAppDistributionAuthPersistence.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2020 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 "FIRAppDistributionAuthPersistence+Private.h"
  15. NS_ASSUME_NONNULL_BEGIN
  16. NSString *const kFIRAppDistributionAuthPersistenceErrorDomain =
  17. @"com.firebase.app_distribution.auth_persistence";
  18. @implementation FIRAppDistributionAuthPersistence
  19. + (void)handleAuthStateError:(NSError **_Nullable)error
  20. description:(NSString *)description
  21. code:(FIRAppDistributionKeychainError)code {
  22. if (error) {
  23. NSDictionary *userInfo = @{NSLocalizedDescriptionKey : description};
  24. *error = [NSError errorWithDomain:kFIRAppDistributionAuthPersistenceErrorDomain
  25. code:code
  26. userInfo:userInfo];
  27. }
  28. }
  29. + (BOOL)clearAuthState:(NSError **_Nullable)error {
  30. NSMutableDictionary *keychainQuery = [self getKeyChainQuery];
  31. BOOL success = [FIRAppDistributionKeychainUtility deleteKeychainItem:keychainQuery];
  32. if (!success) {
  33. NSString *description = NSLocalizedString(
  34. @"Failed to clear auth state from keychain. Tester will overwrite data on sign in.",
  35. @"Error message for failure to retrieve auth state from keychain");
  36. [self handleAuthStateError:error
  37. description:description
  38. code:FIRAppDistributionErrorTokenDeletionFailure];
  39. return NO;
  40. }
  41. return YES;
  42. }
  43. + (OIDAuthState *)retrieveAuthState:(NSError **_Nullable)error {
  44. NSMutableDictionary *keychainQuery = [self getKeyChainQuery];
  45. [keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
  46. [keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
  47. NSData *passwordData = [FIRAppDistributionKeychainUtility fetchKeychainItemMatching:keychainQuery
  48. error:NULL];
  49. NSData *result = nil;
  50. if (!passwordData) {
  51. NSString *description = NSLocalizedString(
  52. @"Failed to retrieve auth state from keychain. Tester will have to sign in again.",
  53. @"Error message for failure to retrieve auth state from keychain");
  54. [self handleAuthStateError:error
  55. description:description
  56. code:FIRAppDistributionErrorTokenRetrievalFailure];
  57. return nil;
  58. }
  59. result = [passwordData copy];
  60. if (!result) {
  61. NSString *description =
  62. NSLocalizedString(@"Failed to unarchive auth state. Tester will have to sign in again.",
  63. @"Error message for failure to retrieve auth state from keychain");
  64. [self handleAuthStateError:error
  65. description:description
  66. code:FIRAppDistributionErrorTokenRetrievalFailure];
  67. return nil;
  68. }
  69. OIDAuthState *authState = [FIRAppDistributionKeychainUtility unarchiveKeychainResult:result];
  70. return authState;
  71. }
  72. + (BOOL)persistAuthState:(OIDAuthState *)authState error:(NSError **_Nullable)error {
  73. NSData *authorizationData = [FIRAppDistributionKeychainUtility archiveDataForKeychain:authState];
  74. NSMutableDictionary *keychainQuery = [self getKeyChainQuery];
  75. BOOL success = NO;
  76. BOOL hasAuthState = [self retrieveAuthState:NULL];
  77. if (hasAuthState) {
  78. success = [FIRAppDistributionKeychainUtility updateKeychainItem:keychainQuery
  79. withDataDictionary:authorizationData];
  80. } else {
  81. success = [FIRAppDistributionKeychainUtility addKeychainItem:keychainQuery
  82. withDataDictionary:authorizationData];
  83. }
  84. if (!success) {
  85. NSString *description = NSLocalizedString(
  86. @"Failed to persist auth state. Tester will have to sign in again after app close.",
  87. @"Error message for failure to persist auth state to keychain");
  88. [self handleAuthStateError:error
  89. description:description
  90. code:FIRAppDistributionErrorTokenPersistenceFailure];
  91. return NO;
  92. }
  93. return YES;
  94. }
  95. + (NSMutableDictionary *)getKeyChainQuery {
  96. NSMutableDictionary *keychainQuery = [NSMutableDictionary
  97. dictionaryWithObjectsAndKeys:(id)kSecClassGenericPassword, (id)kSecClass, @"OAuth",
  98. (id)kSecAttrGeneric, @"OAuth", (id)kSecAttrAccount,
  99. @"fire-fad-auth", (id)kSecAttrService, nil];
  100. return keychainQuery;
  101. }
  102. @end
  103. NS_ASSUME_NONNULL_END