Skip to content

Commit dee3c59

Browse files
WIP: feat(appcheck-network): Extend NetworkClient for reCAPTCHA Enterprise
This commit modifies the `NetworkClient` to support fetching App Check tokens using the reCAPTCHA Enterprise attestation type. - Adds the `RECAPTCHA_ENTERPRISE_URL_TEMPLATE` constant for the new API endpoint. - Includes `RECAPTCHA_ENTERPRISE` as a new `AttestationTokenType`. - Updates the `getUrlTemplate` method to return the correct URL for reCAPTCHA Enterprise token exchange requests.
1 parent bef3b03 commit dee3c59

File tree

1 file changed

+12
-3
lines changed
  • appcheck/firebase-appcheck/src/main/java/com/google/firebase/appcheck/internal

1 file changed

+12
-3
lines changed

appcheck/firebase-appcheck/src/main/java/com/google/firebase/appcheck/internal/NetworkClient.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import android.content.Context;
2020
import android.content.pm.PackageManager.NameNotFoundException;
2121
import android.util.Log;
22+
2223
import androidx.annotation.IntDef;
2324
import androidx.annotation.NonNull;
2425
import androidx.annotation.VisibleForTesting;
26+
2527
import com.google.android.gms.common.util.AndroidUtilsLight;
2628
import com.google.android.gms.common.util.Hex;
2729
import com.google.android.gms.tasks.Tasks;
@@ -31,6 +33,7 @@
3133
import com.google.firebase.appcheck.FirebaseAppCheck;
3234
import com.google.firebase.heartbeatinfo.HeartBeatController;
3335
import com.google.firebase.inject.Provider;
36+
3437
import java.io.BufferedOutputStream;
3538
import java.io.BufferedReader;
3639
import java.io.IOException;
@@ -41,6 +44,8 @@
4144
import java.lang.annotation.RetentionPolicy;
4245
import java.net.HttpURLConnection;
4346
import java.net.URL;
47+
import java.nio.charset.StandardCharsets;
48+
4449
import org.json.JSONException;
4550

4651
/**
@@ -57,9 +62,10 @@ public class NetworkClient {
5762
"https://firebaseappcheck.googleapis.com/v1/projects/%s/apps/%s:exchangePlayIntegrityToken?key=%s";
5863
private static final String PLAY_INTEGRITY_CHALLENGE_URL_TEMPLATE =
5964
"https://firebaseappcheck.googleapis.com/v1/projects/%s/apps/%s:generatePlayIntegrityChallenge?key=%s";
65+
private static final String RECAPTCHA_ENTERPRISE_URL_TEMPLATE =
66+
"https://firebaseappcheck.googleapis.com/v1/projects/%s/apps/%s:exchangeRecaptchaEnterpriseToken?key=%s";
6067
private static final String CONTENT_TYPE = "Content-Type";
6168
private static final String APPLICATION_JSON = "application/json";
62-
private static final String UTF_8 = "UTF-8";
6369
@VisibleForTesting static final String X_FIREBASE_CLIENT = "X-Firebase-Client";
6470
@VisibleForTesting static final String X_ANDROID_PACKAGE = "X-Android-Package";
6571
@VisibleForTesting static final String X_ANDROID_CERT = "X-Android-Cert";
@@ -71,12 +77,13 @@ public class NetworkClient {
7177
private final Provider<HeartBeatController> heartBeatControllerProvider;
7278

7379
@Retention(RetentionPolicy.SOURCE)
74-
@IntDef({UNKNOWN, DEBUG, PLAY_INTEGRITY})
80+
@IntDef({UNKNOWN, DEBUG, PLAY_INTEGRITY, RECAPTCHA_ENTERPRISE})
7581
public @interface AttestationTokenType {}
7682

7783
public static final int UNKNOWN = 0;
7884
public static final int DEBUG = 2;
7985
public static final int PLAY_INTEGRITY = 3;
86+
public static final int RECAPTCHA_ENTERPRISE = 4;
8087

8188
public NetworkClient(@NonNull FirebaseApp firebaseApp) {
8289
this(
@@ -172,7 +179,7 @@ private String makeNetworkRequest(
172179
? urlConnection.getInputStream()
173180
: urlConnection.getErrorStream();
174181
StringBuilder response = new StringBuilder();
175-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, UTF_8))) {
182+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
176183
String line;
177184
while ((line = reader.readLine()) != null) {
178185
response.append(line);
@@ -236,6 +243,8 @@ private static String getUrlTemplate(@AttestationTokenType int tokenType) {
236243
return DEBUG_EXCHANGE_URL_TEMPLATE;
237244
case PLAY_INTEGRITY:
238245
return PLAY_INTEGRITY_EXCHANGE_URL_TEMPLATE;
246+
case RECAPTCHA_ENTERPRISE:
247+
return RECAPTCHA_ENTERPRISE_URL_TEMPLATE;
239248
default:
240249
throw new IllegalArgumentException("Unknown token type.");
241250
}

0 commit comments

Comments
 (0)