Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(app-check): Debug token support for the activate method #16942

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions docs/app-check/debug-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,48 @@ Because this token allows access to your Firebase resources without a
valid device, it is crucial that you keep it private. Don't commit it to a
public repository, and if a registered token is ever compromised, revoke it
immediately in the Firebase console.

## Manually setting up the App Check Debug Token for CI environment or development

If you want to use the debug provider in a testing environment or CI, you can
manually set the debug token in your app. This is useful when you want to run
your app in an environment where the debug token is not automatically generated.

To manually set the debug token, use the `androidDebugToken` and `appleDebugToken`
parameters when activating App Check. For example:

```dart
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';

// Import the firebase_app_check plugin
import 'package:firebase_app_check/firebase_app_check.dart';

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await FirebaseAppCheck.instance.activate(
webRecaptchaSiteKey: 'recaptcha-v3-site-key',
// Set androidProvider to `AndroidProvider.debug`
androidProvider: AndroidProvider.debug,
// Set appleProvider to `AppleProvider.debug`
appleProvider: AppleProvider.debug,
// Set the androidDebugToken for Android
androidDebugToken: '123a4567-b89c-12d3-e456-789012345678',
// Set the appleDebugToken for Apple platforms
appleDebugToken: '123a4567-b89c-12d3-e456-789012345678',
);
runApp(App());
}

```

