Skip to content

Commit

Permalink
Migrate remaining App Check SDKs to go/firebase-android-executors. (#…
Browse files Browse the repository at this point in the history
…4449)

* Migrate `firebase-appcheck-safetynet` to go/firebase-android-executors.

* Migrate `firebase-appcheck-playintegrity` to go/firebase-android-executors.

* Migrate `firebase-appcheck-debug` to go/firebase-android-executors.

* Remove unnecessary class field from `SafetyNetAppCheckProvider`.

* Update changelogs.
  • Loading branch information
rosalyntan committed Dec 13, 2022
1 parent 02dd166 commit f133454
Show file tree
Hide file tree
Showing 20 changed files with 152 additions and 72 deletions.
1 change: 1 addition & 0 deletions appcheck/firebase-appcheck-debug-testing/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Unreleased
* [changed] Integrated the [app_check] Debug Testing SDK with Firebase Components. (#4436)

# 16.1.0
* [unchanged] Updated to accommodate the release of the updated
Expand Down
2 changes: 2 additions & 0 deletions appcheck/firebase-appcheck-debug/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Unreleased
* [changed] Migrated [app_check] SDKs to use standard Firebase executors. (#4431, #4449)
* [changed] Integrated the [app_check] Debug SDK with Firebase Components. (#4436)

# 16.1.0
* [unchanged] Updated to accommodate the release of the updated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ android {
}

dependencies {
implementation project(':firebase-annotations')
implementation project(':firebase-common')
implementation project(':firebase-components')
implementation project(':appcheck:firebase-appcheck')
Expand All @@ -49,6 +50,7 @@ dependencies {

javadocClasspath 'com.google.auto.value:auto-value-annotations:1.6.6'

testImplementation project(':integ-testing')
testImplementation 'junit:junit:4.13-beta-2'
testImplementation 'org.mockito:mockito-core:2.25.0'
testImplementation "org.robolectric:robolectric:$robolectricVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@

import com.google.android.gms.common.annotation.KeepForSdk;
import com.google.firebase.FirebaseApp;
import com.google.firebase.annotations.concurrent.Background;
import com.google.firebase.annotations.concurrent.Blocking;
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.components.Qualified;
import com.google.firebase.platforminfo.LibraryVersionComponent;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;

/**
* {@link ComponentRegistrar} for setting up FirebaseAppCheck debug's dependency injections in
Expand All @@ -36,16 +40,23 @@ public class FirebaseAppCheckDebugRegistrar implements ComponentRegistrar {

@Override
public List<Component<?>> getComponents() {
Qualified<Executor> backgroundExecutor = Qualified.qualified(Background.class, Executor.class);
Qualified<Executor> blockingExecutor = Qualified.qualified(Blocking.class, Executor.class);

return Arrays.asList(
Component.builder(DebugAppCheckProvider.class)
.name(LIBRARY_NAME)
.add(Dependency.required(FirebaseApp.class))
.add(Dependency.optionalProvider(InternalDebugSecretProvider.class))
.add(Dependency.required(backgroundExecutor))
.add(Dependency.required(blockingExecutor))
.factory(
(container) ->
new DebugAppCheckProvider(
container.get(FirebaseApp.class),
container.getProvider(InternalDebugSecretProvider.class)))
container.getProvider(InternalDebugSecretProvider.class),
container.get(backgroundExecutor),
container.get(blockingExecutor)))
.build(),
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.google.android.gms.tasks.TaskCompletionSource;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApp;
import com.google.firebase.annotations.concurrent.Background;
import com.google.firebase.annotations.concurrent.Blocking;
import com.google.firebase.appcheck.AppCheckProvider;
import com.google.firebase.appcheck.AppCheckToken;
import com.google.firebase.appcheck.debug.InternalDebugSecretProvider;
Expand All @@ -32,27 +34,26 @@
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;
import java.util.concurrent.Executor;

public class DebugAppCheckProvider implements AppCheckProvider {

private static final String TAG = DebugAppCheckProvider.class.getName();
private static final String UTF_8 = "UTF-8";

private final NetworkClient networkClient;
private final ExecutorService backgroundExecutor;
private final Executor blockingExecutor;
private final RetryManager retryManager;
private final Task<String> debugSecretTask;

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

String debugSecret = null;
Expand All @@ -61,26 +62,26 @@ public DebugAppCheckProvider(
}
this.debugSecretTask =
debugSecret == null
? determineDebugSecret(firebaseApp, this.backgroundExecutor)
? determineDebugSecret(firebaseApp, backgroundExecutor)
: Tasks.forResult(debugSecret);
}

@VisibleForTesting
DebugAppCheckProvider(
@NonNull String debugSecret,
@NonNull NetworkClient networkClient,
@NonNull ExecutorService backgroundExecutor,
@NonNull Executor blockingExecutor,
@NonNull RetryManager retryManager) {
this.networkClient = networkClient;
this.backgroundExecutor = backgroundExecutor;
this.blockingExecutor = blockingExecutor;
this.retryManager = retryManager;
this.debugSecretTask = Tasks.forResult(debugSecret);
}

@VisibleForTesting
@NonNull
static Task<String> determineDebugSecret(
@NonNull FirebaseApp firebaseApp, @NonNull ExecutorService executor) {
@NonNull FirebaseApp firebaseApp, @NonNull Executor executor) {
TaskCompletionSource<String> taskCompletionSource = new TaskCompletionSource<>();
executor.execute(
() -> {
Expand Down Expand Up @@ -111,7 +112,7 @@ public Task<AppCheckToken> getToken() {
task -> {
ExchangeDebugTokenRequest request = new ExchangeDebugTokenRequest(task.getResult());
return Tasks.call(
backgroundExecutor,
blockingExecutor,
() ->
networkClient.exchangeAttestationForAppCheckToken(
request.toJsonString().getBytes(UTF_8),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
import static com.google.common.truth.Truth.assertThat;

import com.google.firebase.FirebaseApp;
import com.google.firebase.annotations.concurrent.Background;
import com.google.firebase.annotations.concurrent.Blocking;
import com.google.firebase.components.Component;
import com.google.firebase.components.Dependency;
import com.google.firebase.components.Qualified;
import java.util.List;
import java.util.concurrent.Executor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
Expand All @@ -37,7 +41,9 @@ public void testGetComponents() {
assertThat(appCheckDebugComponent.getDependencies())
.containsExactly(
Dependency.required(FirebaseApp.class),
Dependency.optionalProvider(InternalDebugSecretProvider.class));
Dependency.optionalProvider(InternalDebugSecretProvider.class),
Dependency.required(Qualified.qualified(Background.class, Executor.class)),
Dependency.required(Qualified.qualified(Blocking.class, Executor.class)));
assertThat(appCheckDebugComponent.isLazy()).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
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.concurrent.TestOnlyExecutors;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executor;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -72,7 +73,8 @@ public class DebugAppCheckProviderTest {

private StorageHelper storageHelper;
private SharedPreferences sharedPreferences;
private ExecutorService backgroundExecutor = MoreExecutors.newDirectExecutorService();
// TODO(b/258273630): Use TestOnlyExecutors instead of MoreExecutors.directExecutor().
private Executor executor = MoreExecutors.directExecutor();

@Before
public void setup() {
Expand Down Expand Up @@ -100,7 +102,8 @@ public void testPublicConstructor_nullFirebaseApp_expectThrows() {
assertThrows(
NullPointerException.class,
() -> {
new DebugAppCheckProvider(null, null);
new DebugAppCheckProvider(
null, null, TestOnlyExecutors.background(), TestOnlyExecutors.blocking());
});
}

Expand All @@ -110,7 +113,7 @@ public void testDetermineDebugSecret_noStoredSecret_createsNewSecret() {
assertThat(storageHelper.retrieveDebugSecret()).isNull();

Task<String> debugSecretTask =
DebugAppCheckProvider.determineDebugSecret(mockFirebaseApp, backgroundExecutor);
DebugAppCheckProvider.determineDebugSecret(mockFirebaseApp, executor);
assertThat(storageHelper.retrieveDebugSecret()).isNotNull();
assertThat(storageHelper.retrieveDebugSecret()).isEqualTo(debugSecretTask.getResult());
}
Expand All @@ -120,7 +123,7 @@ public void testDetermineDebugSecret_storedSecret_usesExistingSecret() {
storageHelper.saveDebugSecret(DEBUG_SECRET);

Task<String> debugSecretTask =
DebugAppCheckProvider.determineDebugSecret(mockFirebaseApp, backgroundExecutor);
DebugAppCheckProvider.determineDebugSecret(mockFirebaseApp, executor);
assertThat(debugSecretTask.getResult()).isEqualTo(DEBUG_SECRET);
assertThat(storageHelper.retrieveDebugSecret()).isEqualTo(DEBUG_SECRET);
}
Expand All @@ -134,8 +137,7 @@ public void exchangeDebugToken_onSuccess_setsTaskResult() throws Exception {
when(mockAppCheckTokenResponse.getTimeToLive()).thenReturn(TIME_TO_LIVE);

DebugAppCheckProvider provider =
new DebugAppCheckProvider(
DEBUG_SECRET, mockNetworkClient, backgroundExecutor, mockRetryManager);
new DebugAppCheckProvider(DEBUG_SECRET, mockNetworkClient, executor, mockRetryManager);
Task<AppCheckToken> task = provider.getToken();

verify(mockNetworkClient)
Expand All @@ -153,8 +155,7 @@ public void exchangeDebugToken_onFailure_setsTaskException() throws Exception {
.thenThrow(new IOException());

DebugAppCheckProvider provider =
new DebugAppCheckProvider(
DEBUG_SECRET, mockNetworkClient, backgroundExecutor, mockRetryManager);
new DebugAppCheckProvider(DEBUG_SECRET, mockNetworkClient, executor, mockRetryManager);
Task<AppCheckToken> task = provider.getToken();

verify(mockNetworkClient)
Expand Down
2 changes: 2 additions & 0 deletions appcheck/firebase-appcheck-playintegrity/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Unreleased
* [changed] Migrated [app_check] SDKs to use standard Firebase executors. (#4431, #4449)
* [changed] Integrated the [app_check] Play Integrity SDK with Firebase Components. (#4436)

# 16.1.0
* [unchanged] Updated to accommodate the release of the updated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ android {
}

dependencies {
implementation project(':firebase-annotations')
implementation project(':firebase-common')
implementation project(':firebase-components')
implementation project(':appcheck:firebase-appcheck')
Expand All @@ -50,6 +51,7 @@ dependencies {

javadocClasspath 'com.google.auto.value:auto-value-annotations:1.6.6'

testImplementation project(':integ-testing')
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:3.4.6'
testImplementation "com.google.truth:truth:$googleTruthVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

import com.google.android.gms.common.annotation.KeepForSdk;
import com.google.firebase.FirebaseApp;
import com.google.firebase.annotations.concurrent.Blocking;
import com.google.firebase.appcheck.playintegrity.internal.PlayIntegrityAppCheckProvider;
import com.google.firebase.components.Component;
import com.google.firebase.components.ComponentRegistrar;
import com.google.firebase.components.Dependency;
import com.google.firebase.components.Qualified;
import com.google.firebase.platforminfo.LibraryVersionComponent;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;

/**
* {@link ComponentRegistrar} for setting up FirebaseAppCheck play integrity's dependency injections
Expand All @@ -36,12 +39,17 @@ public class FirebaseAppCheckPlayIntegrityRegistrar implements ComponentRegistra

@Override
public List<Component<?>> getComponents() {
Qualified<Executor> blockingExecutor = Qualified.qualified(Blocking.class, Executor.class);

return Arrays.asList(
Component.builder(PlayIntegrityAppCheckProvider.class)
.name(LIBRARY_NAME)
.add(Dependency.required(FirebaseApp.class))
.add(Dependency.required(blockingExecutor))
.factory(
(container) -> new PlayIntegrityAppCheckProvider(container.get(FirebaseApp.class)))
(container) ->
new PlayIntegrityAppCheckProvider(
container.get(FirebaseApp.class), container.get(blockingExecutor)))
.build(),
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
import com.google.android.play.core.integrity.IntegrityTokenRequest;
import com.google.android.play.core.integrity.IntegrityTokenResponse;
import com.google.firebase.FirebaseApp;
import com.google.firebase.annotations.concurrent.Blocking;
import com.google.firebase.appcheck.AppCheckProvider;
import com.google.firebase.appcheck.AppCheckToken;
import com.google.firebase.appcheck.internal.DefaultAppCheckToken;
import com.google.firebase.appcheck.internal.NetworkClient;
import com.google.firebase.appcheck.internal.RetryManager;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Executor;

public class PlayIntegrityAppCheckProvider implements AppCheckProvider {

Expand All @@ -39,17 +39,16 @@ public class PlayIntegrityAppCheckProvider implements AppCheckProvider {
private final String projectNumber;
private final IntegrityManager integrityManager;
private final NetworkClient networkClient;
private final ExecutorService backgroundExecutor;
private final Executor blockingExecutor;
private final RetryManager retryManager;

// TODO(b/258273630): Migrate to go/firebase-android-executors
@SuppressLint("ThreadPoolCreation")
public PlayIntegrityAppCheckProvider(@NonNull FirebaseApp firebaseApp) {
public PlayIntegrityAppCheckProvider(
@NonNull FirebaseApp firebaseApp, @Blocking Executor blockingExecutor) {
this(
firebaseApp.getOptions().getGcmSenderId(),
IntegrityManagerFactory.create(firebaseApp.getApplicationContext()),
new NetworkClient(firebaseApp),
Executors.newCachedThreadPool(),
blockingExecutor,
new RetryManager());
}

Expand All @@ -58,12 +57,12 @@ public PlayIntegrityAppCheckProvider(@NonNull FirebaseApp firebaseApp) {
@NonNull String projectNumber,
@NonNull IntegrityManager integrityManager,
@NonNull NetworkClient networkClient,
@NonNull ExecutorService backgroundExecutor,
@NonNull Executor blockingExecutor,
@NonNull RetryManager retryManager) {
this.projectNumber = projectNumber;
this.integrityManager = integrityManager;
this.networkClient = networkClient;
this.backgroundExecutor = backgroundExecutor;
this.blockingExecutor = blockingExecutor;
this.retryManager = retryManager;
}

Expand All @@ -78,7 +77,7 @@ public Task<AppCheckToken> getToken() {
ExchangePlayIntegrityTokenRequest request =
new ExchangePlayIntegrityTokenRequest(integrityTokenResponse.token());
return Tasks.call(
backgroundExecutor,
blockingExecutor,
() ->
networkClient.exchangeAttestationForAppCheckToken(
request.toJsonString().getBytes(UTF_8),
Expand All @@ -100,7 +99,7 @@ private Task<IntegrityTokenResponse> getPlayIntegrityAttestation() {
new GeneratePlayIntegrityChallengeRequest();
Task<GeneratePlayIntegrityChallengeResponse> generateChallengeTask =
Tasks.call(
backgroundExecutor,
blockingExecutor,
() ->
GeneratePlayIntegrityChallengeResponse.fromJsonString(
networkClient.generatePlayIntegrityChallenge(
Expand Down
Loading

0 comments on commit f133454

Please sign in to comment.