AuthUserDefaults.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2023 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 Foundation
  15. private let kPersistentDomainNamePrefix = "com.google.Firebase.Auth."
  16. // TODO: Remove this type after all tests and internal call sites are converted to Swift
  17. // This is added since a throwing function returning Optional values in Swift cannot be
  18. // exposed to Objective-C due to convention and meaning of nil values in Objective-C.
  19. // This wrapper allows us to always return a value, thus allowing us to expose Objective-C api.
  20. @objc(FIRDataWrapper) public class DataWrapper: NSObject {
  21. @objc public let data: Data?
  22. @objc public init(data: Data?) {
  23. self.data = data
  24. }
  25. }
  26. /** @class FIRAuthUserDefaults
  27. @brief The utility class to storage data in NSUserDefaults.
  28. */
  29. @objc(FIRAuthUserDefaults) public class AuthUserDefaults: NSObject, AuthStorage {
  30. /** @var _persistentDomainName
  31. @brief The name of the persistent domain in user defaults.
  32. */
  33. private let persistentDomainName: String
  34. /** @var _storage
  35. @brief The backing NSUserDefaults storage for this instance.
  36. */
  37. private let storage: UserDefaults
  38. @objc public static func storage(identifier: String) -> Self {
  39. return Self(service: identifier)
  40. }
  41. @objc public required init(service: String) {
  42. persistentDomainName = kPersistentDomainNamePrefix + service
  43. storage = UserDefaults()
  44. }
  45. @objc public func data(forKey key: String) throws -> DataWrapper {
  46. guard let allData = storage.persistentDomain(forName: persistentDomainName)
  47. else { return DataWrapper(data: nil) }
  48. if let data = allData[key] as? Data {
  49. return DataWrapper(data: data)
  50. }
  51. return DataWrapper(data: nil)
  52. }
  53. @objc public func setData(_ data: Data, forKey key: String) throws {
  54. var allData = storage.persistentDomain(forName: persistentDomainName) ?? [:]
  55. allData[key] = data
  56. storage.setPersistentDomain(allData, forName: persistentDomainName)
  57. }
  58. @objc public func removeData(forKey key: String) throws {
  59. guard var allData = storage.persistentDomain(forName: persistentDomainName) else { return }
  60. allData.removeValue(forKey: key)
  61. storage.setPersistentDomain(allData, forName: persistentDomainName)
  62. }
  63. /** @fn clear
  64. @brief Clears all data from the storage.
  65. @remarks This method is only supposed to be called from tests.
  66. */
  67. @objc public func clear() {
  68. storage.setPersistentDomain([:], forName: persistentDomainName)
  69. }
  70. }