Skip to content

Commit e68e599

Browse files
authored
Fixing FISClient to correctly parse expiration timestamp. (#848)
* Fixing FISClient to correctly parse expiration timestamp.
1 parent e2d3631 commit e68e599

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

firebase-installations/firebase-installations.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ dependencies {
5252
testImplementation 'androidx.test:core:1.2.0'
5353
testImplementation 'junit:junit:4.12'
5454
testImplementation "org.robolectric:robolectric:$robolectricVersion"
55+
testImplementation "com.google.truth:truth:$googleTruthVersion"
56+
5557

5658
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
5759
androidTestImplementation 'androidx.test:runner:1.2.0'

firebase-installations/src/main/java/com/google/firebase/installations/remote/FirebaseInstallationServiceClient.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.firebase.installations.remote;
1616

1717
import static android.content.ContentValues.TAG;
18+
import static com.google.android.gms.common.internal.Preconditions.checkArgument;
1819

1920
import android.content.Context;
2021
import android.content.pm.PackageManager;
@@ -23,12 +24,13 @@
2324
import androidx.annotation.NonNull;
2425
import com.google.android.gms.common.util.AndroidUtilsLight;
2526
import com.google.android.gms.common.util.Hex;
27+
import com.google.android.gms.common.util.VisibleForTesting;
2628
import com.google.firebase.installations.InstallationTokenResult;
2729
import java.io.IOException;
2830
import java.io.InputStreamReader;
2931
import java.net.URL;
3032
import java.nio.charset.Charset;
31-
import java.util.concurrent.TimeUnit;
33+
import java.util.regex.Pattern;
3234
import java.util.zip.GZIPOutputStream;
3335
import javax.net.ssl.HttpsURLConnection;
3436
import org.json.JSONException;
@@ -61,6 +63,11 @@ public class FirebaseInstallationServiceClient {
6163

6264
private static final int NETWORK_TIMEOUT_MILLIS = 10000;
6365

66+
private static final Pattern EXPIRATION_TIMESTAMP_PATTERN = Pattern.compile("[0-9]+s");
67+
68+
@VisibleForTesting
69+
static final String PARSING_EXPIRATION_TIME_ERROR_MESSAGE = "Invalid Expiration Timestamp.";
70+
6471
private final Context context;
6572

6673
public FirebaseInstallationServiceClient(@NonNull Context context) {
@@ -273,7 +280,7 @@ private InstallationResponse readCreateResponse(HttpsURLConnection conn) throws
273280
installationTokenResult.setToken(reader.nextString());
274281
} else if (key.equals("expiresIn")) {
275282
installationTokenResult.setTokenExpirationInSecs(
276-
TimeUnit.MILLISECONDS.toSeconds(reader.nextLong()));
283+
parseTokenExpirationTimestamp(reader.nextString()));
277284
} else {
278285
reader.skipValue();
279286
}
@@ -301,7 +308,7 @@ private InstallationTokenResult readGenerateAuthTokenResponse(HttpsURLConnection
301308
if (name.equals("token")) {
302309
builder.setToken(reader.nextString());
303310
} else if (name.equals("expiresIn")) {
304-
builder.setTokenExpirationInSecs(TimeUnit.MILLISECONDS.toSeconds(reader.nextLong()));
311+
builder.setTokenExpirationInSecs(parseTokenExpirationTimestamp(reader.nextString()));
305312
} else {
306313
reader.skipValue();
307314
}
@@ -329,4 +336,19 @@ private String getFingerprintHashForPackage() {
329336
return null;
330337
}
331338
}
339+
340+
/**
341+
* Returns parsed token expiration timestamp in seconds.
342+
*
343+
* @param expiresIn is expiration timestamp in String format: 604800s
344+
*/
345+
@VisibleForTesting
346+
static long parseTokenExpirationTimestamp(String expiresIn) {
347+
checkArgument(
348+
EXPIRATION_TIMESTAMP_PATTERN.matcher(expiresIn).matches(),
349+
PARSING_EXPIRATION_TIME_ERROR_MESSAGE);
350+
return (expiresIn == null || expiresIn.length() == 0)
351+
? 0L
352+
: Long.parseLong(expiresIn.substring(0, expiresIn.length() - 1));
353+
}
332354
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.installations.remote;
16+
17+
import static com.google.common.truth.Truth.assertThat;
18+
import static com.google.common.truth.Truth.assertWithMessage;
19+
import static org.junit.Assert.fail;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.robolectric.RobolectricTestRunner;
24+
25+
/** Tests for {@link FirebaseInstallationServiceClient}. */
26+
@RunWith(RobolectricTestRunner.class)
27+
public class FirebaseInstallationServiceClientTest {
28+
29+
private final String TEST_EXPIRATION_TIMESTAMP = "604800s";
30+
private final long TEST_EXPIRATION_IN_SECS = 604800;
31+
private final String INCORRECT_EXPIRATION_TIMESTAMP = "2345";
32+
33+
@Test
34+
public void parseTokenExpirationTimestamp_successful() {
35+
long actual =
36+
FirebaseInstallationServiceClient.parseTokenExpirationTimestamp(TEST_EXPIRATION_TIMESTAMP);
37+
38+
assertWithMessage("Exception status doesn't match")
39+
.that(actual)
40+
.isEqualTo(TEST_EXPIRATION_IN_SECS);
41+
}
42+
43+
@Test
44+
public void parseTokenExpirationTimestamp_failed() {
45+
try {
46+
FirebaseInstallationServiceClient.parseTokenExpirationTimestamp(
47+
INCORRECT_EXPIRATION_TIMESTAMP);
48+
fail("Parsing token expiration timestamp failed.");
49+
} catch (IllegalArgumentException expected) {
50+
assertThat(expected)
51+
.hasMessageThat()
52+
.isEqualTo(FirebaseInstallationServiceClient.PARSING_EXPIRATION_TIME_ERROR_MESSAGE);
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)