Skip to content

Fix app-check-compat providers #5573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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