Skip to content

Update App Check SDKs to use v1 API. #3627

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 2 commits into from
Apr 18, 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
2 changes: 1 addition & 1 deletion appcheck/firebase-appcheck-debug-testing/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=16.0.0-beta06
version=16.0.0
2 changes: 1 addition & 1 deletion appcheck/firebase-appcheck-debug/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=16.0.0-beta06
version=16.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
public class DebugAppCheckProviderTest {

private static final String DEBUG_SECRET = "debugSecret";
private static final String ATTESTATION_TOKEN = "token";
private static final String APP_CHECK_TOKEN = "appCheckToken";
private static final String TIME_TO_LIVE = "3600s";
private static final String API_KEY = "apiKey";
private static final String APP_ID = "appId";
Expand Down Expand Up @@ -128,7 +128,7 @@ public void exchangeDebugToken_onSuccess_setsTaskResult() throws Exception {
when(mockNetworkClient.exchangeAttestationForAppCheckToken(
any(), eq(NetworkClient.DEBUG), eq(mockRetryManager)))
.thenReturn(mockAppCheckTokenResponse);
when(mockAppCheckTokenResponse.getAttestationToken()).thenReturn(ATTESTATION_TOKEN);
when(mockAppCheckTokenResponse.getToken()).thenReturn(APP_CHECK_TOKEN);
when(mockAppCheckTokenResponse.getTimeToLive()).thenReturn(TIME_TO_LIVE);

DebugAppCheckProvider provider =
Expand All @@ -141,7 +141,7 @@ public void exchangeDebugToken_onSuccess_setsTaskResult() throws Exception {

AppCheckToken token = task.getResult();
assertThat(token).isInstanceOf(DefaultAppCheckToken.class);
assertThat(token.getToken()).isEqualTo(ATTESTATION_TOKEN);
assertThat(token.getToken()).isEqualTo(APP_CHECK_TOKEN);
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions appcheck/firebase-appcheck-interop/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version=16.0.0-beta06
latestReleasedVersion=16.0.0-beta05
version=16.0.0
latestReleasedVersion=16.0.0-beta06
2 changes: 1 addition & 1 deletion appcheck/firebase-appcheck-safetynet/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=16.0.0-beta06
version=16.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class SafetyNetAppCheckProviderTest {
private static final String APP_ID = "appId";
private static final String PROJECT_ID = "projectId";
private static final String SAFETY_NET_TOKEN = "safetyNetToken";
private static final String ATTESTATION_TOKEN = "token";
private static final String APP_CHECK_TOKEN = "appCheckToken";
private static final String TIME_TO_LIVE = "3600s";

private FirebaseApp firebaseApp;
Expand Down Expand Up @@ -188,7 +188,7 @@ public void exchangeSafetyNetJwsForToken_onSuccess_setsTaskResult() throws Excep
when(mockNetworkClient.exchangeAttestationForAppCheckToken(
any(), eq(NetworkClient.SAFETY_NET), eq(mockRetryManager)))
.thenReturn(mockAppCheckTokenResponse);
when(mockAppCheckTokenResponse.getAttestationToken()).thenReturn(ATTESTATION_TOKEN);
when(mockAppCheckTokenResponse.getToken()).thenReturn(APP_CHECK_TOKEN);
when(mockAppCheckTokenResponse.getTimeToLive()).thenReturn(TIME_TO_LIVE);

SafetyNetAppCheckProvider provider =
Expand All @@ -207,7 +207,7 @@ public void exchangeSafetyNetJwsForToken_onSuccess_setsTaskResult() throws Excep

AppCheckToken token = task.getResult();
assertThat(token).isInstanceOf(DefaultAppCheckToken.class);
assertThat(token.getToken()).isEqualTo(ATTESTATION_TOKEN);
assertThat(token.getToken()).isEqualTo(APP_CHECK_TOKEN);
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion appcheck/firebase-appcheck/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=16.0.0-beta06
version=16.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,35 @@
import org.json.JSONObject;

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

@VisibleForTesting static final String ATTESTATION_TOKEN_KEY = "attestationToken";
@VisibleForTesting static final String TOKEN_KEY = "token";
@VisibleForTesting static final String TIME_TO_LIVE_KEY = "ttl";

private String attestationToken;
private String token;
private String timeToLive;

@NonNull
public static AppCheckTokenResponse fromJsonString(@NonNull String jsonString)
throws JSONException {
JSONObject jsonObject = new JSONObject(jsonString);
String attestationToken = emptyToNull(jsonObject.optString(ATTESTATION_TOKEN_KEY));
String token = emptyToNull(jsonObject.optString(TOKEN_KEY));
String timeToLive = emptyToNull(jsonObject.optString(TIME_TO_LIVE_KEY));
return new AppCheckTokenResponse(attestationToken, timeToLive);
return new AppCheckTokenResponse(token, timeToLive);
}

private AppCheckTokenResponse(@NonNull String attestationToken, @NonNull String timeToLive) {
checkNotNull(attestationToken);
private AppCheckTokenResponse(@NonNull String token, @NonNull String timeToLive) {
checkNotNull(token);
checkNotNull(timeToLive);
this.attestationToken = attestationToken;
this.token = token;
this.timeToLive = timeToLive;
}

@NonNull
public String getAttestationToken() {
return attestationToken;
public String getToken() {
return token;
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,13 @@ public static DefaultAppCheckToken constructFromAppCheckTokenResponse(
} catch (NumberFormatException e) {
// If parsing the duration string returned by the server fails for any reason, fall back to
// computing the timeToLive from the token claims directly.
Map<String, Object> claimsMap =
TokenParser.parseTokenClaims(tokenResponse.getAttestationToken());
Map<String, Object> claimsMap = TokenParser.parseTokenClaims(tokenResponse.getToken());
long iat = getLongFromClaimsSafely(claimsMap, ISSUED_AT_KEY);
long exp = getLongFromClaimsSafely(claimsMap, EXPIRATION_TIME_KEY);
expiresInMillis = (exp - iat) * ONE_SECOND_MILLIS;
}

return new DefaultAppCheckToken(tokenResponse.getAttestationToken(), expiresInMillis);
return new DefaultAppCheckToken(tokenResponse.getToken(), expiresInMillis);
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public class NetworkClient {
private static final String TAG = NetworkClient.class.getName();

private static final String SAFETY_NET_EXCHANGE_URL_TEMPLATE =
"https://firebaseappcheck.googleapis.com/v1beta/projects/%s/apps/%s:exchangeSafetyNetToken?key=%s";
"https://firebaseappcheck.googleapis.com/v1/projects/%s/apps/%s:exchangeSafetyNetToken?key=%s";
private static final String DEBUG_EXCHANGE_URL_TEMPLATE =
"https://firebaseappcheck.googleapis.com/v1beta/projects/%s/apps/%s:exchangeDebugToken?key=%s";
"https://firebaseappcheck.googleapis.com/v1/projects/%s/apps/%s:exchangeDebugToken?key=%s";
private static final String CONTENT_TYPE = "Content-Type";
private static final String APPLICATION_JSON = "application/json";
private static final String UTF_8 = "UTF-8";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@
@Config(manifest = Config.NONE)
public class AppCheckTokenResponseTest {

private static final String ATTESTATION_TOKEN = "attestationToken";
private static final String TIME_TO_LIVE = "ttl";
private static final String APP_CHECK_TOKEN = "appCheckToken";
private static final String TIME_TO_LIVE = "3600s";

@Test
public void fromJsonString_expectDeserialized() throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject.put(AppCheckTokenResponse.ATTESTATION_TOKEN_KEY, ATTESTATION_TOKEN);
jsonObject.put(AppCheckTokenResponse.TOKEN_KEY, APP_CHECK_TOKEN);
jsonObject.put(AppCheckTokenResponse.TIME_TO_LIVE_KEY, TIME_TO_LIVE);

AppCheckTokenResponse appCheckTokenResponse =
AppCheckTokenResponse.fromJsonString(jsonObject.toString());
assertThat(appCheckTokenResponse.getAttestationToken()).isEqualTo(ATTESTATION_TOKEN);
assertThat(appCheckTokenResponse.getToken()).isEqualTo(APP_CHECK_TOKEN);
assertThat(appCheckTokenResponse.getTimeToLive()).isEqualTo(TIME_TO_LIVE);
}

@Test
public void fromJsonString_nullAttestationToken_throwsException() throws Exception {
public void fromJsonString_nullToken_throwsException() throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject.put(AppCheckTokenResponse.TIME_TO_LIVE_KEY, TIME_TO_LIVE);

Expand All @@ -56,7 +56,7 @@ public void fromJsonString_nullAttestationToken_throwsException() throws Excepti
@Test
public void fromJsonString_nullTimeToLive_throwsException() throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject.put(AppCheckTokenResponse.ATTESTATION_TOKEN_KEY, ATTESTATION_TOKEN);
jsonObject.put(AppCheckTokenResponse.TOKEN_KEY, APP_CHECK_TOKEN);

assertThrows(
NullPointerException.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void testConstructFromRawToken_normalToken_expectSuccess() throws Excepti

@Test
public void testConstructFromAppCheckTokenResponse_success() {
when(mockAppCheckTokenResponse.getAttestationToken()).thenReturn(TOKEN_PAYLOAD);
when(mockAppCheckTokenResponse.getToken()).thenReturn(TOKEN_PAYLOAD);
when(mockAppCheckTokenResponse.getTimeToLive()).thenReturn(TIME_TO_LIVE_ONE_HOUR);

DefaultAppCheckToken defaultAppCheckToken =
Expand All @@ -124,7 +124,7 @@ public void testConstructFromAppCheckTokenResponse_success() {

@Test
public void testConstructFromAppCheckTokenResponse_withNanoSecondsDuration_success() {
when(mockAppCheckTokenResponse.getAttestationToken()).thenReturn(TOKEN_PAYLOAD);
when(mockAppCheckTokenResponse.getToken()).thenReturn(TOKEN_PAYLOAD);
when(mockAppCheckTokenResponse.getTimeToLive()).thenReturn(TIME_TO_LIVE_ONE_HOUR_PLUS_NANOS);

DefaultAppCheckToken defaultAppCheckToken =
Expand All @@ -138,7 +138,7 @@ public void testConstructFromAppCheckTokenResponse_withNanoSecondsDuration_succe
public void testConstructFromAppCheckTokenResponse_invalidTimeToLiveFormat_fallbackToTokenClaims()
throws Exception {
String rawToken = constructFakeRawToken();
when(mockAppCheckTokenResponse.getAttestationToken()).thenReturn(rawToken);
when(mockAppCheckTokenResponse.getToken()).thenReturn(rawToken);
when(mockAppCheckTokenResponse.getTimeToLive()).thenReturn(INVALID_TIME_TO_LIVE);

DefaultAppCheckToken defaultAppCheckToken =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package com.google.firebase.appcheck.internal;

import static com.google.common.truth.Truth.assertThat;
import static com.google.firebase.appcheck.internal.AppCheckTokenResponse.ATTESTATION_TOKEN_KEY;
import static com.google.firebase.appcheck.internal.AppCheckTokenResponse.TIME_TO_LIVE_KEY;
import static com.google.firebase.appcheck.internal.AppCheckTokenResponse.TOKEN_KEY;
import static com.google.firebase.appcheck.internal.HttpErrorResponse.CODE_KEY;
import static com.google.firebase.appcheck.internal.HttpErrorResponse.ERROR_KEY;
import static com.google.firebase.appcheck.internal.HttpErrorResponse.MESSAGE_KEY;
Expand Down Expand Up @@ -66,13 +66,13 @@ public class NetworkClientTest {
.setProjectId(PROJECT_ID)
.build();
private static final String SAFETY_NET_EXPECTED_URL =
"https://firebaseappcheck.googleapis.com/v1beta/projects/projectId/apps/appId:exchangeSafetyNetToken?key=apiKey";
"https://firebaseappcheck.googleapis.com/v1/projects/projectId/apps/appId:exchangeSafetyNetToken?key=apiKey";
private static final String DEBUG_EXPECTED_URL =
"https://firebaseappcheck.googleapis.com/v1beta/projects/projectId/apps/appId:exchangeDebugToken?key=apiKey";
"https://firebaseappcheck.googleapis.com/v1/projects/projectId/apps/appId:exchangeDebugToken?key=apiKey";
private static final String JSON_REQUEST = "jsonRequest";
private static final int SUCCESS_CODE = 200;
private static final int ERROR_CODE = 404;
private static final String ATTESTATION_TOKEN = "token";
private static final String APP_CHECK_TOKEN = "token";
private static final String TIME_TO_LIVE = "3600s";
private static final String ERROR_MESSAGE = "error message";
private static final String HEART_BEAT_HEADER_TEST = "test-header";
Expand Down Expand Up @@ -122,7 +122,7 @@ public void exchangeSafetyNetToken_successResponse_returnsAppCheckTokenResponse(
AppCheckTokenResponse tokenResponse =
networkClient.exchangeAttestationForAppCheckToken(
JSON_REQUEST.getBytes(), NetworkClient.SAFETY_NET, mockRetryManager);
assertThat(tokenResponse.getAttestationToken()).isEqualTo(ATTESTATION_TOKEN);
assertThat(tokenResponse.getToken()).isEqualTo(APP_CHECK_TOKEN);
assertThat(tokenResponse.getTimeToLive()).isEqualTo(TIME_TO_LIVE);

URL expectedUrl = new URL(SAFETY_NET_EXPECTED_URL);
Expand Down Expand Up @@ -172,7 +172,7 @@ public void exchangeDebugToken_successResponse_returnsAppCheckTokenResponse() th
AppCheckTokenResponse tokenResponse =
networkClient.exchangeAttestationForAppCheckToken(
JSON_REQUEST.getBytes(), NetworkClient.DEBUG, mockRetryManager);
assertThat(tokenResponse.getAttestationToken()).isEqualTo(ATTESTATION_TOKEN);
assertThat(tokenResponse.getToken()).isEqualTo(APP_CHECK_TOKEN);
assertThat(tokenResponse.getTimeToLive()).isEqualTo(TIME_TO_LIVE);

URL expectedUrl = new URL(DEBUG_EXPECTED_URL);
Expand Down Expand Up @@ -264,7 +264,7 @@ private void verifyRequestHeaders() {

private static JSONObject createAttestationResponse() throws Exception {
JSONObject responseBodyJson = new JSONObject();
responseBodyJson.put(ATTESTATION_TOKEN_KEY, ATTESTATION_TOKEN);
responseBodyJson.put(TOKEN_KEY, APP_CHECK_TOKEN);
responseBodyJson.put(TIME_TO_LIVE_KEY, TIME_TO_LIVE);

return responseBodyJson;
Expand Down