Skip to content

Commit 3352ea0

Browse files
committed
Do not chain the tasks of getting auth and appcheck tokens.
1 parent d0f05c4 commit 3352ea0

File tree

1 file changed

+64
-54
lines changed

1 file changed

+64
-54
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/remote/FirestoreCallCredentials.java

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

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

17+
import com.google.android.gms.tasks.Task;
1718
import com.google.android.gms.tasks.Tasks;
1819
import com.google.firebase.FirebaseApiNotAvailableException;
1920
import com.google.firebase.firestore.auth.CredentialsProvider;
@@ -52,66 +53,75 @@ public void thisUsesUnstableApi() {}
5253
@Override
5354
public void applyRequestMetadata(
5455
RequestInfo requestInfo, Executor executor, final MetadataApplier metadataApplier) {
55-
authProvider
56-
.getToken()
57-
.continueWithTask(
56+
Task<Metadata> authTask =
57+
authProvider
58+
.getToken()
59+
.continueWithTask(
60+
executor,
61+
task -> {
62+
Metadata metadata = new Metadata();
63+
if (task.isSuccessful()) {
64+
String token = task.getResult();
65+
Logger.debug(LOG_TAG, "Successfully fetched auth token.");
66+
if (token != null) {
67+
metadata.put(AUTHORIZATION_HEADER, "Bearer " + token);
68+
}
69+
return Tasks.forResult(metadata);
70+
} else {
71+
Exception exception = task.getException();
72+
if (exception instanceof FirebaseApiNotAvailableException) {
73+
Logger.debug(
74+
LOG_TAG, "Firebase Auth API not available, not using authentication.");
75+
return Tasks.forResult(metadata);
76+
} else if (exception instanceof FirebaseNoSignedInUserException) {
77+
Logger.debug(LOG_TAG, "No user signed in, not using authentication.");
78+
return Tasks.forResult(metadata);
79+
} else {
80+
Logger.warn(LOG_TAG, "Failed to get auth token: %s.", exception);
81+
return Tasks.forException(exception);
82+
}
83+
}
84+
});
85+
86+
Task<Metadata> appCheckTask =
87+
appCheckProvider
88+
.getToken()
89+
.continueWithTask(
90+
executor,
91+
task -> {
92+
Metadata metadata = new Metadata();
93+
if (task.isSuccessful()) {
94+
String token = task.getResult();
95+
if (token != null && !token.isEmpty()) {
96+
Logger.debug(LOG_TAG, "Successfully fetched AppCheck token.");
97+
metadata.put(X_FIREBASE_APPCHECK, token);
98+
}
99+
return Tasks.forResult(metadata);
100+
} else {
101+
Exception exception = task.getException();
102+
if (exception instanceof FirebaseApiNotAvailableException) {
103+
Logger.debug(LOG_TAG, "Firebase AppCheck API not available.");
104+
return Tasks.forResult(metadata);
105+
} else {
106+
Logger.warn(LOG_TAG, "Failed to get AppCheck token: %s.", exception);
107+
return Tasks.forException(exception);
108+
}
109+
}
110+
});
111+
112+
Tasks.whenAll(authTask, appCheckTask)
113+
.addOnSuccessListener(
58114
executor,
59-
task -> {
60-
Metadata metadata = new Metadata();
61-
if (task.isSuccessful()) {
62-
String token = task.getResult();
63-
Logger.debug(LOG_TAG, "Successfully fetched auth token.");
64-
if (token != null) {
65-
metadata.put(AUTHORIZATION_HEADER, "Bearer " + token);
66-
}
67-
return Tasks.forResult(metadata);
68-
} else {
69-
Exception exception = task.getException();
70-
if (exception instanceof FirebaseApiNotAvailableException) {
71-
Logger.debug(
72-
LOG_TAG, "Firebase Auth API not available, not using authentication.");
73-
return Tasks.forResult(metadata);
74-
} else if (exception instanceof FirebaseNoSignedInUserException) {
75-
Logger.debug(LOG_TAG, "No user signed in, not using authentication.");
76-
return Tasks.forResult(metadata);
77-
} else {
78-
Logger.warn(LOG_TAG, "Failed to get auth token: %s.", exception);
79-
return Tasks.forException(exception);
80-
}
81-
}
115+
unused -> {
116+
Metadata authMetadata = authTask.getResult();
117+
Metadata appCheckMetadata = appCheckTask.getResult();
118+
authMetadata.merge(appCheckMetadata);
119+
metadataApplier.apply(authMetadata);
82120
})
83121
.addOnFailureListener(
84122
executor,
85123
exception -> {
86-
// If the auth task has failed, there is no point in getting the AppCheck token.
87124
metadataApplier.fail(Status.UNAUTHENTICATED.withCause(exception));
88-
})
89-
.addOnSuccessListener(
90-
executor,
91-
metadata -> {
92-
// If the auth task has succeeded, try to get the AppCheck token as well.
93-
appCheckProvider
94-
.getToken()
95-
.addOnSuccessListener(
96-
executor,
97-
appCheckToken -> {
98-
if (appCheckToken != null && !appCheckToken.isEmpty()) {
99-
Logger.debug(LOG_TAG, "Successfully fetched AppCheck token.");
100-
metadata.put(X_FIREBASE_APPCHECK, appCheckToken);
101-
}
102-
metadataApplier.apply(metadata);
103-
})
104-
.addOnFailureListener(
105-
executor,
106-
exception -> {
107-
if (exception instanceof FirebaseApiNotAvailableException) {
108-
Logger.debug(LOG_TAG, "Firebase AppCheck API not available.");
109-
metadataApplier.apply(metadata);
110-
} else {
111-
Logger.warn(LOG_TAG, "Failed to get AppCheck token: %s.", exception);
112-
metadataApplier.fail(Status.UNAUTHENTICATED.withCause(exception));
113-
}
114-
});
115125
});
116126
}
117127
}

0 commit comments

Comments
 (0)