Skip to content

Commit 91633ec

Browse files
committed
Merge branch 'master' into rl.changelog_seeding
2 parents c384ab8 + f05d34b commit 91633ec

File tree

214 files changed

+4417
-1662
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

214 files changed

+4417
-1662
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
version=16.0.1
1+
version=16.0.3
2+
latestReleasedVersion=16.0.2
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
version=16.0.1
1+
version=16.0.3
2+
latestReleasedVersion=16.0.2

appcheck/firebase-appcheck-debug/src/main/java/com/google/firebase/appcheck/debug/FirebaseAppCheckDebugRegistrar.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
*/
3030
@KeepForSdk
3131
public class FirebaseAppCheckDebugRegistrar implements ComponentRegistrar {
32+
private static final String LIBRARY_NAME = "fire-app-check-debug";
3233

3334
@Override
3435
public List<Component<?>> getComponents() {
35-
return Arrays.asList(
36-
LibraryVersionComponent.create("fire-app-check-debug", BuildConfig.VERSION_NAME));
36+
return Arrays.asList(LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME));
3737
}
3838
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version=16.0.1
2-
latestReleasedVersion=16.0.0
1+
version=16.0.3
2+
latestReleasedVersion=16.0.2
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
version=16.0.1
1+
version=16.0.3
2+
latestReleasedVersion=16.0.2

appcheck/firebase-appcheck-playintegrity/src/main/java/com/google/firebase/appcheck/playintegrity/FirebaseAppCheckPlayIntegrityRegistrar.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
*/
3030
@KeepForSdk
3131
public class FirebaseAppCheckPlayIntegrityRegistrar implements ComponentRegistrar {
32+
private static final String LIBRARY_NAME = "fire-app-check-play-integrity";
3233

3334
@Override
3435
public List<Component<?>> getComponents() {
35-
return Arrays.asList(
36-
LibraryVersionComponent.create("fire-app-check-play-integrity", BuildConfig.VERSION_NAME));
36+
return Arrays.asList(LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME));
3737
}
3838
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
version=16.0.1
1+
version=16.0.3
2+
latestReleasedVersion=16.0.2

appcheck/firebase-appcheck-safetynet/src/main/java/com/google/firebase/appcheck/safetynet/FirebaseAppCheckSafetyNetRegistrar.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
*/
3030
@KeepForSdk
3131
public class FirebaseAppCheckSafetyNetRegistrar implements ComponentRegistrar {
32+
private static final String LIBRARY_NAME = "fire-app-check-safety-net";
3233

3334
@Override
3435
public List<Component<?>> getComponents() {
35-
return Arrays.asList(
36-
LibraryVersionComponent.create("fire-app-check-safety-net", BuildConfig.VERSION_NAME));
36+
return Arrays.asList(LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME));
3737
}
3838
}

