Skip to content

Commit 132d7c2

Browse files
committed
Implement platform logging.
1 parent aeab642 commit 132d7c2

File tree

5 files changed

+64
-30
lines changed

5 files changed

+64
-30
lines changed

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/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/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: 20 additions & 11 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;
@@ -47,8 +48,9 @@ public class NetworkClient {
4748
private static final String APPLICATION_JSON = "application/json";
4849
private static final String UTF_8 = "UTF-8";
4950
private static final String X_FIREBASE_CLIENT = "X-Firebase-Client";
50-
private static final String PLATFORM_NAME = "fire-app-check";
51+
private 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)