Skip to content

Integrate AppCheckProviders with Firebase Components. #4436

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

Merged
merged 5 commits into from
Dec 12, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@
package="com.google.firebase.appcheck.debug.testing">
android:exported="false">
android:value="com.google.firebase.components.ComponentRegistrar" />
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@
package com.google.firebase.appcheck.debug.testing;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.test.platform.app.InstrumentationRegistry;
import com.google.firebase.FirebaseApp;
import com.google.firebase.appcheck.AppCheckProviderFactory;
import com.google.firebase.appcheck.FirebaseAppCheck;
import com.google.firebase.appcheck.debug.DebugAppCheckProviderFactory;
import com.google.firebase.appcheck.debug.DebugAppCheckProviderFactoryHelper;
import com.google.firebase.appcheck.internal.DefaultFirebaseAppCheck;

/**
Expand Down Expand Up @@ -66,28 +63,16 @@
*
*/
public final class DebugAppCheckTestHelper {
private static final String DEBUG_SECRET_KEY = "firebaseAppCheckDebugSecret";

private final String debugSecret;

/**
* Creates a {@link DebugAppCheckTestHelper} instance with the debug secret obtained from {@link
* InstrumentationRegistry} arguments.
* Creates a {@link DebugAppCheckTestHelper} instance with a debug secret obtained from {@link
* androidx.test.platform.app.InstrumentationRegistry} arguments.
*/
@NonNull
public static DebugAppCheckTestHelper fromInstrumentationArgs() {
String debugSecret = InstrumentationRegistry.getArguments().getString(DEBUG_SECRET_KEY);
return new DebugAppCheckTestHelper(debugSecret);
return new DebugAppCheckTestHelper();
}

@VisibleForTesting
static DebugAppCheckTestHelper fromString(String debugSecret) {
return new DebugAppCheckTestHelper(debugSecret);
}

private DebugAppCheckTestHelper(String debugSecret) {
this.debugSecret = debugSecret;
}
private DebugAppCheckTestHelper() {}

/**
* Installs a {@link DebugAppCheckProviderFactory} to the default {@link FirebaseApp} and runs the
Expand All @@ -109,8 +94,7 @@ public void withDebugProvider(
(DefaultFirebaseAppCheck) FirebaseAppCheck.getInstance(firebaseApp);
AppCheckProviderFactory currentAppCheckProviderFactory =
firebaseAppCheck.getInstalledAppCheckProviderFactory();
firebaseAppCheck.installAppCheckProviderFactory(
DebugAppCheckProviderFactoryHelper.createDebugAppCheckProviderFactory(debugSecret));
firebaseAppCheck.installAppCheckProviderFactory(DebugAppCheckProviderFactory.getInstance());
try {
runnable.run();
} catch (Throwable throwable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.appcheck.debug.testing;

import androidx.test.platform.app.InstrumentationRegistry;
import com.google.firebase.appcheck.debug.InternalDebugSecretProvider;

/** @hide */
public class DebugSecretProvider implements InternalDebugSecretProvider {
private static final String DEBUG_SECRET_KEY = "firebaseAppCheckDebugSecret";

DebugSecretProvider() {}

/**
* Returns a debug secret from {@link InstrumentationRegistry} arguments to be used with the
* {@link com.google.firebase.appcheck.debug.internal.DebugAppCheckProvider} in continuous
* integration testing flows.
*/
@Override
public String getDebugSecret() {
return InstrumentationRegistry.getArguments().getString(DEBUG_SECRET_KEY);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.appcheck.debug.testing;

import com.google.android.gms.common.annotation.KeepForSdk;
import com.google.firebase.appcheck.debug.BuildConfig;
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.List;

/**
* {@link ComponentRegistrar} for setting up FirebaseAppCheck debug testing's dependency injections
* in Firebase Android Components.
*
* @hide
*/
@KeepForSdk
public class FirebaseAppCheckDebugTestingRegistrar implements ComponentRegistrar {
private static final String LIBRARY_NAME = "fire-app-check-debug-testing";

@Override
public List> getComponents() {
return Arrays.asList(
Component.builder(DebugSecretProvider.class, (InternalDebugSecretProvider.class))
.name(LIBRARY_NAME)
.factory((container) -> new DebugSecretProvider())
.build(),
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ public class DebugAppCheckTestHelperTest {
private static final String PROJECT_ID = "projectId";
private static final String APP_ID = "appId";
private static final String OTHER_FIREBASE_APP_NAME = "otherFirebaseAppName";
private static final String DEBUG_SECRET = "debugSecret";

private final DebugAppCheckTestHelper debugAppCheckTestHelper =
DebugAppCheckTestHelper.fromString(DEBUG_SECRET);
DebugAppCheckTestHelper.fromInstrumentationArgs();

@Before
public void setUp() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.appcheck.debug.testing;

import static com.google.common.truth.Truth.assertThat;

import com.google.firebase.components.Component;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

/** Tests for {@link FirebaseAppCheckDebugTestingRegistrar}. */
@RunWith(RobolectricTestRunner.class)
public class FirebaseAppCheckDebugTestingRegistrarTest {
@Test
public void testGetComponents() {
FirebaseAppCheckDebugTestingRegistrar registrar = new FirebaseAppCheckDebugTestingRegistrar();
List> components = registrar.getComponents();
assertThat(components).isNotEmpty();
assertThat(components).hasSize(2);
Component appCheckDebugTestingComponent = components.get(0);
assertThat(appCheckDebugTestingComponent.getDependencies()).isEmpty();
assertThat(appCheckDebugTestingComponent.isLazy()).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,7 @@ public class DebugAppCheckProviderFactory implements AppCheckProviderFactory {

private static final DebugAppCheckProviderFactory instance = new DebugAppCheckProviderFactory();

private String debugSecret;

private DebugAppCheckProviderFactory() {
this.debugSecret = null;
}

/**
* This constructor is package-private in order to prevent debug secrets from being hard-coded in
* application logic. This constructor is used by the firebase-appcheck-debug-testing SDK, to
* inject debug secrets in integration tests.
*/
DebugAppCheckProviderFactory(String debugSecret) {
this.debugSecret = debugSecret;
}
private DebugAppCheckProviderFactory() {}

/**
* Gets an instance of this class for installation into a {@link
Expand All @@ -55,7 +42,8 @@ public static DebugAppCheckProviderFactory getInstance() {

@NonNull
@Override
@SuppressWarnings("FirebaseUseExplicitDependencies")
public AppCheckProvider create(@NonNull FirebaseApp firebaseApp) {
return new DebugAppCheckProvider(firebaseApp, debugSecret);
return firebaseApp.get(DebugAppCheckProvider.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package com.google.firebase.appcheck.debug;

import com.google.android.gms.common.annotation.KeepForSdk;
import com.google.firebase.FirebaseApp;
import com.google.firebase.appcheck.debug.internal.DebugAppCheckProvider;
import com.google.firebase.components.Component;
import com.google.firebase.components.ComponentRegistrar;
import com.google.firebase.components.Dependency;
import com.google.firebase.platforminfo.LibraryVersionComponent;
import java.util.Arrays;
import java.util.List;
Expand All @@ -33,6 +36,17 @@ public class FirebaseAppCheckDebugRegistrar implements ComponentRegistrar {

@Override
public List> getComponents() {
return Arrays.asList(LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME));
return Arrays.asList(
Component.builder(DebugAppCheckProvider.class)
.name(LIBRARY_NAME)
.add(Dependency.required(FirebaseApp.class))
.add(Dependency.optionalProvider(InternalDebugSecretProvider.class))
.factory(
(container) ->
new DebugAppCheckProvider(
container.get(FirebaseApp.class),
container.getProvider(InternalDebugSecretProvider.class)))
.build(),
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Google LLC
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -14,19 +14,15 @@

package com.google.firebase.appcheck.debug;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
* Helper class used by {@link com.google.firebase.appcheck.debug.testing.DebugAppCheckTestHelper}
* in order to access the package-private {@link DebugAppCheckProviderFactory} constructor that
* takes in a debug secret.
* An interface for obtaining a debug secret to be used with {@link
* com.google.firebase.appcheck.debug.internal.DebugAppCheckProvider}.
*
* @hide
*/
public class DebugAppCheckProviderFactoryHelper {
@NonNull
public static DebugAppCheckProviderFactory createDebugAppCheckProviderFactory(
@NonNull String debugSecret) {
return new DebugAppCheckProviderFactory(debugSecret);
}
public interface InternalDebugSecretProvider {
@Nullable
String getDebugSecret();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@
import android.annotation.SuppressLint;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApp;
import com.google.firebase.appcheck.AppCheckProvider;
import com.google.firebase.appcheck.AppCheckToken;
import com.google.firebase.appcheck.debug.InternalDebugSecretProvider;
import com.google.firebase.appcheck.internal.DefaultAppCheckToken;
import com.google.firebase.appcheck.internal.NetworkClient;
import com.google.firebase.appcheck.internal.RetryManager;
import com.google.firebase.inject.Provider;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -46,11 +47,18 @@ public class DebugAppCheckProvider implements AppCheckProvider {

// TODO(b/258273630): Migrate to go/firebase-android-executors
@SuppressLint("ThreadPoolCreation")
public DebugAppCheckProvider(@NonNull FirebaseApp firebaseApp, @Nullable String debugSecret) {
public DebugAppCheckProvider(
@NonNull FirebaseApp firebaseApp,
@NonNull Provider debugSecretProvider) {
checkNotNull(firebaseApp);
this.networkClient = new NetworkClient(firebaseApp);
this.backgroundExecutor = Executors.newCachedThreadPool();
this.retryManager = new RetryManager();

String debugSecret = null;
if (debugSecretProvider.get() != null) {
debugSecret = debugSecretProvider.get().getDebugSecret();
}
this.debugSecretTask =
debugSecret == null
? determineDebugSecret(firebaseApp, this.backgroundExecutor)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.appcheck.debug;

import static com.google.common.truth.Truth.assertThat;

import com.google.firebase.FirebaseApp;
import com.google.firebase.components.Component;
import com.google.firebase.components.Dependency;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

/** Tests for {@link FirebaseAppCheckDebugRegistrar}. */
@RunWith(RobolectricTestRunner.class)
public class FirebaseAppCheckDebugRegistrarTest {
@Test
public void testGetComponents() {
FirebaseAppCheckDebugRegistrar registrar = new FirebaseAppCheckDebugRegistrar();
List> components = registrar.getComponents();
assertThat(components).isNotEmpty();
assertThat(components).hasSize(2);
Component appCheckDebugComponent = components.get(0);
assertThat(appCheckDebugComponent.getDependencies())
.containsExactly(
Dependency.required(FirebaseApp.class),
Dependency.optionalProvider(InternalDebugSecretProvider.class));
assertThat(appCheckDebugComponent.isLazy()).isTrue();
}
}
Loading