diff --git a/AUTHORS b/AUTHORS index 0c0e6b091146..952d45c379b0 100644 --- a/AUTHORS +++ b/AUTHORS @@ -71,3 +71,4 @@ kfurue Max Wojczuk Julian Pscheid Artem Shevchenko +Adem Ocel diff --git a/docs/app-check/default-providers.md b/docs/app-check/default-providers.md index 0b844789d574..0f382a643ed5 100644 --- a/docs/app-check/default-providers.md +++ b/docs/app-check/default-providers.md @@ -132,6 +132,24 @@ This limitation is easiest to miss on a simulator, where the Apple SDK already defaults to the debug provider. See [flutterfire#18613](https://github.com/firebase/flutterfire/issues/18613). +### Leaving out the reCAPTCHA Enterprise SDK on iOS {:#without-recaptcha} + +With Swift Package Manager, the iOS plugin adds the +[reCAPTCHA Enterprise SDK](https://github.com/GoogleCloudPlatform/recaptcha-enterprise-mobile-sdk) +so that `AppleReCaptchaProvider` works without extra setup. If your app uses +another Apple provider, set the `FIREBASE_APP_CHECK_WITHOUT_RECAPTCHA` +environment variable when building to leave the SDK out of the app: + +```bash +FIREBASE_APP_CHECK_WITHOUT_RECAPTCHA=true flutter build ios +``` + +You can also add this variable to your Xcode scheme's environment variables +for persistent configuration. With CocoaPods, the plugin does not add the SDK; +to use `AppleReCaptchaProvider`, add `pod 'RecaptchaEnterprise'` to your app's +`Podfile`. Without the SDK, `AppleReCaptchaProvider` configures no provider +and App Check token requests fail. + ## Next steps Once the App Check library is installed in your app, start distributing the diff --git a/packages/firebase_app_check/firebase_app_check/ios/firebase_app_check/Package.swift b/packages/firebase_app_check/firebase_app_check/ios/firebase_app_check/Package.swift index 975bc45cf49b..fe613208bf7e 100644 --- a/packages/firebase_app_check/firebase_app_check/ios/firebase_app_check/Package.swift +++ b/packages/firebase_app_check/firebase_app_check/ios/firebase_app_check/Package.swift @@ -5,10 +5,31 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import Foundation import PackageDescription let firebaseSdkVersion: Version = "13.2.0" +/// Set FIREBASE_APP_CHECK_WITHOUT_RECAPTCHA=true to leave out the reCAPTCHA +/// Enterprise SDK if the app does not use AppleReCaptchaProvider. +/// e.g. FIREBASE_APP_CHECK_WITHOUT_RECAPTCHA=true flutter build ios +let withoutRecaptcha = + ProcessInfo.processInfo.environment["FIREBASE_APP_CHECK_WITHOUT_RECAPTCHA"] != nil + +var recaptchaPackages: [Package.Dependency] = [] +var recaptchaProducts: [Target.Dependency] = [] +if !withoutRecaptcha { + recaptchaPackages.append( + .package( + url: "https://github.com/GoogleCloudPlatform/recaptcha-enterprise-mobile-sdk.git", + from: "18.0.0" + ) + ) + recaptchaProducts.append( + .product(name: "RecaptchaEnterprise", package: "recaptcha-enterprise-mobile-sdk") + ) +} + let package = Package( name: "firebase_app_check", platforms: [ @@ -19,22 +40,17 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/firebase/firebase-ios-sdk", exact: firebaseSdkVersion), - .package( - url: "https://github.com/GoogleCloudPlatform/recaptcha-enterprise-mobile-sdk.git", - from: "18.0.0" - ), .package(name: "firebase_core", path: "../firebase_core"), .package(name: "FlutterFramework", path: "../FlutterFramework"), - ], + ] + recaptchaPackages, targets: [ .target( name: "firebase_app_check", dependencies: [ .product(name: "FirebaseAppCheck", package: "firebase-ios-sdk"), - .product(name: "RecaptchaEnterprise", package: "recaptcha-enterprise-mobile-sdk"), .product(name: "firebase-core", package: "firebase_core"), .product(name: "FlutterFramework", package: "FlutterFramework"), - ], + ] + recaptchaProducts, resources: [ .process("Resources") ] 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 390f308e2033..f6af9a37e1eb 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 @@ -400,12 +400,21 @@ class AppCheckProviderWrapper: NSObject, AppCheckProvider { delegateProvider = appAttestProvider ?? DeviceCheckProvider(app: app) case "recaptcha": #if os(iOS) - if let recaptchaSiteKey { + // The reCAPTCHA Enterprise SDK can be left out of the build (CocoaPods, + // or FIREBASE_APP_CHECK_WITHOUT_RECAPTCHA with Swift Package Manager). + // RecaptchaProvider then raises an Objective-C exception, so look up the + // class the Firebase SDK uses to detect it first. + let recaptchaLinked = NSClassFromString("RecaptchaEnterprise.RCARecaptcha") != nil + if recaptchaLinked, let recaptchaSiteKey { delegateProvider = RecaptchaProvider(app: app, siteKey: recaptchaSiteKey) } else { delegateProvider = nil } - if delegateProvider == nil { + if !recaptchaLinked { + print( + "Firebase App Check: failed to initialize RecaptchaProvider. Link the reCAPTCHA Enterprise SDK." + ) + } else if delegateProvider == nil { print( "Firebase App Check: failed to initialize RecaptchaProvider. Ensure site key is provided." ) diff --git a/packages/firebase_app_check/firebase_app_check_platform_interface/lib/src/apple_providers.dart b/packages/firebase_app_check/firebase_app_check_platform_interface/lib/src/apple_providers.dart index a84679abd2e9..500fd2833f7b 100644 --- a/packages/firebase_app_check/firebase_app_check_platform_interface/lib/src/apple_providers.dart +++ b/packages/firebase_app_check/firebase_app_check_platform_interface/lib/src/apple_providers.dart @@ -56,6 +56,12 @@ class AppleAppAttestWithDeviceCheckFallbackProvider } /// reCAPTCHA provider for Apple platforms. +/// +/// Only available on iOS, and only if the app links the reCAPTCHA Enterprise +/// SDK: Swift Package Manager builds add it unless +/// `FIREBASE_APP_CHECK_WITHOUT_RECAPTCHA` is set; CocoaPods builds need the +/// `RecaptchaEnterprise` pod in the app's `Podfile`. Without the SDK, no +/// provider is configured and token requests fail. class AppleReCaptchaProvider extends AppleAppCheckProvider { /// Creates an Apple reCAPTCHA provider with a required [siteKey]. const AppleReCaptchaProvider(this.siteKey) : super('recaptcha');