Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<FirebaseException>().having(
(e) => e.message,
'message',
contains('AppAttestProvider'),
),
),
);
}, skip: _isIosSimulator ? null : 'iOS simulator only.');

test(
'appAttestWithDeviceCheckFallback falls back rather than erroring',
() async {
Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,34 @@ 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
{
private var eventChannels: [String: FlutterEventChannel] = [:]
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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional, could be a follow-up. AppCheck is created eagerly for every app, so when providers[app.name] is nil in FlutterAppCheckProviderFactory.configure (line 354), this factory never handed a provider to that app's AppCheck. The wrapper made there is never asked for a token, yet activate() still completes. That is what happens in the two cases listed under Limits (FirebaseApp.configure() in the AppDelegate, #18613, and a new firebase_app_check with an older firebase_core). Could that branch log something like "App Check for was configured before firebase_app_check installed its provider factory, so the Apple provider passed to activate() is not used (see #18613)"? Apps following the native workaround from #18614 would see it on every launch, which is accurate for them, and everyone else would get a console line instead of only a 403 later.


/// `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
}()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,6 +59,7 @@ public class FLTFirebaseCorePlugin: NSObject, FlutterPlugin, FLTFirebasePluginPr
if FirebaseOptions.defaultOptions() != nil,
FirebaseApp.allApps?["__FIRAPP_DEFAULT"] == nil
{
installAppCheckProviderFactoryIfLinked()
FirebaseApp.configure()
}

Expand Down
Loading