Skip to content

Commit

Permalink
fix(firebase_app_check_web): Activate web app check on startup if it …
Browse files Browse the repository at this point in the history
…was previously activated (#11625)

* Activate web app check on startup if it was previously activated

* Refactoring

* Use the proper firebase app
  • Loading branch information
Rexios80 committed Oct 12, 2023
1 parent adc80df commit 493f254
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:html';

import 'package:firebase_app_check_platform_interface/firebase_app_check_platform_interface.dart';
import 'package:firebase_core/firebase_core.dart';
Expand All @@ -15,6 +16,9 @@ import 'src/internals.dart';
import 'src/interop/app_check.dart' as app_check_interop;

class FirebaseAppCheckWeb extends FirebaseAppCheckPlatform {
static const recaptchaTypeV3 = 'recaptcha-v3';
static const recaptchaTypeEnterprise = 'enterprise';

static Map<String, StreamController<String?>> _tokenChangesListeners = {};

/// Stub initializer to allow the [registerWith] to create an instance without
Expand All @@ -31,7 +35,27 @@ class FirebaseAppCheckWeb extends FirebaseAppCheckPlatform {
FirebaseCoreWeb.registerService(
'app-check',
productNameOverride: 'app_check',
ensurePluginInitialized: (firebaseApp) async {
final instance =
FirebaseAppCheckWeb(app: Firebase.app(firebaseApp.name));
final recaptchaType =
window.sessionStorage[_sessionKeyRecaptchaType(firebaseApp.name)];
final recaptchaSiteKey = window
.sessionStorage[_sessionKeyRecaptchaSiteKey(firebaseApp.name)];
if (recaptchaType != null && recaptchaSiteKey != null) {
final WebProvider provider;
if (recaptchaType == recaptchaTypeV3) {
provider = ReCaptchaV3Provider(recaptchaSiteKey);
} else if (recaptchaType == recaptchaTypeEnterprise) {
provider = ReCaptchaEnterpriseProvider(recaptchaSiteKey);
} else {
throw Exception('Invalid recaptcha type: $recaptchaType');
}
await instance.activate(webProvider: provider);
}
},
);

FirebaseAppCheckPlatform.instance = FirebaseAppCheckWeb.instance;
}

Expand All @@ -40,6 +64,14 @@ class FirebaseAppCheckWeb extends FirebaseAppCheckPlatform {
return FirebaseAppCheckWeb._();
}

static String _sessionKeyRecaptchaType(String appName) {
return 'FlutterFire-$appName-recaptchaType';
}

static String _sessionKeyRecaptchaSiteKey(String appName) {
return 'FlutterFire-$appName-recaptchaSiteKey';
}

/// instance of AppCheck from the web plugin
app_check_interop.AppCheck? _webAppCheck;

Expand Down Expand Up @@ -68,6 +100,21 @@ class FirebaseAppCheckWeb extends FirebaseAppCheckPlatform {
AndroidProvider? androidProvider,
AppleProvider? appleProvider,
}) async {
// save the recaptcha type and site key for future startups
if (webProvider != null) {
final String recaptchaType;
if (webProvider is ReCaptchaV3Provider) {
recaptchaType = recaptchaTypeV3;
} else if (webProvider is ReCaptchaEnterpriseProvider) {
recaptchaType = recaptchaTypeEnterprise;
} else {
throw Exception('Invalid web provider: $webProvider');
}
window.sessionStorage[_sessionKeyRecaptchaType(app.name)] = recaptchaType;
window.sessionStorage[_sessionKeyRecaptchaSiteKey(app.name)] =
webProvider.siteKey;
}

// activate API no longer exists, recaptcha key has to be passed on initialization of app-check instance.
return convertWebExceptions<Future<void>>(() async {
_webAppCheck ??= app_check_interop.getAppCheckInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ class FirebaseCoreWeb extends FirebasePlatform {
}
}

final appCheck = _services.remove('app-check');
if (appCheck != null) {
// Activate app check first
await appCheck.ensurePluginInitialized!(app!);
}

await Future.wait(
_services.values.map((service) {
final ensureInitializedFunction = service.ensurePluginInitialized;
Expand Down

0 comments on commit 493f254

Please sign in to comment.