Skip to content

Commit 2ddbaf3

Browse files
committed
Add method to call the GeneratePlayIntegrityChallenge endpoint to NetworkClient.
1 parent 295c8f5 commit 2ddbaf3

File tree

1 file changed

+26
-1
lines changed
  • appcheck/firebase-appcheck/src/main/java/com/google/firebase/appcheck/internal

1 file changed

+26
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public class NetworkClient {
5757
"https://firebaseappcheck.googleapis.com/v1beta/projects/%s/apps/%s:exchangeDebugToken?key=%s";
5858
private static final String PLAY_INTEGRITY_EXCHANGE_URL_TEMPLATE =
5959
"https://firebaseappcheck.googleapis.com/v1/projects/%s/apps/%s:exchangePlayIntegrityToken?key=%s";
60+
private static final String PLAY_INTEGRITY_CHALLENGE_URL_TEMPLATE =
61+
"https://firebaseappcheck.googleapis.com/v1/projects/%s/apps/%s:generatePlayIntegrityChallenge?key=%s";
6062
private static final String CONTENT_TYPE = "Content-Type";
6163
private static final String APPLICATION_JSON = "application/json";
6264
private static final String UTF_8 = "UTF-8";
@@ -119,6 +121,29 @@ public AppCheckTokenResponse exchangeAttestationForAppCheckToken(
119121
throw new FirebaseException("Too many attempts.");
120122
}
121123
URL url = new URL(String.format(getUrlTemplate(tokenType), projectId, appId, apiKey));
124+
String response = makeNetworkRequest(url, requestBytes, retryManager);
125+
return AppCheckTokenResponse.fromJsonString(response);
126+
}
127+
128+
/**
129+
* Calls the App Check backend using {@link HttpURLConnection} in order to generate a challenge
130+
* nonce for the Play Integrity attestation flow.
131+
*/
132+
@NonNull
133+
public String generatePlayIntegrityChallenge(
134+
@NonNull byte[] requestBytes, @NonNull RetryManager retryManager)
135+
throws FirebaseException, IOException, JSONException {
136+
if (!retryManager.canRetry()) {
137+
throw new FirebaseException("Too many attempts.");
138+
}
139+
URL url =
140+
new URL(String.format(PLAY_INTEGRITY_CHALLENGE_URL_TEMPLATE, projectId, appId, apiKey));
141+
return makeNetworkRequest(url, requestBytes, retryManager);
142+
}
143+
144+
private String makeNetworkRequest(
145+
URL url, @NonNull byte[] requestBytes, @NonNull RetryManager retryManager)
146+
throws FirebaseException, IOException, JSONException {
122147
HttpURLConnection urlConnection = createHttpUrlConnection(url);
123148

124149
try {
@@ -163,7 +188,7 @@ public AppCheckTokenResponse exchangeAttestationForAppCheckToken(
163188
+ httpErrorResponse.getErrorMessage());
164189
}
165190
retryManager.resetBackoffOnSuccess();
166-
return AppCheckTokenResponse.fromJsonString(responseBody);
191+
return responseBody;
167192
} finally {
168193
urlConnection.disconnect();
169194
}

0 commit comments

Comments
 (0)