Skip to content

Commit 6cc75cd

Browse files
committed
[AppCheck] Recaptcha: use site key from FirebaseOptions
Removed the requirement to provide the site key manually during instantiation of RecaptchaEnterpriseAppCheckProviderFactory. The factory now retrieves the site key directly from FirebaseOptions via the FirebaseApp instance provided during the create method.
1 parent 87b6032 commit 6cc75cd

4 files changed

Lines changed: 35 additions & 24 deletions

File tree

appcheck/firebase-appcheck-recaptchaenterprise/api.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.google.firebase.appcheck.recaptchaenterprise {
33

44
public class RecaptchaEnterpriseAppCheckProviderFactory implements com.google.firebase.appcheck.AppCheckProviderFactory {
55
method public com.google.firebase.appcheck.AppCheckProvider create(com.google.firebase.FirebaseApp);
6-
method public static com.google.firebase.appcheck.recaptchaenterprise.RecaptchaEnterpriseAppCheckProviderFactory getInstance(String);
6+
method public static com.google.firebase.appcheck.recaptchaenterprise.RecaptchaEnterpriseAppCheckProviderFactory getInstance();
77
}
88

99
}

appcheck/firebase-appcheck-recaptchaenterprise/firebase-appcheck-recaptchaenterprise.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ dependencies {
5050
implementation(libs.dagger.dagger)
5151

5252
api project(':appcheck:firebase-appcheck')
53-
api libs.firebase.common
53+
api project(':firebase-common')
5454
api libs.firebase.components
5555
api 'com.google.android.recaptcha:recaptcha:18.7.1'
5656

appcheck/firebase-appcheck-recaptchaenterprise/src/main/java/com/google/firebase/appcheck/recaptchaenterprise/RecaptchaEnterpriseAppCheckProviderFactory.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,28 @@
1515
package com.google.firebase.appcheck.recaptchaenterprise;
1616

1717
import androidx.annotation.NonNull;
18+
import com.google.android.gms.common.internal.Preconditions;
1819
import com.google.firebase.FirebaseApp;
1920
import com.google.firebase.appcheck.AppCheckProvider;
2021
import com.google.firebase.appcheck.AppCheckProviderFactory;
2122
import com.google.firebase.appcheck.FirebaseAppCheck;
2223
import com.google.firebase.appcheck.recaptchaenterprise.internal.ProviderMultiResourceComponent;
2324
import com.google.firebase.appcheck.recaptchaenterprise.internal.RecaptchaEnterpriseAppCheckProvider;
24-
import java.util.Objects;
2525

2626
/**
2727
* Implementation of an {@link AppCheckProviderFactory} that builds <br>
2828
* {@link RecaptchaEnterpriseAppCheckProvider}s. This is the default implementation.
2929
*/
3030
public class RecaptchaEnterpriseAppCheckProviderFactory implements AppCheckProviderFactory {
3131

32-
private final String siteKey;
3332
private volatile RecaptchaEnterpriseAppCheckProvider provider;
3433

35-
private RecaptchaEnterpriseAppCheckProviderFactory(@NonNull String siteKey) {
36-
this.siteKey = siteKey;
37-
}
34+
private RecaptchaEnterpriseAppCheckProviderFactory() {}
3835

3936
/** Gets an instance of this class for installation into a {@link FirebaseAppCheck} instance. */
4037
@NonNull
41-
public static RecaptchaEnterpriseAppCheckProviderFactory getInstance(@NonNull String siteKey) {
42-
Objects.requireNonNull(siteKey, "siteKey cannot be null");
43-
return new RecaptchaEnterpriseAppCheckProviderFactory(siteKey);
38+
public static RecaptchaEnterpriseAppCheckProviderFactory getInstance() {
39+
return new RecaptchaEnterpriseAppCheckProviderFactory();
4440
}
4541

4642
@NonNull
@@ -50,6 +46,10 @@ public AppCheckProvider create(@NonNull FirebaseApp firebaseApp) {
5046
if (provider == null) {
5147
synchronized (this) {
5248
if (provider == null) {
49+
String siteKey = firebaseApp.getOptions().getRecaptchaSiteKey();
50+
Preconditions.checkNotEmpty(
51+
siteKey,
52+
"Missing site key from configuration. Verify your google-services.json file is updated.");
5353
ProviderMultiResourceComponent component =
5454
firebaseApp.get(ProviderMultiResourceComponent.class);
5555
provider = component.get(siteKey);

appcheck/firebase-appcheck-recaptchaenterprise/src/test/java/com/google/firebase/appcheck/recaptchaenterprise/RecaptchaEnterpriseAppCheckProviderFactoryTest.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,57 +24,68 @@
2424
import static org.mockito.Mockito.when;
2525

2626
import com.google.firebase.FirebaseApp;
27+
import com.google.firebase.FirebaseOptions;
2728
import com.google.firebase.appcheck.AppCheckProvider;
2829
import com.google.firebase.appcheck.recaptchaenterprise.internal.ProviderMultiResourceComponent;
2930
import com.google.firebase.appcheck.recaptchaenterprise.internal.RecaptchaEnterpriseAppCheckProvider;
3031
import org.junit.Before;
3132
import org.junit.Test;
3233
import org.junit.runner.RunWith;
3334
import org.mockito.Mock;
34-
import org.mockito.junit.MockitoJUnitRunner;
35+
import org.mockito.MockitoAnnotations;
36+
import org.robolectric.RobolectricTestRunner;
37+
import org.robolectric.annotation.Config;
3538

3639
/** Tests for {@link RecaptchaEnterpriseAppCheckProviderFactory}. */
37-
@RunWith(MockitoJUnitRunner.class)
40+
@RunWith(RobolectricTestRunner.class)
41+
@Config(manifest = Config.NONE)
3842
public class RecaptchaEnterpriseAppCheckProviderFactoryTest {
3943
static final String SITE_KEY_1 = "siteKey1";
4044

4145
@Mock private FirebaseApp mockFirebaseApp;
46+
@Mock private FirebaseOptions mockFirebaseOptions;
4247
@Mock private ProviderMultiResourceComponent mockComponent;
4348
@Mock private RecaptchaEnterpriseAppCheckProvider mockProvider;
4449

4550
@Before
4651
public void setUp() {
52+
MockitoAnnotations.openMocks(this);
4753
when(mockFirebaseApp.get(eq(ProviderMultiResourceComponent.class))).thenReturn(mockComponent);
4854
when(mockComponent.get(anyString())).thenReturn(mockProvider);
55+
when(mockFirebaseApp.getOptions()).thenReturn(mockFirebaseOptions);
4956
}
5057

5158
@Test
52-
public void getInstance_nonNullSiteKey_returnsNonNullInstance() {
59+
public void getInstance_returnsNonNullInstance() {
5360
RecaptchaEnterpriseAppCheckProviderFactory factory =
54-
RecaptchaEnterpriseAppCheckProviderFactory.getInstance(SITE_KEY_1);
61+
RecaptchaEnterpriseAppCheckProviderFactory.getInstance();
5562
assertNotNull(factory);
5663
}
5764

5865
@Test
59-
public void getInstance_nullSiteKey_expectThrows() {
60-
assertThrows(
61-
NullPointerException.class,
62-
() -> RecaptchaEnterpriseAppCheckProviderFactory.getInstance(null));
63-
}
64-
65-
@Test
66-
public void create_nonNullFirebaseApp_returnsRecaptchaEnterpriseAppCheckProvider() {
66+
public void create_siteKeyInOptions_returnsRecaptchaEnterpriseAppCheckProvider() {
67+
when(mockFirebaseOptions.getRecaptchaSiteKey()).thenReturn(SITE_KEY_1);
6768
RecaptchaEnterpriseAppCheckProviderFactory factory =
68-
RecaptchaEnterpriseAppCheckProviderFactory.getInstance(SITE_KEY_1);
69+
RecaptchaEnterpriseAppCheckProviderFactory.getInstance();
6970
AppCheckProvider provider = factory.create(mockFirebaseApp);
7071
assertNotNull(provider);
7172
assertEquals(RecaptchaEnterpriseAppCheckProvider.class, provider.getClass());
73+
verify(mockComponent).get(SITE_KEY_1);
74+
}
75+
76+
@Test
77+
public void create_noSiteKeyInOptionsOrFactory_expectThrows() {
78+
when(mockFirebaseOptions.getRecaptchaSiteKey()).thenReturn(null);
79+
RecaptchaEnterpriseAppCheckProviderFactory factory =
80+
RecaptchaEnterpriseAppCheckProviderFactory.getInstance();
81+
assertThrows(IllegalArgumentException.class, () -> factory.create(mockFirebaseApp));
7282
}
7383

7484
@Test
7585
public void create_callMultipleTimes_providerIsInitializedOnlyOnce() {
86+
when(mockFirebaseOptions.getRecaptchaSiteKey()).thenReturn(SITE_KEY_1);
7687
RecaptchaEnterpriseAppCheckProviderFactory factory =
77-
RecaptchaEnterpriseAppCheckProviderFactory.getInstance(SITE_KEY_1);
88+
RecaptchaEnterpriseAppCheckProviderFactory.getInstance();
7889

7990
factory.create(mockFirebaseApp);
8091
factory.create(mockFirebaseApp);

0 commit comments

Comments
 (0)