Skip to content

Commit 3e920c8

Browse files
authored
Fix app-check-compat providers (#5573)
1 parent 15719e0 commit 3e920c8

File tree

5 files changed

+59
-6
lines changed

5 files changed

+59
-6
lines changed

.changeset/wild-vans-camp.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@firebase/app-check-compat': patch
3+
'firebase': patch
4+
---
5+
6+
Fixed App Check compat package to correctly export and handle `ReCaptchaV3Provider` and `CustomProvider` classes.

packages/app-check-compat/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
} from '@firebase/component';
2929
import { AppCheckService } from './service';
3030
import { FirebaseAppCheck } from '@firebase/app-check-types';
31+
import { ReCaptchaV3Provider, CustomProvider } from '@firebase/app-check';
3132

3233
const factory: InstanceFactory<'appCheck-compat'> = (
3334
container: ComponentContainer
@@ -40,7 +41,14 @@ const factory: InstanceFactory<'appCheck-compat'> = (
4041

4142
export function registerAppCheck(): void {
4243
(firebase as _FirebaseNamespace).INTERNAL.registerComponent(
43-
new Component('appCheck-compat', factory, ComponentType.PUBLIC)
44+
new Component(
45+
'appCheck-compat',
46+
factory,
47+
ComponentType.PUBLIC
48+
).setServiceProps({
49+
ReCaptchaV3Provider,
50+
CustomProvider
51+
})
4452
);
4553
}
4654

packages/app-check-compat/src/service.test.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('Firebase App Check > Service', () => {
6565

6666
it(
6767
'activate("string") calls modular initializeAppCheck() with a ' +
68-
'ReCaptchaV3Provider',
68+
'ReCaptchaV3Provider',
6969
() => {
7070
const initializeAppCheckStub = stub(appCheckExp, 'initializeAppCheck');
7171
service = new AppCheckService(app);
@@ -79,8 +79,8 @@ describe('Firebase App Check > Service', () => {
7979
);
8080

8181
it(
82-
'activate(CustomProvider) calls modular initializeAppCheck() with' +
83-
' a CustomProvider',
82+
'activate({getToken: () => token}) calls modular initializeAppCheck() with' +
83+
' a CustomProvider',
8484
() => {
8585
const initializeAppCheckStub = stub(appCheckExp, 'initializeAppCheck');
8686
service = new AppCheckService(app);
@@ -103,6 +103,37 @@ describe('Firebase App Check > Service', () => {
103103
}
104104
);
105105

106+
it(
107+
'activate(new RecaptchaV3Provider(...)) calls modular initializeAppCheck() with' +
108+
' a RecaptchaV3Provider',
109+
() => {
110+
const initializeAppCheckStub = stub(appCheckExp, 'initializeAppCheck');
111+
service = new AppCheckService(app);
112+
service.activate(new ReCaptchaV3Provider('a-site-key'));
113+
expect(initializeAppCheckStub).to.be.calledWith(app, {
114+
provider: match.instanceOf(ReCaptchaV3Provider),
115+
isTokenAutoRefreshEnabled: undefined
116+
});
117+
initializeAppCheckStub.restore();
118+
}
119+
);
120+
121+
it(
122+
'activate(new CustomProvider(...)) calls modular initializeAppCheck() with' +
123+
' a CustomProvider',
124+
() => {
125+
const initializeAppCheckStub = stub(appCheckExp, 'initializeAppCheck');
126+
service = new AppCheckService(app);
127+
const customGetTokenStub = stub();
128+
service.activate(new CustomProvider({ getToken: customGetTokenStub }));
129+
expect(initializeAppCheckStub).to.be.calledWith(app, {
130+
provider: match.instanceOf(CustomProvider),
131+
isTokenAutoRefreshEnabled: undefined
132+
});
133+
initializeAppCheckStub.restore();
134+
}
135+
);
136+
106137
it('setTokenAutoRefreshEnabled() calls modular setTokenAutoRefreshEnabled()', () => {
107138
const setTokenAutoRefreshEnabledStub: SinonStub = stub(
108139
appCheckExp,
@@ -167,7 +198,7 @@ describe('Firebase App Check > Service', () => {
167198

168199
it('onTokenChanged() throws if activate() has not been called', async () => {
169200
service = createTestService(app);
170-
expect(() => service.onTokenChanged(() => { })).to.throw(
201+
expect(() => service.onTokenChanged(() => {})).to.throw(
171202
AppCheckError.USE_BEFORE_ACTIVATION
172203
);
173204
});

packages/app-check-compat/src/service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ export class AppCheckService
4646
let provider: ReCaptchaV3Provider | CustomProvider;
4747
if (typeof siteKeyOrProvider === 'string') {
4848
provider = new ReCaptchaV3Provider(siteKeyOrProvider);
49+
} else if (
50+
siteKeyOrProvider instanceof ReCaptchaV3Provider ||
51+
siteKeyOrProvider instanceof CustomProvider
52+
) {
53+
provider = siteKeyOrProvider;
4954
} else {
5055
provider = new CustomProvider({ getToken: siteKeyOrProvider.getToken });
5156
}

packages/firebase/compat/index.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1580,7 +1580,9 @@ declare namespace firebase.appCheck {
15801580
export interface AppCheck {
15811581
/**
15821582
* Activate AppCheck
1583-
* @param provider reCAPTCHA provider, custom token provider, or reCAPTCHA site key.
1583+
* @param provider This can be a `ReCaptchaV3Provider` instance,
1584+
* a `CustomProvider` instance, an object with a custom `getToken()`
1585+
* method, or a reCAPTCHA site key.
15841586
* @param isTokenAutoRefreshEnabled If true, the SDK automatically
15851587
* refreshes App Check tokens as needed. If undefined, defaults to the
15861588
* value of `app.automaticDataCollectionEnabled`, which defaults to
@@ -1591,6 +1593,7 @@ declare namespace firebase.appCheck {
15911593
| ReCaptchaV3Provider
15921594
| CustomProvider
15931595
| AppCheckProvider
1596+
| { getToken: () => AppCheckToken }
15941597
| string,
15951598
isTokenAutoRefreshEnabled?: boolean
15961599
): void;

0 commit comments

Comments
 (0)