diff --git a/packages/firebase_app_check/firebase_app_check/example/integration_test/e2e_test.dart b/packages/firebase_app_check/firebase_app_check/example/integration_test/e2e_test.dart index 3f63301478af..70de5a8de7d0 100644 --- a/packages/firebase_app_check/firebase_app_check/example/integration_test/e2e_test.dart +++ b/packages/firebase_app_check/firebase_app_check/example/integration_test/e2e_test.dart @@ -5,6 +5,7 @@ // ignore_for_file: do_not_use_environment import 'dart:convert'; +import 'dart:io'; import 'package:firebase_app_check/firebase_app_check.dart'; import 'package:firebase_core/firebase_core.dart'; @@ -124,6 +125,28 @@ void main() { ); }, skip: defaultTargetPlatform != TargetPlatform.iOS); + test('getToken uses the provider passed to activate', () async { + await FirebaseAppCheck.instance.activate( + providerApple: const AppleAppAttestProvider(), + ); + + // No simulator supports App Attest, so the error names App Attest + // only when the provider passed to activate() is the one asked for a + // token. The native SDK used to keep the provider it created while + // firebase_core configured Firebase, before activate() ran. On a + // simulator that is the debug provider, which fails differently. + await expectLater( + FirebaseAppCheck.instance.getToken(true), + throwsA( + isA().having( + (e) => e.message, + 'message', + contains('AppAttestProvider'), + ), + ), + ); + }, skip: _isIosSimulator ? null : 'iOS simulator only.'); + test( 'appAttestWithDeviceCheckFallback falls back rather than erroring', () async { @@ -200,6 +223,13 @@ void main() { }); } +/// A simulator app runs out of the host's CoreSimulator directory. iOS gives +/// Dart no environment variables to check instead. +bool get _isIosSimulator => + !kIsWeb && + defaultTargetPlatform == TargetPlatform.iOS && + Platform.resolvedExecutable.contains('CoreSimulator'); + /// The lifetime (`exp` - `iat`) of an App Check token, read from its JWT claims. Duration _tokenTtl(String token) { final claims = diff --git a/packages/firebase_app_check/firebase_app_check/ios/firebase_app_check/Sources/firebase_app_check/FirebaseAppCheckPlugin.swift b/packages/firebase_app_check/firebase_app_check/ios/firebase_app_check/Sources/firebase_app_check/FirebaseAppCheckPlugin.swift index 8820cc452ad7..390f308e2033 100644 --- a/packages/firebase_app_check/firebase_app_check/ios/firebase_app_check/Sources/firebase_app_check/FirebaseAppCheckPlugin.swift +++ b/packages/firebase_app_check/firebase_app_check/ios/firebase_app_check/Sources/firebase_app_check/FirebaseAppCheckPlugin.swift @@ -21,6 +21,10 @@ import FirebaseCore let kFirebaseAppCheckChannelName = "plugins.flutter.io/firebase_app_check" let kFirebaseAppCheckTokenChannelPrefix = "plugins.flutter.io/firebase_app_check/token/" +/// Has a fixed Objective-C name because `firebase_core` looks this class up by +/// name. Keep it in sync with `installAppCheckProviderFactoryIfLinked` in +/// FLTFirebaseCorePlugin. +@objc(FirebaseAppCheckPlugin) public class FirebaseAppCheckPlugin: NSObject, FlutterPlugin, FLTFirebasePluginProtocol, FirebaseAppCheckHostApi { @@ -28,10 +32,23 @@ public class FirebaseAppCheckPlugin: NSObject, FlutterPlugin, private var streamHandlers: [String: AppCheckTokenStreamHandler] = [:] private var providerFactory: FlutterAppCheckProviderFactory? + /// The Apple SDK asks the installed factory for a provider during + /// `FirebaseApp.configure()` and keeps that provider. `activate()` + /// reconfigures the provider this factory handed out, so there must only + /// ever be one factory. + static let sharedProviderFactory = FlutterAppCheckProviderFactory() + + /// `firebase_core` calls this immediately before `FirebaseApp.configure()`. + /// Flutter registers `firebase_core` first, so installing the factory from + /// `register(with:)` alone would be too late. + @objc static func installProviderFactory() { + AppCheck.setAppCheckProviderFactory(sharedProviderFactory) + } + static let shared: FirebaseAppCheckPlugin = { let instance = FirebaseAppCheckPlugin() - instance.providerFactory = FlutterAppCheckProviderFactory() - AppCheck.setAppCheckProviderFactory(instance.providerFactory) + instance.providerFactory = sharedProviderFactory + installProviderFactory() FLTFirebasePluginRegistry.sharedInstance().register(instance) return instance }() diff --git a/packages/firebase_core/firebase_core/ios/firebase_core/Sources/firebase_core/FLTFirebaseCorePlugin.swift b/packages/firebase_core/firebase_core/ios/firebase_core/Sources/firebase_core/FLTFirebaseCorePlugin.swift index 33d5b130fc58..744c413a3eb5 100644 --- a/packages/firebase_core/firebase_core/ios/firebase_core/Sources/firebase_core/FLTFirebaseCorePlugin.swift +++ b/packages/firebase_core/firebase_core/ios/firebase_core/Sources/firebase_core/FLTFirebaseCorePlugin.swift @@ -22,6 +22,22 @@ extension FlutterError: @retroactive Error {} private let kFLTFirebaseCoreChannelName = "dev.flutter.pigeon.FirebaseCoreHostApi.initializeApp" +/// Lets firebase_app_check install its provider factory before +/// `FirebaseApp.configure()`. The Apple SDK asks the factory for a provider +/// during configure and ignores a factory set afterwards. Flutter registers +/// firebase_core before firebase_app_check, so that plugin cannot do this from +/// its own registration. Looked up by name because firebase_core cannot depend +/// on firebase_app_check. Does nothing when firebase_app_check is not linked. +private func installAppCheckProviderFactoryIfLinked() { + let selector = NSSelectorFromString("installProviderFactory") + guard let plugin = NSClassFromString("FirebaseAppCheckPlugin") as? NSObject.Type, + plugin.responds(to: selector) + else { + return + } + plugin.perform(selector) +} + @objc(FLTFirebaseCorePlugin) public class FLTFirebaseCorePlugin: NSObject, FlutterPlugin, FLTFirebasePluginProtocol, FirebaseCoreHostApi, FirebaseAppHostApi @@ -43,6 +59,7 @@ public class FLTFirebaseCorePlugin: NSObject, FlutterPlugin, FLTFirebasePluginPr if FirebaseOptions.defaultOptions() != nil, FirebaseApp.allApps?["__FIRAPP_DEFAULT"] == nil { + installAppCheckProviderFactoryIfLinked() FirebaseApp.configure() }