Skip to content

Commit

Permalink
feat(app-check)!: update activate() to be able to choose android ap…
Browse files Browse the repository at this point in the history
…p attest provider. Support Play Integrity provider. Deprecate Safety Net provider (#9646)
  • Loading branch information
russellwheatley committed Oct 25, 2022
1 parent 03eab1f commit 02d7148
Show file tree
Hide file tree
Showing 27 changed files with 275 additions and 655 deletions.
6 changes: 3 additions & 3 deletions docs/app-check/debug-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ immediately in the Firebase console.

## Android

To use the debug provider while running your Flutter app in an Android environment,
To use the debug provider while running your Flutter app in an Android environment,
implement the following code in your Flutter application:

```dart
Expand All @@ -118,8 +118,8 @@ Future<void> main() async {
await Firebase.initializeApp();
await FirebaseAppCheck.instance.activate(
webRecaptchaSiteKey: 'recaptcha-v3-site-key',
// Set the androidDebugProvider to `true`
androidDebugProvider: true,
// Set androidProvider to `AndroidProvider.debug`
androidProvider: AndroidProvider.debug,
);
runApp(App());
}
Expand Down
10 changes: 8 additions & 2 deletions docs/app-check/default-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Book: /docs/_book.yaml
# Get started using App Check in Flutter apps

This page shows you how to enable App Check in a Flutter app, using the
default providers: SafetyNet on Android, Device Check on Apple platforms, and
default providers: Play Integrity on Android, Device Check on Apple platforms, and
reCAPTCHA v3 on web. When you enable App Check, you help ensure that
only your app can access your project's Firebase resources. See an
[Overview](/docs/app-check) of this feature.
Expand All @@ -17,7 +17,7 @@ only your app can access your project's Firebase resources. See an
1. [Install and initialize FlutterFire](/docs/flutter/setup) if you haven't
already done so.

1. Register your apps to use App Check with the SafetyNet, Device Check, and reCAPTCHA providers in the
1. Register your apps to use App Check with the Play Integrity, Device Check, and reCAPTCHA providers in the
[**Project Settings > App Check**](https://console.firebase.google.com/project/_/settings/appcheck)
section of the Firebase console.

Expand Down Expand Up @@ -79,6 +79,12 @@ Future<void> main() async {
await Firebase.initializeApp();
await FirebaseAppCheck.instance.activate(
webRecaptchaSiteKey: 'recaptcha-v3-site-key',
// Default provider for Android is the Play Integrity provider. You can use the "AndroidProvider" enum to choose
// your preferred provider. Choose from:
// 1. debug provider
// 2. safety net provider
// 3. play integrity provider
androidProvider: AndroidProvider.debug,
);
runApp(App());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ android {
api firebaseCoreProject
implementation platform("com.google.firebase:firebase-bom:${getRootProjectExtOrCoreProperty("FirebaseSDKVersion", firebaseCoreProject)}")
// TODO: Since App Check is a beta SDK it's not available in the Firebase Android BoM so we need to specify an exact version here.
implementation 'com.google.firebase:firebase-appcheck-safetynet:16.0.2'
implementation 'com.google.firebase:firebase-appcheck-debug:16.0.2'
implementation 'com.google.firebase:firebase-appcheck-safetynet:16.1.0'
implementation 'com.google.firebase:firebase-appcheck-debug:16.1.0'
implementation 'com.google.firebase:firebase-appcheck-playintegrity'
implementation 'androidx.annotation:annotation:1.5.0'
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.google.firebase.appcheck.AppCheckTokenResult;
import com.google.firebase.appcheck.FirebaseAppCheck;
import com.google.firebase.appcheck.debug.DebugAppCheckProviderFactory;
import com.google.firebase.appcheck.playintegrity.PlayIntegrityAppCheckProviderFactory;
import com.google.firebase.appcheck.safetynet.SafetyNetAppCheckProviderFactory;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger;
Expand All @@ -33,6 +34,11 @@ public class FlutterFirebaseAppCheckPlugin

private static final String METHOD_CHANNEL_NAME = "plugins.flutter.io/firebase_app_check";
private final Map<EventChannel, TokenChannelStreamHandler> streamHandlers = new HashMap<>();
private final String TAG = "FLTAppCheckPlugin";

private final String debugProvider = "debug";
private final String safetyNetProvider = "safetyNet";
private final String playIntegrity = "playIntegrity";

@Nullable private BinaryMessenger messenger;

Expand Down Expand Up @@ -78,16 +84,30 @@ private Task<Void> activate(Map<String, Object> arguments) {
cachedThreadPool.execute(
() -> {
try {
Boolean debug = (Boolean) arguments.get("androidDebugProvider");

if (Boolean.TRUE.equals(debug)) {
FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
firebaseAppCheck.installAppCheckProviderFactory(
DebugAppCheckProviderFactory.getInstance());
} else {
FirebaseAppCheck firebaseAppCheck = getAppCheck(arguments);
firebaseAppCheck.installAppCheckProviderFactory(
SafetyNetAppCheckProviderFactory.getInstance());
String provider = (String) Objects.requireNonNull(arguments.get("androidProvider"));

switch (provider) {
case debugProvider:
{
FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
firebaseAppCheck.installAppCheckProviderFactory(
DebugAppCheckProviderFactory.getInstance());
break;
}
case safetyNetProvider:
{
FirebaseAppCheck firebaseAppCheck = getAppCheck(arguments);
firebaseAppCheck.installAppCheckProviderFactory(
SafetyNetAppCheckProviderFactory.getInstance());
break;
}
case playIntegrity:
{
FirebaseAppCheck firebaseAppCheck = getAppCheck(arguments);
firebaseAppCheck.installAppCheckProviderFactory(
PlayIntegrityAppCheckProviderFactory.getInstance());
break;
}
}
taskCompletionSource.setResult(null);
} catch (Exception e) {
Expand Down

0 comments on commit 02d7148

Please sign in to comment.