AuthComponent.swift 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 FirebaseAppCheckInterop
  15. import FirebaseCore
  16. import FirebaseCoreExtension
  17. import Foundation
  18. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  19. @objc(FIRAuthProvider) protocol AuthProvider {
  20. @objc func auth() -> Auth
  21. }
  22. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  23. @objc(FIRAuthComponent)
  24. class AuthComponent: NSObject, Library, AuthProvider, ComponentLifecycleMaintainer {
  25. // MARK: - Private Variables
  26. /// The app associated with all Auth instances in this container.
  27. /// This is `unowned` instead of `weak` so it can be used without unwrapping in `auth()`
  28. private unowned let app: FirebaseApp
  29. /// A map of active instances, grouped by app. Keys are FirebaseApp names and values are arrays
  30. /// containing all instances of Auth associated with the given app.
  31. private var instances: [String: Auth] = [:]
  32. /// Lock to manage access to the instances array to avoid race conditions.
  33. private var instancesLock: os_unfair_lock = .init()
  34. // MARK: - Initializers
  35. required init(app: FirebaseApp) {
  36. self.app = app
  37. }
  38. // MARK: - Library conformance
  39. static func componentsToRegister() -> [Component] {
  40. let appCheckInterop = Dependency(with: AppCheckInterop.self, isRequired: false)
  41. return [Component(AuthProvider.self,
  42. instantiationTiming: .alwaysEager,
  43. dependencies: [appCheckInterop]) { container, isCacheable in
  44. guard let app = container.app else { return nil }
  45. isCacheable.pointee = true
  46. let newComponent = AuthComponent(app: app)
  47. // Set up instances early enough so User on keychain will be decoded.
  48. newComponent.auth()
  49. return newComponent
  50. }]
  51. }
  52. // MARK: - AuthProvider conformance
  53. @discardableResult func auth() -> Auth {
  54. os_unfair_lock_lock(&instancesLock)
  55. // Unlock before the function returns.
  56. defer { os_unfair_lock_unlock(&instancesLock) }
  57. if let instance = instances[app.name] {
  58. return instance
  59. }
  60. let newInstance = FirebaseAuth.Auth(app: app)
  61. instances[app.name] = newInstance
  62. return newInstance
  63. }
  64. // MARK: - ComponentLifecycleMaintainer conformance
  65. func appWillBeDeleted(_ app: FirebaseApp) {
  66. kAuthGlobalWorkQueue.async {
  67. // This doesn't stop any request already issued, see b/27704535
  68. if let keychainServiceName = Auth.keychainServiceName(forAppName: app.name) {
  69. Auth.deleteKeychainServiceNameForAppName(app.name)
  70. let keychain = AuthKeychainServices(service: keychainServiceName)
  71. let userKey = "\(app.name)_firebase_user"
  72. try? keychain.removeData(forKey: userKey)
  73. }
  74. DispatchQueue.main.async {
  75. // TODO(ObjC): Move over to fire an event instead, once ready.
  76. NotificationCenter.default.post(name: Auth.authStateDidChangeNotification, object: nil)
  77. }
  78. }
  79. }
  80. }