Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Auth] Schedule keychain access after prewarming #14142

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 37 additions & 31 deletions FirebaseAuth/Sources/Swift/Auth/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1618,7 +1618,6 @@ extension Auth: AuthInterop {
keychainStorageProvider: AuthKeychainStorage = AuthKeychainStorageReal(),
backend: AuthBackend = .init(rpcIssuer: AuthBackendRPCIssuer()),
authDispatcher: AuthDispatcher = .init()) {
Auth.setKeychainServiceNameForApp(app)
self.app = app
mainBundleUrlTypes = Bundle.main
.object(forInfoDictionaryKey: "CFBundleURLTypes") as? [[String: Any]]
Expand All @@ -1644,27 +1643,28 @@ extension Auth: AuthInterop {
appCheck: appCheck)
self.backend = backend
self.authDispatcher = authDispatcher

let keychainServiceName = Auth.keychainServiceName(for: app)
keychainServices = AuthKeychainServices(service: keychainServiceName,
storage: keychainStorageProvider)
storedUserManager = AuthStoredUserManager(
serviceName: keychainServiceName,
keychainServices: keychainServices
)

super.init()
requestConfiguration.auth = self

protectedDataInitialization(keychainStorageProvider)
protectedDataInitialization()
}

private func protectedDataInitialization(_ keychainStorageProvider: AuthKeychainStorage) {
private func protectedDataInitialization() {
// Continue with the rest of initialization in the work thread.
kAuthGlobalWorkQueue.async { [weak self] in
// Load current user from Keychain.
guard let self else {
return
}
if let keychainServiceName = Auth.keychainServiceName(forAppName: self.firebaseAppName) {
self.keychainServices = AuthKeychainServices(service: keychainServiceName,
storage: keychainStorageProvider)
self.storedUserManager = AuthStoredUserManager(
serviceName: keychainServiceName,
keychainServices: self.keychainServices
)
}
ncooke3 marked this conversation as resolved.
Show resolved Hide resolved

do {
if let storedUserAccessGroup = self.storedUserManager.getStoredUserAccessGroup() {
Expand Down Expand Up @@ -1723,21 +1723,21 @@ extension Auth: AuthInterop {

#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
private func addProtectedDataDidBecomeAvailableObserver() {
weak var weakSelf = self
protectedDataDidBecomeAvailableObserver =
NotificationCenter.default.addObserver(
forName: UIApplication.protectedDataDidBecomeAvailableNotification,
object: nil,
queue: nil
) { notification in
let strongSelf = weakSelf
if let observer = strongSelf?.protectedDataDidBecomeAvailableObserver {
) { [weak self] notification in
guard let self else { return }
if let observer = self.protectedDataDidBecomeAvailableObserver {
NotificationCenter.default.removeObserver(
observer,
name: UIApplication.protectedDataDidBecomeAvailableNotification,
object: nil
)
}
self.protectedDataInitialization()
ncooke3 marked this conversation as resolved.
Show resolved Hide resolved
}
}
#endif
Expand Down Expand Up @@ -1811,28 +1811,34 @@ extension Auth: AuthInterop {
/// @synchronized([FIRAuth class]) context.
fileprivate static var gKeychainServiceNameForAppName: [String: String] = [:]

/// Sets the keychain service name global data for the particular app.
/// - Parameter app: The Firebase app to set keychain service name for.
class func setKeychainServiceNameForApp(_ app: FirebaseApp) {
objc_sync_enter(Auth.self)
gKeychainServiceNameForAppName[app.name] = "firebase_auth_\(app.options.googleAppID)"
objc_sync_exit(Auth.self)
}

/// Gets the keychain service name global data for the particular app by name.
/// - Parameter appName: The name of the Firebase app to get keychain service name for.
class func keychainServiceName(forAppName appName: String) -> String? {
/// Gets the keychain service name global data for the particular app by
/// name, creating an entry for one if it does not exist.
/// - Parameter app: The Firebase app to get the keychain service name for.
/// - Returns: The keychain service name for the given app.
static func keychainServiceName(for app: FirebaseApp) -> String {
objc_sync_enter(Auth.self)
defer { objc_sync_exit(Auth.self) }
return gKeychainServiceNameForAppName[appName]
let appName = app.name
if let serviceName = gKeychainServiceNameForAppName[appName] {
return serviceName
} else {
let serviceName = "firebase_auth_\(app.options.googleAppID)"
gKeychainServiceNameForAppName[appName] = serviceName
return serviceName
}
}

/// Deletes the keychain service name global data for the particular app by name.
/// - Parameter appName: The name of the Firebase app to delete keychain service name for.
class func deleteKeychainServiceNameForAppName(_ appName: String) {
/// - Returns: The deleted keychain service name, if any.
static func deleteKeychainServiceNameForAppName(_ appName: String) -> String? {
objc_sync_enter(Auth.self)
defer { objc_sync_exit(Auth.self) }
guard let serviceName = gKeychainServiceNameForAppName[appName] else {
return nil
}
gKeychainServiceNameForAppName.removeValue(forKey: appName)
objc_sync_exit(Auth.self)
return serviceName
}

func signOutByForce(withUserID userID: String) throws {
Expand Down Expand Up @@ -2358,15 +2364,15 @@ extension Auth: AuthInterop {
// MARK: Private properties

/// The stored user manager.
private var storedUserManager: AuthStoredUserManager!
private let storedUserManager: AuthStoredUserManager

/// The Firebase app name.
private let firebaseAppName: String

private let authDispatcher: AuthDispatcher

/// The keychain service.
private var keychainServices: AuthKeychainServices!
private let keychainServices: AuthKeychainServices

/// The user access (ID) token used last time for posting auth state changed notification.
private var lastNotifiedUserToken: String?
Expand Down
3 changes: 1 addition & 2 deletions FirebaseAuth/Sources/Swift/Auth/AuthComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ class AuthComponent: NSObject, Library, ComponentLifecycleMaintainer {
kAuthGlobalWorkQueue.async {
// This doesn't stop any request already issued, see b/27704535

if let keychainServiceName = Auth.keychainServiceName(forAppName: app.name) {
Auth.deleteKeychainServiceNameForAppName(app.name)
if let keychainServiceName = Auth.deleteKeychainServiceNameForAppName(app.name) {
let keychain = AuthKeychainServices(service: keychainServiceName)
let userKey = "\(app.name)_firebase_user"
try? keychain.removeData(forKey: userKey)
Expand Down
Loading