Skip to content

Commit

Permalink
feat(app_check, mobile): add option to pass debugTokens in initializa…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
xVemu committed Aug 1, 2024
1 parent d41e30a commit b2aff73
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ 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.Arrays;
import java.util.Collections;
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.<Component<?>>asList(library, debugSecretProvider);
}

@Nullable
@Override
public String getDebugSecret() {
return debugToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@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 @@ -14,10 +14,19 @@ - (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 @@ -7,6 +7,6 @@

@property NSMutableDictionary *_Nullable providers;

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

@end
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,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 @@ -40,7 +42,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 @@ -118,9 +118,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[@"iosDebugToken"];

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 @@ -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 `iosDebugToken` allow you to set a debug token for the "debug" provider on Android and iOS respectively.
/// On iOS you have to rerun app after changing `iosDebugToken`.
///
/// 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? iosDebugToken,
}) {
return _delegate.activate(
webProvider: webProvider,
androidProvider: androidProvider,
appleProvider: appleProvider,
androidDebugToken: androidDebugToken,
iosDebugToken: iosDebugToken,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,21 @@ class MethodChannelFirebaseAppCheck extends FirebaseAppCheckPlatform {
WebProvider? webProvider,
AndroidProvider? androidProvider,
AppleProvider? appleProvider,
String? androidDebugToken,
String? iosDebugToken,
}) async {
try {
await channel.invokeMethod<void>('FirebaseAppCheck#activate', {
'appName': app.name,
// Allow value to pass for debug mode for unit testing
if (Platform.isAndroid || kDebugMode)
'androidProvider': getAndroidProviderString(androidProvider),
if(androidDebugToken != null)
'androidDebugToken': androidDebugToken,
if (Platform.isIOS || Platform.isMacOS || kDebugMode)
'appleProvider': getAppleProviderString(appleProvider),
if(iosDebugToken != null)
'iosDebugToken': iosDebugToken,
});
} on PlatformException catch (e, s) {
convertPlatformException(e, s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ abstract class FirebaseAppCheckPlatform extends PlatformInterface {
WebProvider? webProvider,
AndroidProvider? androidProvider,
AppleProvider? appleProvider,
String? androidDebugToken,
String? iosDebugToken,
}) {
throw UnimplementedError('activate() is not implemented');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 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 'dart:io';

import 'package:firebase_app_check/firebase_app_check.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -72,6 +74,34 @@ void main() {
},
skip: kIsWeb,
);

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

test(
'debugToken on iOS',
() async {
await expectLater(
FirebaseAppCheck.instance.activate(
iosDebugToken: 'debug_token',
appleProvider: AppleProvider.debug,
),
completes,
);
},
skip: !Platform.isIOS,
);
},
);
}

0 comments on commit b2aff73

Please sign in to comment.