AuthComponent.swift 3.2 KB

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