|
15 | 15 | package com.google.firebase.installations.remote;
|
16 | 16 |
|
17 | 17 | import static android.content.ContentValues.TAG;
|
| 18 | +import static com.google.android.gms.common.internal.Preconditions.checkArgument; |
18 | 19 |
|
19 | 20 | import android.content.Context;
|
20 | 21 | import android.content.pm.PackageManager;
|
|
23 | 24 | import androidx.annotation.NonNull;
|
24 | 25 | import com.google.android.gms.common.util.AndroidUtilsLight;
|
25 | 26 | import com.google.android.gms.common.util.Hex;
|
| 27 | +import com.google.android.gms.common.util.VisibleForTesting; |
26 | 28 | import com.google.firebase.installations.InstallationTokenResult;
|
27 | 29 | import java.io.IOException;
|
28 | 30 | import java.io.InputStreamReader;
|
29 | 31 | import java.net.URL;
|
30 | 32 | import java.nio.charset.Charset;
|
31 |
| -import java.util.concurrent.TimeUnit; |
| 33 | +import java.util.regex.Pattern; |
32 | 34 | import java.util.zip.GZIPOutputStream;
|
33 | 35 | import javax.net.ssl.HttpsURLConnection;
|
34 | 36 | import org.json.JSONException;
|
@@ -61,6 +63,11 @@ public class FirebaseInstallationServiceClient {
|
61 | 63 |
|
62 | 64 | private static final int NETWORK_TIMEOUT_MILLIS = 10000;
|
63 | 65 |
|
| 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 | + |
64 | 71 | private final Context context;
|
65 | 72 |
|
66 | 73 | public FirebaseInstallationServiceClient(@NonNull Context context) {
|
@@ -273,7 +280,7 @@ private InstallationResponse readCreateResponse(HttpsURLConnection conn) throws
|
273 | 280 | installationTokenResult.setToken(reader.nextString());
|
274 | 281 | } else if (key.equals("expiresIn")) {
|
275 | 282 | installationTokenResult.setTokenExpirationInSecs(
|
276 |
| - TimeUnit.MILLISECONDS.toSeconds(reader.nextLong())); |
| 283 | + parseTokenExpirationTimestamp(reader.nextString())); |
277 | 284 | } else {
|
278 | 285 | reader.skipValue();
|
279 | 286 | }
|
@@ -301,7 +308,7 @@ private InstallationTokenResult readGenerateAuthTokenResponse(HttpsURLConnection
|
301 | 308 | if (name.equals("token")) {
|
302 | 309 | builder.setToken(reader.nextString());
|
303 | 310 | } else if (name.equals("expiresIn")) {
|
304 |
| - builder.setTokenExpirationInSecs(TimeUnit.MILLISECONDS.toSeconds(reader.nextLong())); |
| 311 | + builder.setTokenExpirationInSecs(parseTokenExpirationTimestamp(reader.nextString())); |
305 | 312 | } else {
|
306 | 313 | reader.skipValue();
|
307 | 314 | }
|
@@ -329,4 +336,19 @@ private String getFingerprintHashForPackage() {
|
329 | 336 | return null;
|
330 | 337 | }
|
331 | 338 | }
|
| 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 | + } |
332 | 354 | }
|
0 commit comments