appcheck/firebase-appcheck/firebase-appcheck.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,14 @@ dependencies {
5656
testImplementation "com.google.truth:truth:$googleTruthVersion"
5757
testImplementation 'androidx.test:core:1.2.0'
5858
testImplementation 'androidx.test:rules:1.2.0'
59+
60+
androidTestImplementation project(':appcheck:firebase-appcheck')
61+
androidTestImplementation project(':integ-testing')
62+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
63+
androidTestImplementation 'androidx.test:runner:1.2.0'
64+
androidTestImplementation "com.google.truth:truth:$googleTruthVersion"
65+
androidTestImplementation 'junit:junit:4.12'
66+
androidTestImplementation "androidx.annotation:annotation:1.0.0"
67+
androidTestImplementation 'org.mockito:mockito-core:2.25.0'
68+
androidTestImplementation 'org.mockito:mockito-inline:2.25.0'
5969
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
version=16.0.1
1+
version=16.0.3
2+
latestReleasedVersion=16.0.2
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<!-- Copyright 2021 Google LLC -->
2+
<!-- Copyright 2022 Google LLC -->
33
<!-- -->
44
<!-- Licensed under the Apache License, Version 2.0 (the "License"); -->
55
<!-- you may not use this file except in compliance with the License. -->
@@ -14,11 +14,13 @@
1414
<!-- limitations under the License. -->
1515

1616
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
17-
xmlns:tools="http://schemas.android.com/tools"
18-
package="com.google.firebase.benchmark.test">
17+
package="com.google.firebase.appcheck">
1918

20-
<uses-permission
21-
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
22-
tools:ignore="ScopedStorage" />
19+
<application>
20+
<uses-library android:name="android.test.runner"/>
21+
</application>
2322

23+
<instrumentation
24+
android:name="androidx.test.runner.AndroidJUnitRunner"
25+
android:targetPackage="com.google.firebase.appcheck" />
2426
</manifest>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.appcheck;
16+
17+
import androidx.test.core.app.ApplicationProvider;
18+
import androidx.test.ext.junit.runners.AndroidJUnit4;
19+
import com.google.firebase.FirebaseApp;
20+
import com.google.firebase.FirebaseOptions;
21+
import com.google.firebase.appcheck.interop.InternalAppCheckTokenProvider;
22+
import com.google.firebase.testing.integ.StrictModeRule;
23+
import org.junit.Rule;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
27+
@RunWith(AndroidJUnit4.class)
28+
public class StrictModeTest {
29+
30+
@Rule public StrictModeRule strictMode = new StrictModeRule();
31+
32+
@Test
33+
public void initializingFirebaseAppcheck_shouldNotViolateStrictMode() {
34+
strictMode.runOnMainThread(
35+
() -> {
36+
FirebaseApp app =
37+
FirebaseApp.initializeApp(
38+
ApplicationProvider.getApplicationContext(),
39+
new FirebaseOptions.Builder()
40+
.setApiKey("api")
41+
.setProjectId("123")
42+
.setApplicationId("appId")
43+
.build(),
44+
"hello");
45+
app.get(FirebaseAppCheck.class);
46+
app.get(InternalAppCheckTokenProvider.class);
47+
});
48+
}
49+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@
3535
*/
3636
@KeepForSdk
3737
public class FirebaseAppCheckRegistrar implements ComponentRegistrar {
38+
private static final String LIBRARY_NAME = "fire-app-check";
3839

3940
@Override
4041
public List<Component<?>> getComponents() {
4142
return Arrays.asList(
4243
Component.builder(FirebaseAppCheck.class, (InternalAppCheckTokenProvider.class))
44+
.name(LIBRARY_NAME)
4345
.add(Dependency.required(FirebaseApp.class))
4446
.add(Dependency.optionalProvider(HeartBeatController.class))
4547
.factory(
@@ -50,6 +52,6 @@ public List<Component<?>> getComponents() {
5052
.alwaysEager()
5153
.build(),
5254
HeartBeatConsumerComponent.create(),
53-
LibraryVersionComponent.create("fire-app-check", BuildConfig.VERSION_NAME));
55+
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME));
5456
}
5557
}

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

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import androidx.annotation.VisibleForTesting;
2222
import com.google.android.gms.tasks.Continuation;
2323
import com.google.android.gms.tasks.Task;
24+
import com.google.android.gms.tasks.TaskCompletionSource;
2425
import com.google.android.gms.tasks.Tasks;
2526
import com.google.firebase.FirebaseApp;
2627
import com.google.firebase.FirebaseException;
@@ -35,6 +36,8 @@
3536
import com.google.firebase.inject.Provider;
3637
import java.util.ArrayList;
3738
import java.util.List;
39+
import java.util.concurrent.ExecutorService;
40+
import java.util.concurrent.Executors;
3841

3942
public class DefaultFirebaseAppCheck extends FirebaseAppCheck {
4043

@@ -46,6 +49,8 @@ public class DefaultFirebaseAppCheck extends FirebaseAppCheck {
4649
private final List<AppCheckListener> appCheckListenerList;
4750
private final StorageHelper storageHelper;
4851
private final TokenRefreshManager tokenRefreshManager;
52+
private final ExecutorService backgroundExecutor;
53+
private final Task<Void> retrieveStoredTokenTask;
4954
private final Clock clock;
5055

5156
private AppCheckProviderFactory appCheckProviderFactory;
@@ -55,6 +60,17 @@ public class DefaultFirebaseAppCheck extends FirebaseAppCheck {
5560
public DefaultFirebaseAppCheck(
5661
@NonNull FirebaseApp firebaseApp,
5762
@NonNull Provider<HeartBeatController> heartBeatController) {
63+
this(
64+
checkNotNull(firebaseApp),
65+
checkNotNull(heartBeatController),
66+
Executors.newCachedThreadPool());
67+
}
68+
69+
@VisibleForTesting
70+
DefaultFirebaseAppCheck(
71+
@NonNull FirebaseApp firebaseApp,
72+
@NonNull Provider<HeartBeatController> heartBeatController,
73+
@NonNull ExecutorService backgroundExecutor) {
5874
checkNotNull(firebaseApp);
5975
checkNotNull(heartBeatController);
6076
this.firebaseApp = firebaseApp;
@@ -65,8 +81,22 @@ public DefaultFirebaseAppCheck(
6581
new StorageHelper(firebaseApp.getApplicationContext(), firebaseApp.getPersistenceKey());
6682
this.tokenRefreshManager =
6783
new TokenRefreshManager(firebaseApp.getApplicationContext(), /* firebaseAppCheck= */ this);
84+
this.backgroundExecutor = backgroundExecutor;
85+
this.retrieveStoredTokenTask = retrieveStoredAppCheckTokenInBackground(backgroundExecutor);
6886
this.clock = new Clock.DefaultClock();
69-
setCachedToken(storageHelper.retrieveAppCheckToken());
87+
}
88+
89+
private Task<Void> retrieveStoredAppCheckTokenInBackground(@NonNull ExecutorService executor) {
90+
TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();
91+
executor.execute(
92+
() -> {
93+
AppCheckToken token = storageHelper.retrieveAppCheckToken();
94+
if (token != null) {
95+
setCachedToken(token);
96+
}
97+
taskCompletionSource.setResult(null);
98+
});
99+
return taskCompletionSource.getTask();
70100
}
71101

72102
@Override
@@ -146,44 +176,50 @@ public void removeAppCheckListener(@NonNull AppCheckListener listener) {
146176
@NonNull
147177
@Override
148178
public Task<AppCheckTokenResult> getToken(boolean forceRefresh) {
149-
if (!forceRefresh && hasValidToken()) {
150-
return Tasks.forResult(DefaultAppCheckTokenResult.constructFromAppCheckToken(cachedToken));
151-
}
152-
if (appCheckProvider == null) {
153-
return Tasks.forResult(
154-
DefaultAppCheckTokenResult.constructFromError(
155-
new FirebaseException("No AppCheckProvider installed.")));
156-
}
157-
// TODO: Cache the in-flight task.
158-
return fetchTokenFromProvider()
159-
.continueWithTask(
160-
new Continuation<AppCheckToken, Task<AppCheckTokenResult>>() {
161-
@Override
162-
public Task<AppCheckTokenResult> then(@NonNull Task<AppCheckToken> task) {
163-
if (task.isSuccessful()) {
164-
return Tasks.forResult(
165-
DefaultAppCheckTokenResult.constructFromAppCheckToken(task.getResult()));
166-
}
167-
// If the token exchange failed, return a dummy token for integrators to attach in
168-
// their headers.
169-
return Tasks.forResult(
170-
DefaultAppCheckTokenResult.constructFromError(
171-
new FirebaseException(
172-
task.getException().getMessage(), task.getException())));
173-
}
174-
});
179+
return retrieveStoredTokenTask.continueWithTask(
180+
unused -> {
181+
if (!forceRefresh && hasValidToken()) {
182+
return Tasks.forResult(
183+
DefaultAppCheckTokenResult.constructFromAppCheckToken(cachedToken));
184+
}
185+
if (appCheckProvider == null) {
186+
return Tasks.forResult(
187+
DefaultAppCheckTokenResult.constructFromError(
188+
new FirebaseException("No AppCheckProvider installed.")));
189+
}
190+
// TODO: Cache the in-flight task.
191+
return fetchTokenFromProvider()
192+
.continueWithTask(
193+
appCheckTokenTask -> {
194+
if (appCheckTokenTask.isSuccessful()) {
195+
return Tasks.forResult(
196+
DefaultAppCheckTokenResult.constructFromAppCheckToken(
197+
appCheckTokenTask.getResult()));
198+
}
199+
// If the token exchange failed, return a dummy token for integrators to attach
200+
// in their headers.
201+
return Tasks.forResult(
202+
DefaultAppCheckTokenResult.constructFromError(
203+
new FirebaseException(
204+
appCheckTokenTask.getException().getMessage(),
205+
appCheckTokenTask.getException())));
206+
});
207+
});
175208
}
176209

177210
@NonNull
178211
@Override
179212
public Task<AppCheckToken> getAppCheckToken(boolean forceRefresh) {
180-
if (!forceRefresh && hasValidToken()) {
181-
return Tasks.forResult(cachedToken);
182-
}
183-
if (appCheckProvider == null) {
184-
return Tasks.forException(new FirebaseException("No AppCheckProvider installed."));
185-
}
186-
return fetchTokenFromProvider();
213+
return retrieveStoredTokenTask.continueWithTask(
214+
unused -> {
215+
if (!forceRefresh && hasValidToken()) {
216+
return Tasks.forResult(cachedToken);
217+
}
218+
if (appCheckProvider == null) {
219+
return Tasks.forException(new FirebaseException("No AppCheckProvider installed."));
220+
}
221+
return fetchTokenFromProvider();
222+
});
187223
}
188224

189225
/** Fetches an {@link AppCheckToken} via the installed {@link AppCheckProvider}. */
@@ -227,7 +263,7 @@ void setCachedToken(@NonNull AppCheckToken token) {
227263
* well as the in-memory cached {@link AppCheckToken}.
228264
*/
229265
private void updateStoredToken(@NonNull AppCheckToken token) {
230-
storageHelper.saveAppCheckToken(token);
266+
backgroundExecutor.execute(() -> storageHelper.saveAppCheckToken(token));
231267
setCachedToken(token);
232268

233269
tokenRefreshManager.maybeScheduleTokenRefresh(token);

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import androidx.annotation.VisibleForTesting;
2525
import com.google.firebase.appcheck.AppCheckToken;
2626
import com.google.firebase.appcheck.internal.util.Logger;
27+
import com.google.firebase.components.Lazy;
2728

2829
/**
2930
* Internal class used to persist {@link com.google.firebase.appcheck.AppCheckToken}s. Uses {@link
@@ -47,24 +48,27 @@ enum TokenType {
4748
UNKNOWN_APP_CHECK_TOKEN
4849
}
4950

50-
private SharedPreferences sharedPreferences;
51+
private Lazy<SharedPreferences> sharedPreferences;
5152

5253
public StorageHelper(@NonNull Context context, @NonNull String persistenceKey) {
5354
checkNotNull(context);
5455
checkNotEmpty(persistenceKey);
5556
String prefsName = String.format(PREFS_TEMPLATE, persistenceKey);
56-
this.sharedPreferences = context.getSharedPreferences(prefsName, Context.MODE_PRIVATE);
57+
this.sharedPreferences =
58+
new Lazy(() -> context.getSharedPreferences(prefsName, Context.MODE_PRIVATE));
5759
}
5860

5961
public void saveAppCheckToken(@NonNull AppCheckToken appCheckToken) {
6062
if (appCheckToken instanceof DefaultAppCheckToken) {
6163
sharedPreferences
64+
.get()
6265
.edit()
6366
.putString(TOKEN_KEY, ((DefaultAppCheckToken) appCheckToken).serializeTokenToString())
6467
.putString(TOKEN_TYPE_KEY, TokenType.DEFAULT_APP_CHECK_TOKEN.name())
6568
.apply();
6669
} else {
6770
sharedPreferences
71+
.get()
6872
.edit()
6973
.putString(TOKEN_KEY, appCheckToken.getToken())
7074
.putString(TOKEN_TYPE_KEY, TokenType.UNKNOWN_APP_CHECK_TOKEN.name())
@@ -74,8 +78,8 @@ public void saveAppCheckToken(@NonNull AppCheckToken appCheckToken) {
7478

7579
@Nullable
7680
public AppCheckToken retrieveAppCheckToken() {
77-
String tokenType = sharedPreferences.getString(TOKEN_TYPE_KEY, null);
78-
String serializedToken = sharedPreferences.getString(TOKEN_KEY, null);
81+
String tokenType = sharedPreferences.get().getString(TOKEN_TYPE_KEY, null);
82+
String serializedToken = sharedPreferences.get().getString(TOKEN_KEY, null);
7983
if (tokenType == null || serializedToken == null) {
8084
return null;
8185
}
@@ -101,6 +105,6 @@ public AppCheckToken retrieveAppCheckToken() {
101105
}
102106

103107
void clearSharedPrefs() {
104-
sharedPreferences.edit().remove(TOKEN_KEY).remove(TOKEN_TYPE_KEY).apply();
108+
sharedPreferences.get().edit().remove(TOKEN_KEY).remove(TOKEN_TYPE_KEY).apply();
105109
}
106110
}

0 commit comments

Comments
 (0)