Skip to content

Commit

Permalink
Fix app-check-compat providers (#5573)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsubox76 committed Oct 5, 2021
1 parent 15719e0 commit 3e920c8
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/wild-vans-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@firebase/app-check-compat': patch
'firebase': patch
---

Fixed App Check compat package to correctly export and handle `ReCaptchaV3Provider` and `CustomProvider` classes.
10 changes: 9 additions & 1 deletion packages/app-check-compat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from '@firebase/component';
import { AppCheckService } from './service';
import { FirebaseAppCheck } from '@firebase/app-check-types';
import { ReCaptchaV3Provider, CustomProvider } from '@firebase/app-check';

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

export function registerAppCheck(): void {
(firebase as _FirebaseNamespace).INTERNAL.registerComponent(
new Component('appCheck-compat', factory, ComponentType.PUBLIC)
new Component(
'appCheck-compat',
factory,
ComponentType.PUBLIC
).setServiceProps({
ReCaptchaV3Provider,
CustomProvider
})
);
}

Expand Down
39 changes: 35 additions & 4 deletions packages/app-check-compat/src/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('Firebase App Check > Service', () => {

it(
'activate("string") calls modular initializeAppCheck() with a ' +
'ReCaptchaV3Provider',
'ReCaptchaV3Provider',
() => {
const initializeAppCheckStub = stub(appCheckExp, 'initializeAppCheck');
service = new AppCheckService(app);
Expand All @@ -79,8 +79,8 @@ describe('Firebase App Check > Service', () => {
);

it(
'activate(CustomProvider) calls modular initializeAppCheck() with' +
' a CustomProvider',
'activate({getToken: () => token}) calls modular initializeAppCheck() with' +
' a CustomProvider',
() => {
const initializeAppCheckStub = stub(appCheckExp, 'initializeAppCheck');
service = new AppCheckService(app);
Expand All @@ -103,6 +103,37 @@ describe('Firebase App Check > Service', () => {
}
);

it(
'activate(new RecaptchaV3Provider(...)) calls modular initializeAppCheck() with' +
' a RecaptchaV3Provider',
() => {
const initializeAppCheckStub = stub(appCheckExp, 'initializeAppCheck');
service = new AppCheckService(app);
service.activate(new ReCaptchaV3Provider('a-site-key'));
expect(initializeAppCheckStub).to.be.calledWith(app, {
provider: match.instanceOf(ReCaptchaV3Provider),
isTokenAutoRefreshEnabled: undefined
});
initializeAppCheckStub.restore();
}
);

it(
'activate(new CustomProvider(...)) calls modular initializeAppCheck() with' +
' a CustomProvider',
() => {
const initializeAppCheckStub = stub(appCheckExp, 'initializeAppCheck');
service = new AppCheckService(app);
const customGetTokenStub = stub();
service.activate(new CustomProvider({ getToken: customGetTokenStub }));
expect(initializeAppCheckStub).to.be.calledWith(app, {
provider: match.instanceOf(CustomProvider),
isTokenAutoRefreshEnabled: undefined
});
initializeAppCheckStub.restore();
}
);

it('setTokenAutoRefreshEnabled() calls modular setTokenAutoRefreshEnabled()', () => {
const setTokenAutoRefreshEnabledStub: SinonStub = stub(
appCheckExp,
Expand Down Expand Up @@ -167,7 +198,7 @@ describe('Firebase App Check > Service', () => {

it('onTokenChanged() throws if activate() has not been called', async () => {
service = createTestService(app);
expect(() => service.onTokenChanged(() => { })).to.throw(
expect(() => service.onTokenChanged(() => {})).to.throw(
AppCheckError.USE_BEFORE_ACTIVATION
);
});
Expand Down
5 changes: 5 additions & 0 deletions packages/app-check-compat/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export class AppCheckService
let provider: ReCaptchaV3Provider | CustomProvider;
if (typeof siteKeyOrProvider === 'string') {
provider = new ReCaptchaV3Provider(siteKeyOrProvider);
} else if (
siteKeyOrProvider instanceof ReCaptchaV3Provider ||
siteKeyOrProvider instanceof CustomProvider
) {
provider = siteKeyOrProvider;
} else {
provider = new CustomProvider({ getToken: siteKeyOrProvider.getToken });
}
Expand Down
5 changes: 4 additions & 1 deletion packages/firebase/compat/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,9 @@ declare namespace firebase.appCheck {
export interface AppCheck {
/**
* Activate AppCheck
* @param provider reCAPTCHA provider, custom token provider, or reCAPTCHA site key.
* @param provider This can be a `ReCaptchaV3Provider` instance,
* a `CustomProvider` instance, an object with a custom `getToken()`
* method, or a reCAPTCHA site key.
* @param isTokenAutoRefreshEnabled If true, the SDK automatically
* refreshes App Check tokens as needed. If undefined, defaults to the
* value of `app.automaticDataCollectionEnabled`, which defaults to
Expand All @@ -1591,6 +1593,7 @@ declare namespace firebase.appCheck {
| ReCaptchaV3Provider
| CustomProvider
| AppCheckProvider
| { getToken: () => AppCheckToken }
| string,
isTokenAutoRefreshEnabled?: boolean
): void;
Expand Down

0 comments on commit 3e920c8

Please sign in to comment.