Skip to content

feat(appcheck): Implement reCAPTCHA Enterprise App Check provider #7125

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 24 commits into from
Jul 25, 2025
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e193c14
WIP: feat(appcheck): Implement initial reCAPTCHA Enterprise provider
hiteshmaurya56 Jul 8, 2025
5288e7c
WIP: feat(appcheck-recaptcha-enterprise): Add token exchange models a…
hiteshmaurya56 Jul 8, 2025
c25340b
WIP: feat(appcheck-network): Extend NetworkClient for reCAPTCHA Enter…
hiteshmaurya56 Jul 8, 2025
86c3bbb
WIP: feat(appcheck-network): Extend NetworkClient for reCAPTCHA Enter…
hiteshmaurya56 Jul 8, 2025
68ac173
WIP: feat(appcheck-recaptcha-enterprise): Implement RecaptchaEnterpri…
hiteshmaurya56 Jul 8, 2025
014dc4a
WIP: chore(appcheck-recaptcha-enterprise): Integrate module into buil…
hiteshmaurya56 Jul 8, 2025
2d2b96d
test(appcheck-recaptcha-enterprise): Add unit tests for reCAPTCHA Ent…
hiteshmaurya56 Jul 9, 2025
7a99c47
Resolves comments
hiteshmaurya56 Jul 11, 2025
c8e295b
Resolves comments
hiteshmaurya56 Jul 11, 2025
5f925b4
feat: Add new library versions
hiteshmaurya56 Jul 14, 2025
9c34a6e
Resolves build errors
hiteshmaurya56 Jul 14, 2025
290a9fb
Fix: Apply Spotless formatting to appcheck-recaptchaenterprise module
hiteshmaurya56 Jul 14, 2025
b6dea8d
Fix: Apply Spotless formatting to appcheck-recaptchaenterprise module
hiteshmaurya56 Jul 14, 2025
ff30974
Fix: Add api.txt file
hiteshmaurya56 Jul 14, 2025
874ef41
Resolves comments
hiteshmaurya56 Jul 16, 2025
f30a68e
Resolves comments
hiteshmaurya56 Jul 17, 2025
24056ce
Resolves comments
hiteshmaurya56 Jul 17, 2025
4027c4d
Resolves comments
hiteshmaurya56 Jul 18, 2025
97fc25d
Corrects Formatting
hiteshmaurya56 Jul 18, 2025
1f56a6a
Resolves Comments
hiteshmaurya56 Jul 22, 2025
8d0f5ee
Removes Map from RecaptchaEnterpriseAppCheckProviderFactory
hiteshmaurya56 Jul 23, 2025
beac8e3
Resolves Spotless formatting issue
hiteshmaurya56 Jul 24, 2025
b99b2fa
Merge branch 'main' into android-recaptchaenterprise
hiteshmaurya56 Jul 24, 2025
4160a2a
Adds existing_api.txt file
hiteshmaurya56 Jul 24, 2025
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
10 changes: 10 additions & 0 deletions appcheck/firebase-appcheck-recaptchaenterprise/api.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Signature format: 3.0
package com.google.firebase.appcheck.recaptchaenterprise {

public class RecaptchaEnterpriseAppCheckProviderFactory implements com.google.firebase.appcheck.AppCheckProviderFactory {
method public com.google.firebase.appcheck.AppCheckProvider create(com.google.firebase.FirebaseApp);
method public static com.google.firebase.appcheck.recaptchaenterprise.RecaptchaEnterpriseAppCheckProviderFactory getInstance(String);
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
plugins {
id 'firebase-library'
}

firebaseLibrary {
libraryGroup = "appcheck"
releaseNotes {
name.set("{{app_check}} Recaptcha Enterprise")
versionName.set("appcheck-recaptchaenterprise")
}
}

android {
adbOptions {
timeOutInMs 60 * 1000
}

namespace "com.google.firebase.appcheck.recaptchaenterprise"
compileSdkVersion project.compileSdkVersion
defaultConfig {
targetSdkVersion project.targetSdkVersion
minSdkVersion project.minSdkVersion
versionName version
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

testOptions.unitTests.includeAndroidResources = false
}

dependencies {
javadocClasspath libs.autovalue.annotations

api project(':appcheck:firebase-appcheck')
api libs.firebase.annotations
api libs.firebase.common.v2200
api libs.firebase.common.ktx
api libs.firebase.components.v1800
api libs.recaptcha.v1871

testImplementation(project(":integ-testing")) {
exclude group: 'com.google.firebase', module: 'firebase-common'
exclude group: 'com.google.firebase', module: 'firebase-components'
}
testImplementation libs.androidx.test.core
testImplementation libs.truth
testImplementation libs.junit
testImplementation libs.mockito.core.v5120
testImplementation libs.robolectric
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application>
<service
android:name="com.google.firebase.components.ComponentDiscoveryService"
android:exported="false">
<meta-data
android:name="com.google.firebase.components:com.google.firebase.appcheck.recaptchaenterprise.FirebaseAppCheckRecaptchaEnterpriseRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
</service>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.google.firebase.appcheck.recaptchaenterprise;

import android.app.Application;
import android.content.Context;
import com.google.android.gms.common.annotation.KeepForSdk;
import com.google.firebase.annotations.concurrent.Blocking;
import com.google.firebase.annotations.concurrent.Lightweight;
import com.google.firebase.appcheck.recaptchaenterprise.internal.FirebaseExecutors;
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 reCAPTCHA Enterprise's dependency
* injections in Firebase Android Components.
*
* @hide
*/
@KeepForSdk
public class FirebaseAppCheckRecaptchaEnterpriseRegistrar implements ComponentRegistrar {
private static final String LIBRARY_NAME = "fire-app-check-recaptcha-enterprise";

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

return Arrays.asList(
Component.builder(Application.class)
.name(LIBRARY_NAME)
.add(Dependency.required(Context.class))
.factory(
container -> {
Context context = container.get(Context.class);
return (Application) context.getApplicationContext();
})
.build(),
Component.builder(FirebaseExecutors.class)
.name(LIBRARY_NAME)
.add(Dependency.required(liteExecutor))
.add(Dependency.required(blockingExecutor))
.factory(
container ->
new FirebaseExecutors(
container.get(liteExecutor), container.get(blockingExecutor)))
.build(),
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.google.firebase.appcheck.recaptchaenterprise;

import android.app.Application;
import androidx.annotation.NonNull;
import com.google.firebase.FirebaseApp;
import com.google.firebase.appcheck.AppCheckProvider;
import com.google.firebase.appcheck.AppCheckProviderFactory;
import com.google.firebase.appcheck.FirebaseAppCheck;
import com.google.firebase.appcheck.recaptchaenterprise.internal.FirebaseExecutors;
import com.google.firebase.appcheck.recaptchaenterprise.internal.RecaptchaEnterpriseAppCheckProvider;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Implementation of an {@link AppCheckProviderFactory} that builds <br>
* {@link RecaptchaEnterpriseAppCheckProvider}s. This is the default implementation.
*/
public class RecaptchaEnterpriseAppCheckProviderFactory implements AppCheckProviderFactory {

private static FirebaseExecutors firebaseExecutors;
private static final Map<String, RecaptchaEnterpriseAppCheckProviderFactory> factoryInstances =
new ConcurrentHashMap<>();
private final String siteKey;
private volatile RecaptchaEnterpriseAppCheckProvider provider;

private RecaptchaEnterpriseAppCheckProviderFactory(@NonNull String siteKey) {
this.siteKey = siteKey;
}

/** Gets an instance of this class for installation into a {@link FirebaseAppCheck} instance. */
@NonNull
public static RecaptchaEnterpriseAppCheckProviderFactory getInstance(@NonNull String siteKey) {
RecaptchaEnterpriseAppCheckProviderFactory factory = factoryInstances.get(siteKey);
if (factory == null) {
synchronized (factoryInstances) {
factory = factoryInstances.get(siteKey);
if (factory == null) {
factory = new RecaptchaEnterpriseAppCheckProviderFactory(siteKey);
factoryInstances.put(siteKey, factory);
}
}
}
return factory;
}

@NonNull
@Override
@SuppressWarnings("FirebaseUseExplicitDependencies")
public AppCheckProvider create(@NonNull FirebaseApp firebaseApp) {
if (provider == null) {
synchronized (this) {
if (provider == null) {
if (RecaptchaEnterpriseAppCheckProviderFactory.firebaseExecutors == null) {
firebaseExecutors = firebaseApp.get(FirebaseExecutors.class);
}
Application application = firebaseApp.get(Application.class);

provider =
new RecaptchaEnterpriseAppCheckProvider(
firebaseApp,
application,
siteKey,
firebaseExecutors.getLiteExecutor(),
firebaseExecutors.getBlockingExecutor());
}
}
}
return provider;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.google.firebase.appcheck.recaptchaenterprise.internal;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import org.json.JSONException;
import org.json.JSONObject;

/**
* Client-side model of the ExchangeRecaptchaEnterpriseTokenRequest payload from the Firebase App
* Check Token Exchange API.
*/
public class ExchangeRecaptchaEnterpriseTokenRequest {

@VisibleForTesting
static final String RECAPTCHA_ENTERPRISE_TOKEN_KEY = "recaptchaEnterpriseToken";

private final String recaptchaEnterpriseToken;

public ExchangeRecaptchaEnterpriseTokenRequest(@NonNull String recaptchaEnterpriseToken) {
this.recaptchaEnterpriseToken = recaptchaEnterpriseToken;
}

@NonNull
public String toJsonString() throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put(RECAPTCHA_ENTERPRISE_TOKEN_KEY, recaptchaEnterpriseToken);

return jsonObject.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.google.firebase.appcheck.recaptchaenterprise.internal;

import com.google.firebase.annotations.concurrent.Blocking;
import com.google.firebase.annotations.concurrent.Lightweight;
import java.util.concurrent.Executor;

/**
* This class encapsulates a {@link com.google.firebase.annotations.concurrent.Lightweight} executor
* and a {@link com.google.firebase.annotations.concurrent.Blocking} executor, making them available
* for various asynchronous operations related to reCAPTCHA Enterprise App Check.
*/
public class FirebaseExecutors {
private final Executor liteExecutor;
private final Executor blockingExecutor;

public FirebaseExecutors(
@Lightweight Executor liteExecutor, @Blocking Executor blockingExecutor) {
this.liteExecutor = liteExecutor;
this.blockingExecutor = blockingExecutor;
}

public Executor getLiteExecutor() {
return liteExecutor;
}

public Executor getBlockingExecutor() {
return blockingExecutor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.google.firebase.appcheck.recaptchaenterprise.internal;

import android.app.Application;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.android.recaptcha.Recaptcha;
import com.google.android.recaptcha.RecaptchaAction;
import com.google.android.recaptcha.RecaptchaTasksClient;
import com.google.firebase.FirebaseApp;
import com.google.firebase.annotations.concurrent.Blocking;
import com.google.firebase.annotations.concurrent.Lightweight;
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.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.concurrent.Executor;

public class RecaptchaEnterpriseAppCheckProvider implements AppCheckProvider {

private final RecaptchaAction recaptchaAction = RecaptchaAction.custom("fire_app_check");
private final Task<RecaptchaTasksClient> recaptchaTasksClientTask;
private final Executor liteExecutor;
private final Executor blockingExecutor;
private final RetryManager retryManager;
private final NetworkClient networkClient;
private static final String TAG = "rCEAppCheckProvider";

public RecaptchaEnterpriseAppCheckProvider(
@NonNull FirebaseApp firebaseApp,
@NonNull Application application,
@NonNull String siteKey,
@Lightweight Executor liteExecutor,
@Blocking Executor blockingExecutor) {
this.liteExecutor = liteExecutor;
this.blockingExecutor = blockingExecutor;
this.retryManager = new RetryManager();
this.networkClient = new NetworkClient(firebaseApp);
recaptchaTasksClientTask = Recaptcha.fetchTaskClient(application, siteKey);
}

@VisibleForTesting
RecaptchaEnterpriseAppCheckProvider(
@Lightweight Executor liteExecutor,
@Blocking Executor blockingExecutor,
@NonNull RetryManager retryManager,
@NonNull NetworkClient networkClient,
@NonNull RecaptchaTasksClient recaptchaTasksClient) {
this.liteExecutor = liteExecutor;
this.blockingExecutor = blockingExecutor;
this.retryManager = retryManager;
this.networkClient = networkClient;
this.recaptchaTasksClientTask = Tasks.forResult(recaptchaTasksClient);
}

@NonNull
@Override
public Task<AppCheckToken> getToken() {
return getRecaptchaEnterpriseAttestation()
.onSuccessTask(
liteExecutor,
recaptchaEnterpriseToken -> {
ExchangeRecaptchaEnterpriseTokenRequest request =
new ExchangeRecaptchaEnterpriseTokenRequest(recaptchaEnterpriseToken);
return Tasks.call(
blockingExecutor,
() ->
networkClient.exchangeAttestationForAppCheckToken(
request.toJsonString().getBytes(StandardCharsets.UTF_8),
NetworkClient.RECAPTCHA_ENTERPRISE,
retryManager));
})
.onSuccessTask(
liteExecutor,
appCheckTokenResponse ->
Tasks.forResult(
DefaultAppCheckToken.constructFromAppCheckTokenResponse(
appCheckTokenResponse)));
}

@NonNull
private Task<String> getRecaptchaEnterpriseAttestation() {
return recaptchaTasksClientTask.continueWithTask(
blockingExecutor,
task -> {
if (task.isSuccessful()) {
RecaptchaTasksClient client = task.getResult();
return client.executeTask(recaptchaAction);
} else {
Log.w(TAG, "Recaptcha task failed", task.getException());
throw Objects.requireNonNull(task.getException());
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** @hide */
package com.google.firebase.appcheck.recaptchaenterprise.internal;
Loading
Loading