Skip to content

Separate the implementation of initializeRecaptchaConfig #7515

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
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions packages/auth/src/core/auth/auth_impl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import * as navigator from '../util/navigator';
import * as reload from '../user/reload';
import { AuthImpl, DefaultConfig } from './auth_impl';
import { _initializeAuthInstance } from './initialize';
import { _initializeRecaptchaConfig } from '../../platform_browser/recaptcha/recaptcha_enterprise_verifier';
import { ClientPlatform } from '../util/version';
import { mockEndpointWithParams } from '../../../test/helpers/api/helper';
import { Endpoint, RecaptchaClientType, RecaptchaVersion } from '../../api';
Expand Down Expand Up @@ -734,7 +735,7 @@ describe('core/auth/auth_impl', () => {
},
recaptchaConfigResponseEnforce
);
await auth.initializeRecaptchaConfig();
await _initializeRecaptchaConfig(auth);

expect(auth._getRecaptchaConfig()).to.eql(cachedRecaptchaConfigEnforce);
});
Expand All @@ -751,7 +752,7 @@ describe('core/auth/auth_impl', () => {
},
recaptchaConfigResponseOff
);
await auth.initializeRecaptchaConfig();
await _initializeRecaptchaConfig(auth);

expect(auth._getRecaptchaConfig()).to.eql(cachedRecaptchaConfigOFF);
});
Expand All @@ -767,7 +768,7 @@ describe('core/auth/auth_impl', () => {
},
recaptchaConfigResponseEnforce
);
await auth.initializeRecaptchaConfig();
await _initializeRecaptchaConfig(auth);
auth.tenantId = 'tenant-id';
mockEndpointWithParams(
Endpoint.GET_RECAPTCHA_CONFIG,
Expand All @@ -778,7 +779,7 @@ describe('core/auth/auth_impl', () => {
},
recaptchaConfigResponseOff
);
await auth.initializeRecaptchaConfig();
await _initializeRecaptchaConfig(auth);

auth.tenantId = null;
expect(auth._getRecaptchaConfig()).to.eql(cachedRecaptchaConfigEnforce);
Expand Down
23 changes: 1 addition & 22 deletions packages/auth/src/core/auth/auth_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ import { _assert } from '../util/assert';
import { _getInstance } from '../util/instantiator';
import { _getUserLanguage } from '../util/navigator';
import { _getClientVersion } from '../util/version';
import { HttpHeader, RecaptchaClientType, RecaptchaVersion } from '../../api';
import { getRecaptchaConfig } from '../../api/authentication/recaptcha';
import { RecaptchaEnterpriseVerifier } from '../../platform_browser/recaptcha/recaptcha_enterprise_verifier';
import { HttpHeader } from '../../api';
import { AuthMiddlewareQueue } from './middleware';
import { RecaptchaConfig } from '../../platform_browser/recaptcha/recaptcha';
import { _logWarn } from '../util/log';
Expand Down Expand Up @@ -395,25 +393,6 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
});
}

async initializeRecaptchaConfig(): Promise<void> {
const response = await getRecaptchaConfig(this, {
clientType: RecaptchaClientType.WEB,
version: RecaptchaVersion.ENTERPRISE
});

const config = new RecaptchaConfig(response);
if (this.tenantId == null) {
this._agentRecaptchaConfig = config;
} else {
this._tenantRecaptchaConfigs[this.tenantId] = config;
}

if (config.emailPasswordEnabled) {
const verifier = new RecaptchaEnterpriseVerifier(this);
void verifier.verify();
}
}