{# Google-internal common file: #}
<<../_includes/manage-debug-tokens.md>>

After you register the token, Firebase backend services will accept it as valid.

Because this token allows access to your Firebase resources without a
valid device, it is crucial that you keep it private. Don't commit it to a
public repository, and if a registered token is ever compromised, revoke it
immediately in the Firebase console.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ private Task<Void> activate(Map<String, Object> arguments) {
case debugProvider:
{
FirebaseAppCheck firebaseAppCheck = getAppCheck(arguments);
FlutterFirebaseAppRegistrar.debugToken =
(String) arguments.get("androidDebugToken");
firebaseAppCheck.installAppCheckProviderFactory(
DebugAppCheckProviderFactory.getInstance());
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,38 @@
package io.flutter.plugins.firebase.appcheck;

import androidx.annotation.Keep;
import androidx.annotation.Nullable;
import com.google.firebase.appcheck.debug.InternalDebugSecretProvider;
import com.google.firebase.components.Component;
import com.google.firebase.components.ComponentRegistrar;
import com.google.firebase.platforminfo.LibraryVersionComponent;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;

@Keep
public class FlutterFirebaseAppRegistrar implements ComponentRegistrar {
public class FlutterFirebaseAppRegistrar
implements ComponentRegistrar, InternalDebugSecretProvider {

private static final String DEBUG_SECRET_NAME = "fire-app-check-debug-secret";
public static String debugToken;

@Override
public List<Component<?>> getComponents() {
return Collections.<Component<?>>singletonList(
LibraryVersionComponent.create(BuildConfig.LIBRARY_NAME, BuildConfig.LIBRARY_VERSION));
Component<?> library =
LibraryVersionComponent.create(BuildConfig.LIBRARY_NAME, BuildConfig.LIBRARY_VERSION);

Component<InternalDebugSecretProvider> debugSecretProvider =
Component.builder(InternalDebugSecretProvider.class)
.name(DEBUG_SECRET_NAME)
.factory(container -> this)
.build();

return Arrays.asList(library, debugSecretProvider);
}

@Nullable
@Override
public String getDebugSecret() {
return debugToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Future<void> main() async {
androidProvider: AndroidProvider.debug,
appleProvider: AppleProvider.debug,
webProvider: ReCaptchaV3Provider(kWebRecaptchaSiteKey),
androidDebugToken: 'your-debug-token',
appleDebugToken: 'your-debug-token',
);

runApp(MyApp());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ - (id)initWithApp:app {
return self;
}

- (void)configure:(FIRApp *)app providerName:(NSString *)providerName {
- (void)configure:(FIRApp *)app
providerName:(NSString *)providerName
debugToken:(NSString *)debugToken {
if ([providerName isEqualToString:@"debug"]) {
if (debugToken != nil) {
// We have a debug token, so just need to stuff it in the environment and it will hook up
char *key = "FIRAAppCheckDebugToken", *value = (char *)[debugToken UTF8String];
int overwrite = 1;
setenv(key, value, overwrite);
}
FIRAppCheckDebugProvider *provider = [[FIRAppCheckDebugProvider alloc] initWithApp:app];
NSLog(@"Firebase App Check Debug Token: %@", [provider localDebugToken]);
if (debugToken == nil) NSLog(@"Firebase App Check Debug Token: %@", [provider localDebugToken]);
self.delegateProvider = provider;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ @implementation FLTAppCheckProviderFactory
self.providers[app.name] = [FLTAppCheckProvider new];
FLTAppCheckProvider *provider = self.providers[app.name];
// We set "deviceCheck" as this is currently what is default. Backward compatible.
[provider configure:app providerName:@"deviceCheck"];
[provider configure:app providerName:@"deviceCheck" debugToken:nil];
}

return self.providers[app.name];
}

- (void)configure:(FIRApp *)app providerName:(NSString *)providerName {
- (void)configure:(FIRApp *)app
providerName:(NSString *)providerName
debugToken:(NSString *)debugToken {
if (self.providers == nil) {
self.providers = [NSMutableDictionary new];
}
Expand All @@ -41,7 +43,7 @@ - (void)configure:(FIRApp *)app providerName:(NSString *)providerName {
}

FLTAppCheckProvider *provider = self.providers[app.name];
[provider configure:app providerName:providerName];
[provider configure:app providerName:providerName debugToken:debugToken];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)flutter
- (void)activate:(id)arguments withMethodCallResult:(FLTFirebaseMethodCallResult *)result {
NSString *appNameDart = arguments[@"appName"];
NSString *providerName = arguments[@"appleProvider"];
NSString *debugToken = arguments[@"appleDebugToken"];

FIRApp *app = [FLTFirebasePlugin firebaseAppNamed:appNameDart];
[self->providerFactory configure:app providerName:providerName];
[self->providerFactory configure:app providerName:providerName debugToken:debugToken];
result.success(nil);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

@property id<FIRAppCheckProvider> delegateProvider;

- (void)configure:(FIRApp *)app providerName:(NSString *)providerName;
- (void)configure:(FIRApp *)app
providerName:(NSString *)providerName
debugToken:(NSString *)debugToken;

- (id)initWithApp:(FIRApp *)app;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

@property NSMutableDictionary *_Nullable providers;

- (void)configure:(FIRApp *_Nonnull)app providerName:(NSString *_Nonnull)providerName;
- (void)configure:(FIRApp *_Nonnull)app
providerName:(NSString *_Nonnull)providerName
debugToken:(NSString *_Nullable)debugToken;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,23 @@ class FirebaseAppCheck extends FirebasePluginPlatform {
/// On iOS or macOS, the default provider is "device check". If you wish to set the provider to "app attest", "debug" or "app attest with fallback to device check"
/// ("app attest" is only available on iOS 14.0+, macOS 14.0+), you may set the `appleProvider` property using the `AppleProvider` enum
///
/// `androidDebugToken` and `appleDebugToken` allow you to set a debug token for the "debug" provider on Android and iOS respectively.
/// On iOS you have to re-run app after changing `appleDebugToken`.
///
/// For more information, see [the Firebase Documentation](https://firebase.google.com/docs/app-check)
Future<void> activate({
WebProvider? webProvider,
AndroidProvider androidProvider = AndroidProvider.playIntegrity,
AppleProvider appleProvider = AppleProvider.deviceCheck,
String? androidDebugToken,
String? appleDebugToken,
}) {
return _delegate.activate(
webProvider: webProvider,
androidProvider: androidProvider,
appleProvider: appleProvider,
androidDebugToken: androidDebugToken,
appleDebugToken: appleDebugToken,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ void main() {
test('successful call', () async {
await appCheck.activate(
webProvider: ReCaptchaV3Provider('key'),
androidDebugToken: 'androidDebug',
appleDebugToken: 'appleDebug',
);

expect(
Expand All @@ -68,6 +70,8 @@ void main() {
'appName': defaultFirebaseAppName,
'androidProvider': 'playIntegrity',
'appleProvider': 'deviceCheck',
'androidDebugToken': 'androidDebug',
'appleDebugToken': 'appleDebug',
},
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,21 @@ class MethodChannelFirebaseAppCheck extends FirebaseAppCheckPlatform {
WebProvider? webProvider,
AndroidProvider? androidProvider,
AppleProvider? appleProvider,
String? androidDebugToken,
String? appleDebugToken,
}) async {
try {
await channel.invokeMethod<void>('FirebaseAppCheck#activate', {
'appName': app.name,
// Allow value to pass for debug mode for unit testing
if (defaultTargetPlatform == TargetPlatform.android || kDebugMode)
'androidProvider': getAndroidProviderString(androidProvider),
if (androidDebugToken != null) 'androidDebugToken': androidDebugToken,
if (defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.macOS ||
kDebugMode)
'appleProvider': getAppleProviderString(appleProvider),
if (appleDebugToken != null) 'appleDebugToken': appleDebugToken,
});
} on PlatformException catch (e, s) {
convertPlatformException(e, s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,16 @@ abstract class FirebaseAppCheckPlatform extends PlatformInterface {
/// On iOS or macOS, the default provider is "device check". If you wish to set the provider to "app attest", "debug" or "app attest with fallback to device check"
/// ("app attest" is only available on iOS 14.0+, macOS 14.0+), you may set the `appleProvider` property using the `AppleProvider` enum
///
/// `androidDebugToken` and `appleDebugToken` allow you to set a debug token for the "debug" provider on Android and iOS respectively.
/// On iOS you have to re-run app after changing `appleDebugToken`.
///
/// For more information, see [the Firebase Documentation](https://firebase.google.com/docs/app-check)
Future<void> activate({
WebProvider? webProvider,
AndroidProvider? androidProvider,
AppleProvider? appleProvider,
String? androidDebugToken,
String? appleDebugToken,
}) {
throw UnimplementedError('activate() is not implemented');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ void main() {
test('activate', () async {
await appCheck.activate(
webProvider: ReCaptchaV3Provider('test-key'),
androidDebugToken: 'androidDebug',
appleDebugToken: 'appleDebug',
);
expect(
methodCallLogger,
Expand All @@ -79,6 +81,8 @@ void main() {
'appName': defaultFirebaseAppName,
'androidProvider': 'playIntegrity',
'appleProvider': 'deviceCheck',
'androidDebugToken': 'androidDebug',
'appleDebugToken': 'appleDebug',
},
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class FirebaseAppCheckWeb extends FirebaseAppCheckPlatform {
WebProvider? webProvider,
AndroidProvider? androidProvider,
AppleProvider? appleProvider,
String? androidDebugToken,
String? appleDebugToken,
}) async {
// save the recaptcha type and site key for future startups
if (webProvider != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ class MockFirebaseAppCheckWeb extends _i1.Mock
_i3.WebProvider? webProvider,
_i3.AndroidProvider? androidProvider,
_i3.AppleProvider? appleProvider,
String? androidDebugToken,
String? appleDebugToken,
}) =>
(super.noSuchMethod(
Invocation.method(
Expand All @@ -140,6 +142,8 @@ class MockFirebaseAppCheckWeb extends _i1.Mock
#webProvider: webProvider,
#androidProvider: androidProvider,
#appleProvider: appleProvider,
#androidDebugToken: androidDebugToken,
#appleDebugToken: appleDebugToken,
},
),
returnValue: _i5.Future<void>.value(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,8 @@ class MockFirebaseAppCheck extends _i1.Mock implements _i10.FirebaseAppCheck {
_i11.WebProvider? webProvider,
_i11.AndroidProvider? androidProvider = _i11.AndroidProvider.playIntegrity,
_i11.AppleProvider? appleProvider = _i11.AppleProvider.deviceCheck,
String? androidDebugToken,
String? appleDebugToken,
}) =>
(super.noSuchMethod(
Invocation.method(
Expand All @@ -790,6 +792,8 @@ class MockFirebaseAppCheck extends _i1.Mock implements _i10.FirebaseAppCheck {
#webProvider: webProvider,
#androidProvider: androidProvider,
#appleProvider: appleProvider,
#androidDebugToken: androidDebugToken,
#appleDebugToken: appleDebugToken,
},
),
returnValue: _i6.Future<void>.value(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,34 @@ void main() {
},
skip: kIsWeb,
);

test(
'debugToken on Android',
() async {
await expectLater(
FirebaseAppCheck.instance.activate(
androidProvider: AndroidProvider.debug,
androidDebugToken: 'debug_token',
),
completes,
);
},
skip: defaultTargetPlatform != TargetPlatform.android,
);

test(
'debugToken on iOS',
() async {
await expectLater(
FirebaseAppCheck.instance.activate(
appleProvider: AppleProvider.debug,
appleDebugToken: 'debug_token',
),
completes,
);
},
skip: defaultTargetPlatform != TargetPlatform.iOS,
);
},
);
}
Loading