Skip to content

Fixing FISClient to correctly parse expiration timestamp. #848

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 5 commits into from
Oct 9, 2019
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.firebase.installations.remote;

import static android.content.ContentValues.TAG;
import static com.google.android.gms.common.internal.Preconditions.checkArgument;

import android.content.Context;
import android.content.pm.PackageManager;
Expand All @@ -28,7 +29,7 @@
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream;
import javax.net.ssl.HttpsURLConnection;
import org.json.JSONException;
Expand Down Expand Up @@ -61,6 +62,8 @@ public class FirebaseInstallationServiceClient {

private static final int NETWORK_TIMEOUT_MILLIS = 10000;

private static final Pattern EXPIRATION_TIMESTAMP_PATTERN = Pattern.compile("[0-9]+s");

private final Context context;

public FirebaseInstallationServiceClient(@NonNull Context context) {
Expand Down Expand Up @@ -273,7 +276,7 @@ private InstallationResponse readCreateResponse(HttpsURLConnection conn) throws
installationTokenResult.setToken(reader.nextString());
} else if (key.equals("expiresIn")) {
installationTokenResult.setTokenExpirationInSecs(
TimeUnit.MILLISECONDS.toSeconds(reader.nextLong()));
parseTokenExpirationTimestamp(reader.nextString()));
} else {
reader.skipValue();
}
Expand Down Expand Up @@ -301,7 +304,7 @@ private InstallationTokenResult readGenerateAuthTokenResponse(HttpsURLConnection
if (name.equals("token")) {
builder.setToken(reader.nextString());
} else if (name.equals("expiresIn")) {
builder.setTokenExpirationInSecs(TimeUnit.MILLISECONDS.toSeconds(reader.nextLong()));
builder.setTokenExpirationInSecs(parseTokenExpirationTimestamp(reader.nextString()));
} else {
reader.skipValue();
}
Expand Down Expand Up @@ -329,4 +332,17 @@ private String getFingerprintHashForPackage() {
return null;
}
}

/**
* Returns parsed token expiration timestamp in seconds.
*
* @param expiresIn is expiration timestamp in String format: 604800s
*/
private static long parseTokenExpirationTimestamp(String expiresIn) {
checkArgument(
EXPIRATION_TIMESTAMP_PATTERN.matcher(expiresIn).find(), "Invalid Expiration Timestamp.");
return (expiresIn == null || expiresIn.length() == 0)
? 0L
: Long.parseLong(expiresIn.substring(0, expiresIn.length() - 1));
}
}