_getRecaptchaConfig(): RecaptchaConfig | null {
if (this.tenantId == null) {
return this._agentRecaptchaConfig;
Expand Down
7 changes: 4 additions & 3 deletions packages/auth/src/core/credentials/email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { EmailAuthCredential } from './email';
import { MockGreCAPTCHATopLevel } from '../../platform_browser/recaptcha/recaptcha_mock';
import * as jsHelpers from '../../platform_browser/load_js';
import { ServerError } from '../../api/errors';
import { _initializeRecaptchaConfig } from '../../platform_browser/recaptcha/recaptcha_enterprise_verifier';

use(chaiAsPromised);

Expand Down Expand Up @@ -135,7 +136,7 @@ describe('core/credentials/email', () => {
},
recaptchaConfigResponseEnforce
);
await auth.initializeRecaptchaConfig();
await _initializeRecaptchaConfig(auth);

const idTokenResponse = await credential._getIdTokenResponse(auth);
expect(idTokenResponse.idToken).to.eq('id-token');
Expand Down Expand Up @@ -169,7 +170,7 @@ describe('core/credentials/email', () => {
},
recaptchaConfigResponseOff
);
await auth.initializeRecaptchaConfig();
await _initializeRecaptchaConfig(auth);

const idTokenResponse = await credential._getIdTokenResponse(auth);
expect(idTokenResponse.idToken).to.eq('id-token');
Expand Down Expand Up @@ -202,7 +203,7 @@ describe('core/credentials/email', () => {
},
recaptchaConfigResponseEnforce
);
await auth.initializeRecaptchaConfig();
await _initializeRecaptchaConfig(auth);
auth._agentRecaptchaConfig!.siteKey = 'cached-site-key';

await expect(credential._getIdTokenResponse(auth)).to.be.rejectedWith(
Expand Down
5 changes: 2 additions & 3 deletions packages/auth/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
ErrorFn,
Unsubscribe
} from '../model/public_types';
import { _castAuth } from '../core/auth/auth_impl';
import { _initializeRecaptchaConfig } from '../platform_browser/recaptcha/recaptcha_enterprise_verifier';

export {
debugErrorMap,
Expand Down Expand Up @@ -92,8 +92,7 @@ export function setPersistence(
* @public
*/
export function initializeRecaptchaConfig(auth: Auth): Promise<void> {
const authInternal = _castAuth(auth);
return authInternal.initializeRecaptchaConfig();
return _initializeRecaptchaConfig(auth);
}

/**
Expand Down
13 changes: 7 additions & 6 deletions packages/auth/src/core/strategies/email_and_password.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
verifyPasswordResetCode
} from './email_and_password';
import { MockGreCAPTCHATopLevel } from '../../platform_browser/recaptcha/recaptcha_mock';
import { _initializeRecaptchaConfig } from '../../platform_browser/recaptcha/recaptcha_enterprise_verifier';

use(chaiAsPromised);
use(sinonChai);
Expand Down Expand Up @@ -202,7 +203,7 @@ describe('core/strategies/sendPasswordResetEmail', () => {
},
recaptchaConfigResponseEnforce
);
await auth.initializeRecaptchaConfig();
await _initializeRecaptchaConfig(auth);

const apiMock = mockEndpoint(Endpoint.SEND_OOB_CODE, {
email
Expand Down Expand Up @@ -230,7 +231,7 @@ describe('core/strategies/sendPasswordResetEmail', () => {
},
recaptchaConfigResponseOff
);
await auth.initializeRecaptchaConfig();
await _initializeRecaptchaConfig(auth);

const apiMock = mockEndpoint(Endpoint.SEND_OOB_CODE, {
email
Expand Down Expand Up @@ -299,7 +300,7 @@ describe('core/strategies/sendPasswordResetEmail', () => {
},
recaptchaConfigResponseEnforce
);
await auth.initializeRecaptchaConfig();
await _initializeRecaptchaConfig(auth);

mockEndpoint(Endpoint.SEND_OOB_CODE, { email });
const response = await sendPasswordResetEmail(auth, email);
Expand Down Expand Up @@ -617,7 +618,7 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
},
recaptchaConfigResponseEnforce
);
await auth.initializeRecaptchaConfig();
await _initializeRecaptchaConfig(auth);

const { _tokenResponse, user, operationType } =
(await createUserWithEmailAndPassword(
Expand Down Expand Up @@ -648,7 +649,7 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
},
recaptchaConfigResponseEnforce
);
await auth.initializeRecaptchaConfig();
await _initializeRecaptchaConfig(auth);

const { _tokenResponse, user, operationType } =
(await createUserWithEmailAndPassword(
Expand Down Expand Up @@ -726,7 +727,7 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
},
recaptchaConfigResponseEnforce
);
await auth.initializeRecaptchaConfig();
await _initializeRecaptchaConfig(auth);

const { _tokenResponse, user, operationType } =
(await createUserWithEmailAndPassword(
Expand Down
9 changes: 5 additions & 4 deletions packages/auth/src/core/strategies/email_link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
} from './email_link';
import { MockGreCAPTCHATopLevel } from '../../platform_browser/recaptcha/recaptcha_mock';
import * as jsHelpers from '../../platform_browser/load_js';
import { _initializeRecaptchaConfig } from '../../platform_browser/recaptcha/recaptcha_enterprise_verifier';

use(chaiAsPromised);
use(sinonChai);
Expand Down Expand Up @@ -210,7 +211,7 @@ describe('core/strategies/sendSignInLinkToEmail', () => {
},
recaptchaConfigResponseEnforce
);
await auth.initializeRecaptchaConfig();
await _initializeRecaptchaConfig(auth);

const apiMock = mockEndpoint(Endpoint.SEND_OOB_CODE, {
email
Expand Down Expand Up @@ -239,7 +240,7 @@ describe('core/strategies/sendSignInLinkToEmail', () => {
},
recaptchaConfigResponseOff
);
await auth.initializeRecaptchaConfig();
await _initializeRecaptchaConfig(auth);

const apiMock = mockEndpoint(Endpoint.SEND_OOB_CODE, {
email
Expand Down Expand Up @@ -282,7 +283,7 @@ describe('core/strategies/sendSignInLinkToEmail', () => {
},
recaptchaConfigResponseEnforce
);
await auth.initializeRecaptchaConfig();
await _initializeRecaptchaConfig(auth);
auth._agentRecaptchaConfig!.siteKey = 'wrong-site-key';

mockEndpoint(Endpoint.SEND_OOB_CODE, {
Expand Down Expand Up @@ -352,7 +353,7 @@ describe('core/strategies/sendSignInLinkToEmail', () => {
},
recaptchaConfigResponseEnforce
);
await auth.initializeRecaptchaConfig();
await _initializeRecaptchaConfig(auth);

mockEndpoint(Endpoint.SEND_OOB_CODE, { email });
const response = await sendSignInLinkToEmail(auth, email, {
Expand Down
1 change: 0 additions & 1 deletion packages/auth/src/model/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,4 @@ export interface AuthInternal extends Auth {

useDeviceLanguage(): void;
signOut(): Promise<void>;
initializeRecaptchaConfig(): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,24 @@ export async function injectRecaptchaFields<T>(
});
return newRequest;
}

export async function _initializeRecaptchaConfig(auth: Auth): Promise<void> {
const authInternal = _castAuth(auth);

const response = await getRecaptchaConfig(authInternal, {
clientType: RecaptchaClientType.WEB,
version: RecaptchaVersion.ENTERPRISE
});

const config = new RecaptchaConfig(response);
if (authInternal.tenantId == null) {
authInternal._agentRecaptchaConfig = config;
} else {
authInternal._tenantRecaptchaConfigs[authInternal.tenantId] = config;
}

if (config.emailPasswordEnabled) {
const verifier = new RecaptchaEnterpriseVerifier(authInternal);
void verifier.verify();
}
}