Skip to content

Commit ca1ab42

Browse files
committed
Merge branch 'master' into crashlytics/anrs
2 parents 1934f45 + 3f0779e commit ca1ab42

File tree

16 files changed

+175
-82
lines changed

16 files changed

+175
-82
lines changed

appcheck/firebase-appcheck-debug/firebase-appcheck-debug.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ dependencies {
4747
implementation 'com.google.android.gms:play-services-base:17.1.0'
4848
implementation 'com.google.android.gms:play-services-tasks:17.0.0'
4949

50+
javadocClasspath 'com.google.auto.value:auto-value-annotations:1.6.6'
51+
5052
testImplementation 'junit:junit:4.13-beta-2'
5153
testImplementation 'org.mockito:mockito-core:2.25.0'
5254
testImplementation "org.robolectric:robolectric:$robolectricVersion"

appcheck/firebase-appcheck-debug/src/main/java/com/google/firebase/appcheck/debug/internal/DebugAppCheckProvider.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,7 @@ public class DebugAppCheckProvider implements AppCheckProvider {
4545

4646
public DebugAppCheckProvider(@NonNull FirebaseApp firebaseApp, @Nullable String debugSecret) {
4747
checkNotNull(firebaseApp);
48-
String projectId = firebaseApp.getOptions().getProjectId();
49-
if (projectId == null) {
50-
throw new IllegalArgumentException("FirebaseOptions#getProjectId cannot be null.");
51-
}
52-
53-
this.networkClient =
54-
new NetworkClient(
55-
firebaseApp.getOptions().getApiKey(),
56-
firebaseApp.getOptions().getApplicationId(),
57-
projectId);
48+
this.networkClient = new NetworkClient(firebaseApp);
5849
this.backgroundExecutor = Executors.newCachedThreadPool();
5950
this.debugSecretTask =
6051
debugSecret == null

appcheck/firebase-appcheck-safetynet/firebase-appcheck-safetynet.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ dependencies {
4848
implementation 'com.google.android.gms:play-services-tasks:17.0.0'
4949
implementation 'com.google.android.gms:play-services-safetynet:17.0.0'
5050

51+
javadocClasspath 'com.google.auto.value:auto-value-annotations:1.6.6'
52+
5153
testImplementation 'junit:junit:4.13-beta-2'
5254
testImplementation 'org.mockito:mockito-core:2.25.0'
5355
testImplementation "org.robolectric:robolectric:$robolectricVersion"

appcheck/firebase-appcheck-safetynet/src/main/java/com/google/firebase/appcheck/safetynet/internal/SafetyNetAppCheckProvider.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,9 @@ public SafetyNetAppCheckProvider(@NonNull FirebaseApp firebaseApp) {
6767
checkNotNull(googleApiAvailability);
6868
this.context = firebaseApp.getApplicationContext();
6969
this.apiKey = firebaseApp.getOptions().getApiKey();
70-
String appId = firebaseApp.getOptions().getApplicationId();
71-
String projectId = firebaseApp.getOptions().getProjectId();
72-
if (projectId == null) {
73-
throw new IllegalArgumentException("FirebaseOptions#getProjectId cannot be null.");
74-
}
7570
this.backgroundExecutor = backgroundExecutor;
7671
this.safetyNetClientTask = initSafetyNetClient(googleApiAvailability, this.backgroundExecutor);
77-
this.networkClient = new NetworkClient(apiKey, appId, projectId);
72+
this.networkClient = new NetworkClient(firebaseApp);
7873
}
7974

8075
@VisibleForTesting

appcheck/firebase-appcheck/firebase-appcheck.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ dependencies {
4747
implementation 'com.google.android.gms:play-services-base:17.1.0'
4848
implementation 'com.google.android.gms:play-services-tasks:17.0.0'
4949

50+
javadocClasspath 'com.google.auto.value:auto-value-annotations:1.6.6'
51+
5052
testImplementation 'junit:junit:4.13-beta-2'
5153
testImplementation 'org.mockito:mockito-core:2.25.0'
5254
testImplementation 'org.mockito:mockito-inline:2.25.0'

appcheck/firebase-appcheck/src/main/java/com/google/firebase/appcheck/AppCheckProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
/**
2121
* Interface for a provider that generates {@link AppCheckToken}s. This provider can be called at
2222
* any time by any Firebase library that depends (optionally or otherwise) on {@link
23-
* AppCheckToken}s. This provider should be responsible for determining if a new token can be
24-
* created at the time of the call and returning that new token if it can be.
23+
* AppCheckToken}s. This provider is responsible for determining if it can create a new token at the
24+
* time of the call and returning that new token if it can.
2525
*/
2626
public interface AppCheckProvider {
2727

appcheck/firebase-appcheck/src/main/java/com/google/firebase/appcheck/FirebaseAppCheck.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public static FirebaseAppCheck getInstance(@NonNull FirebaseApp firebaseApp) {
3737
}
3838

3939
/**
40-
* Installs the given {@link AppCheckProviderFactory}, overwriting any that previously was
40+
* Installs the given {@link AppCheckProviderFactory}, overwriting any that were previously
4141
* associated with this FirebaseAppCheck instance. Any {@link AppCheckTokenListener}s attached to
42-
* this FirebaseAppCheck instance will be transferred from any existing factory to the newly
42+
* this FirebaseAppCheck instance will be transferred from existing factories to the newly
4343
* installed one.
4444
*
4545
* <p>Automatic token refreshing will only occur if the global {@code
@@ -52,9 +52,9 @@ public static FirebaseAppCheck getInstance(@NonNull FirebaseApp firebaseApp) {
5252
public abstract void installAppCheckProviderFactory(@NonNull AppCheckProviderFactory factory);
5353

5454
/**
55-
* Installs the given {@link AppCheckProviderFactory}, overwriting any that previously was
55+
* Installs the given {@link AppCheckProviderFactory}, overwriting any that were previously
5656
* associated with this FirebaseAppCheck instance. Any {@link AppCheckTokenListener}s attached to
57-
* this FirebaseAppCheck instance will be transferred from any existing factory to the newly
57+
* this FirebaseAppCheck instance will be transferred from existing factories to the newly
5858
* installed one.
5959
*
6060
* <p>Automatic token refreshing will only occur if the {@code isTokenAutoRefreshEnabled} field is

appcheck/firebase-appcheck/src/main/java/com/google/firebase/appcheck/FirebaseAppCheckRegistrar.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import com.google.firebase.components.Component;
2222
import com.google.firebase.components.ComponentRegistrar;
2323
import com.google.firebase.components.Dependency;
24+
import com.google.firebase.heartbeatinfo.HeartBeatInfo;
25+
import com.google.firebase.platforminfo.LibraryVersionComponent;
26+
import com.google.firebase.platforminfo.UserAgentPublisher;
2427
import java.util.Arrays;
2528
import java.util.List;
2629

@@ -38,8 +41,16 @@ public List<Component<?>> getComponents() {
3841
return Arrays.asList(
3942
Component.builder(FirebaseAppCheck.class, (InternalAppCheckTokenProvider.class))
4043
.add(Dependency.required(FirebaseApp.class))
41-
.factory((container) -> new DefaultFirebaseAppCheck(container.get(FirebaseApp.class)))
44+
.add(Dependency.optionalProvider(UserAgentPublisher.class))
45+
.add(Dependency.optionalProvider(HeartBeatInfo.class))
46+
.factory(
47+
(container) ->
48+
new DefaultFirebaseAppCheck(
49+
container.get(FirebaseApp.class),
50+
container.getProvider(UserAgentPublisher.class),
51+
container.getProvider(HeartBeatInfo.class)))
4252
.alwaysEager()
43-
.build());
53+
.build(),
54+
LibraryVersionComponent.create("fire-app-check", BuildConfig.VERSION_NAME));
4455
}
4556
}

appcheck/firebase-appcheck/src/main/java/com/google/firebase/appcheck/internal/DefaultFirebaseAppCheck.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,20 @@
3131
import com.google.firebase.appcheck.FirebaseAppCheck;
3232
import com.google.firebase.appcheck.internal.util.Clock;
3333
import com.google.firebase.appcheck.interop.AppCheckTokenListener;
34+
import com.google.firebase.heartbeatinfo.HeartBeatInfo;
35+
import com.google.firebase.inject.Provider;
36+
import com.google.firebase.platforminfo.UserAgentPublisher;
3437
import java.util.ArrayList;
3538
import java.util.List;
3639

3740
public class DefaultFirebaseAppCheck extends FirebaseAppCheck {
3841

3942
private static final long BUFFER_TIME_MILLIS = 5 * 60 * 1000; // 5 minutes in milliseconds
43+
private static final String HEART_BEAT_STORAGE_TAG = "fire-app-check";
4044

4145
private final FirebaseApp firebaseApp;
46+
private final Provider<UserAgentPublisher> userAgentPublisherProvider;
47+
private final Provider<HeartBeatInfo> heartBeatInfoProvider;
4248
private final List<AppCheckTokenListener> appCheckTokenListenerList;
4349
private final StorageHelper storageHelper;
4450
private final TokenRefreshManager tokenRefreshManager;
@@ -48,9 +54,16 @@ public class DefaultFirebaseAppCheck extends FirebaseAppCheck {
4854
private AppCheckProvider appCheckProvider;
4955
private AppCheckToken cachedToken;
5056

51-
public DefaultFirebaseAppCheck(@NonNull FirebaseApp firebaseApp) {
57+
public DefaultFirebaseAppCheck(
58+
@NonNull FirebaseApp firebaseApp,
59+
@NonNull Provider<UserAgentPublisher> userAgentPublisherProvider,
60+
@NonNull Provider<HeartBeatInfo> heartBeatInfoProvider) {
5261
checkNotNull(firebaseApp);
62+
checkNotNull(userAgentPublisherProvider);
63+
checkNotNull(heartBeatInfoProvider);
5364
this.firebaseApp = firebaseApp;
65+
this.userAgentPublisherProvider = userAgentPublisherProvider;
66+
this.heartBeatInfoProvider = heartBeatInfoProvider;
5467
this.appCheckTokenListenerList = new ArrayList<>();
5568
this.storageHelper =
5669
new StorageHelper(firebaseApp.getApplicationContext(), firebaseApp.getPersistenceKey());
@@ -155,6 +168,21 @@ public Task<AppCheckTokenResult> then(@NonNull Task<AppCheckToken> task) {
155168
});
156169
}
157170

171+
@Nullable
172+
String getUserAgent() {
173+
return userAgentPublisherProvider.get() != null
174+
? userAgentPublisherProvider.get().getUserAgent()
175+
: null;
176+
}
177+
178+
@Nullable
179+
String getHeartbeatCode() {
180+
return heartBeatInfoProvider.get() != null
181+
? Integer.toString(
182+
heartBeatInfoProvider.get().getHeartBeatCode(HEART_BEAT_STORAGE_TAG).getCode())
183+
: null;
184+
}
185+
158186
/** Sets the in-memory cached {@link AppCheckToken}. */
159187
@VisibleForTesting
160188
void setCachedToken(@NonNull AppCheckToken token) {

appcheck/firebase-appcheck/src/main/java/com/google/firebase/appcheck/internal/NetworkClient.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
import androidx.annotation.IntDef;
2020
import androidx.annotation.NonNull;
2121
import androidx.annotation.VisibleForTesting;
22+
import com.google.firebase.FirebaseApp;
2223
import com.google.firebase.FirebaseException;
23-
import com.google.firebase.appcheck.BuildConfig;
24+
import com.google.firebase.appcheck.FirebaseAppCheck;
2425
import java.io.BufferedOutputStream;
2526
import java.io.BufferedReader;
2627
import java.io.IOException;
@@ -46,9 +47,10 @@ public class NetworkClient {
4647
private static final String CONTENT_TYPE = "Content-Type";
4748
private static final String APPLICATION_JSON = "application/json";
4849
private static final String UTF_8 = "UTF-8";
49-
private static final String X_FIREBASE_CLIENT = "X-Firebase-Client";
50-
private static final String PLATFORM_NAME = "fire-app-check";
50+
@VisibleForTesting static final String X_FIREBASE_CLIENT = "X-Firebase-Client";
51+
@VisibleForTesting static final String X_FIREBASE_CLIENT_LOG_TYPE = "X-Firebase-Client-Log-Type";
5152

53+
private final DefaultFirebaseAppCheck firebaseAppCheck;
5254
private final String apiKey;
5355
private final String appId;
5456
private final String projectId;
@@ -61,13 +63,15 @@ public class NetworkClient {
6163
public static final int SAFETY_NET = 1;
6264
public static final int DEBUG = 2;
6365

64-
public NetworkClient(@NonNull String apiKey, @NonNull String appId, @NonNull String projectId) {
65-
checkNotNull(apiKey);
66-
checkNotNull(appId);
67-
checkNotNull(projectId);
68-
this.apiKey = apiKey;
69-
this.appId = appId;
70-
this.projectId = projectId;
66+
public NetworkClient(@NonNull FirebaseApp firebaseApp) {
67+
checkNotNull(firebaseApp);
68+
this.firebaseAppCheck = (DefaultFirebaseAppCheck) FirebaseAppCheck.getInstance(firebaseApp);
69+
this.apiKey = firebaseApp.getOptions().getApiKey();
70+
this.appId = firebaseApp.getOptions().getApplicationId();
71+
this.projectId = firebaseApp.getOptions().getProjectId();
72+
if (projectId == null) {
73+
throw new IllegalArgumentException("FirebaseOptions#getProjectId cannot be null.");
74+
}
7175
}
7276

7377
/**
@@ -85,8 +89,13 @@ public AppCheckTokenResponse exchangeAttestationForAppCheckToken(
8589
urlConnection.setDoOutput(true);
8690
urlConnection.setFixedLengthStreamingMode(requestBytes.length);
8791
urlConnection.setRequestProperty(CONTENT_TYPE, APPLICATION_JSON);
88-
urlConnection.setRequestProperty(
89-
X_FIREBASE_CLIENT, PLATFORM_NAME + "/" + BuildConfig.VERSION_NAME);
92+
if (firebaseAppCheck.getUserAgent() != null) {
93+
urlConnection.setRequestProperty(X_FIREBASE_CLIENT, firebaseAppCheck.getUserAgent());
94+
}
95+
if (firebaseAppCheck.getHeartbeatCode() != null) {
96+
urlConnection.setRequestProperty(
97+
X_FIREBASE_CLIENT_LOG_TYPE, firebaseAppCheck.getHeartbeatCode());
98+
}
9099

91100
try (OutputStream out =
92101
new BufferedOutputStream(urlConnection.getOutputStream(), requestBytes.length)) {

0 commit comments

Comments
 (0)