Skip to content

Commit aa73d95

Browse files
authored
Adding headers for Chemist to check API key restriction. (#821)
1 parent 5b0acbf commit aa73d95

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

firebase-installations/api.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ package com.google.firebase.installations.local {
6767
package com.google.firebase.installations.remote {
6868

6969
public class FirebaseInstallationServiceClient {
70-
ctor public FirebaseInstallationServiceClient();
70+
ctor public FirebaseInstallationServiceClient(@NonNull Context);
7171
method @NonNull public com.google.firebase.installations.remote.InstallationResponse createFirebaseInstallation(@NonNull String, @NonNull String, @NonNull String, @NonNull String) throws com.google.firebase.installations.remote.FirebaseInstallationServiceException;
7272
method @NonNull public void deleteFirebaseInstallation(@NonNull String, @NonNull String, @NonNull String, @NonNull String) throws com.google.firebase.installations.remote.FirebaseInstallationServiceException;
7373
method @NonNull public InstallationTokenResult generateAuthToken(@NonNull String, @NonNull String, @NonNull String, @NonNull String) throws com.google.firebase.installations.remote.FirebaseInstallationServiceException;

firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
6363
DefaultClock.getInstance(),
6464
new ThreadPoolExecutor(0, 1, 30L, TimeUnit.SECONDS, new LinkedBlockingQueue<>()),
6565
firebaseApp,
66-
new FirebaseInstallationServiceClient(),
66+
new FirebaseInstallationServiceClient(firebaseApp.getApplicationContext()),
6767
new PersistedFid(firebaseApp),
6868
new Utils());
6969
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414

1515
package com.google.firebase.installations.remote;
1616

17+
import static android.content.ContentValues.TAG;
18+
19+
import android.content.Context;
20+
import android.content.pm.PackageManager;
1721
import android.util.JsonReader;
22+
import android.util.Log;
1823
import androidx.annotation.NonNull;
24+
import com.google.android.gms.common.util.AndroidUtilsLight;
25+
import com.google.android.gms.common.util.Hex;
1926
import com.google.firebase.installations.InstallationTokenResult;
2027
import java.io.IOException;
2128
import java.io.InputStreamReader;
@@ -49,6 +56,15 @@ public class FirebaseInstallationServiceClient {
4956
private static final String INTERNAL_SERVER_ERROR_MESSAGE = "There was an internal server error.";
5057
private static final String NETWORK_ERROR_MESSAGE = "The server returned an unexpected error:";
5158

59+
private static final String X_ANDROID_PACKAGE_HEADER_KEY = "X-Android-Package";
60+
private static final String X_ANDROID_CERT_HEADER_KEY = "X-Android-Cert";
61+
62+
private final Context context;
63+
64+
public FirebaseInstallationServiceClient(@NonNull Context context) {
65+
this.context = context;
66+
}
67+
5268
/**
5369
* Creates a FID on the FIS Servers by calling FirebaseInstallations API create method.
5470
*
@@ -79,6 +95,10 @@ public InstallationResponse createFirebaseInstallation(
7995
httpsURLConnection.addRequestProperty(CONTENT_TYPE_HEADER_KEY, JSON_CONTENT_TYPE);
8096
httpsURLConnection.addRequestProperty(ACCEPT_HEADER_KEY, JSON_CONTENT_TYPE);
8197
httpsURLConnection.addRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING);
98+
httpsURLConnection.addRequestProperty(X_ANDROID_PACKAGE_HEADER_KEY, context.getPackageName());
99+
httpsURLConnection.addRequestProperty(
100+
X_ANDROID_CERT_HEADER_KEY, getFingerprintHashForPackage());
101+
82102
GZIPOutputStream gzipOutputStream =
83103
new GZIPOutputStream(httpsURLConnection.getOutputStream());
84104
try {
@@ -282,4 +302,23 @@ private InstallationTokenResult readGenerateAuthTokenResponse(HttpsURLConnection
282302

283303
return builder.build();
284304
}
305+
306+
/** Gets the Android package's SHA-1 fingerprint. */
307+
private String getFingerprintHashForPackage() {
308+
byte[] hash;
309+
310+
try {
311+
hash = AndroidUtilsLight.getPackageCertificateHashBytes(context, context.getPackageName());
312+
313+
if (hash == null) {
314+
Log.e(TAG, "Could not get fingerprint hash for package: " + context.getPackageName());
315+
return null;
316+
} else {
317+
return Hex.bytesToStringUppercase(hash, /* zeroTerminated= */ false);
318+
}
319+
} catch (PackageManager.NameNotFoundException e) {
320+
Log.e(TAG, "No such package: " + context.getPackageName(), e);
321+
return null;
322+
}
323+
}
285324
}

0 commit comments

Comments
 (0)