|
14 | 14 |
|
15 | 15 | package com.google.firebase.installations.remote;
|
16 | 16 |
|
| 17 | +import static android.content.ContentValues.TAG; |
| 18 | + |
| 19 | +import android.content.Context; |
| 20 | +import android.content.pm.PackageManager; |
17 | 21 | import android.util.JsonReader;
|
| 22 | +import android.util.Log; |
18 | 23 | import androidx.annotation.NonNull;
|
| 24 | +import com.google.android.gms.common.util.AndroidUtilsLight; |
| 25 | +import com.google.android.gms.common.util.Hex; |
19 | 26 | import com.google.firebase.installations.InstallationTokenResult;
|
20 | 27 | import java.io.IOException;
|
21 | 28 | import java.io.InputStreamReader;
|
@@ -49,6 +56,15 @@ public class FirebaseInstallationServiceClient {
|
49 | 56 | private static final String INTERNAL_SERVER_ERROR_MESSAGE = "There was an internal server error.";
|
50 | 57 | private static final String NETWORK_ERROR_MESSAGE = "The server returned an unexpected error:";
|
51 | 58 |
|
| 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 | + |
52 | 68 | /**
|
53 | 69 | * Creates a FID on the FIS Servers by calling FirebaseInstallations API create method.
|
54 | 70 | *
|
@@ -79,6 +95,10 @@ public InstallationResponse createFirebaseInstallation(
|
79 | 95 | httpsURLConnection.addRequestProperty(CONTENT_TYPE_HEADER_KEY, JSON_CONTENT_TYPE);
|
80 | 96 | httpsURLConnection.addRequestProperty(ACCEPT_HEADER_KEY, JSON_CONTENT_TYPE);
|
81 | 97 | 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 | + |
82 | 102 | GZIPOutputStream gzipOutputStream =
|
83 | 103 | new GZIPOutputStream(httpsURLConnection.getOutputStream());
|
84 | 104 | try {
|
@@ -282,4 +302,23 @@ private InstallationTokenResult readGenerateAuthTokenResponse(HttpsURLConnection
|
282 | 302 |
|
283 | 303 | return builder.build();
|
284 | 304 | }
|
| 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 | + } |
285 | 324 | }
|
0 commit comments