Skip to content

Commit f9a232a

Browse files
author
renkelvin
authored
Separate the implementation of initializeRecaptchaConfig (#7515)
1 parent b395277 commit f9a232a

File tree

8 files changed

+45
-43
lines changed

8 files changed

+45
-43
lines changed

packages/auth/src/core/auth/auth_impl.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import * as navigator from '../util/navigator';
4040
import * as reload from '../user/reload';
4141
import { AuthImpl, DefaultConfig } from './auth_impl';
4242
import { _initializeAuthInstance } from './initialize';
43+
import { _initializeRecaptchaConfig } from '../../platform_browser/recaptcha/recaptcha_enterprise_verifier';
4344
import { ClientPlatform } from '../util/version';
4445
import { mockEndpointWithParams } from '../../../test/helpers/api/helper';
4546
import { Endpoint, RecaptchaClientType, RecaptchaVersion } from '../../api';
@@ -734,7 +735,7 @@ describe('core/auth/auth_impl', () => {
734735
},
735736
recaptchaConfigResponseEnforce
736737
);
737-
await auth.initializeRecaptchaConfig();
738+
await _initializeRecaptchaConfig(auth);
738739

739740
expect(auth._getRecaptchaConfig()).to.eql(cachedRecaptchaConfigEnforce);
740741
});
@@ -751,7 +752,7 @@ describe('core/auth/auth_impl', () => {
751752
},
752753
recaptchaConfigResponseOff
753754
);
754-
await auth.initializeRecaptchaConfig();
755+
await _initializeRecaptchaConfig(auth);
755756

756757
expect(auth._getRecaptchaConfig()).to.eql(cachedRecaptchaConfigOFF);
757758
});
@@ -767,7 +768,7 @@ describe('core/auth/auth_impl', () => {
767768
},
768769
recaptchaConfigResponseEnforce
769770
);
770-
await auth.initializeRecaptchaConfig();
771+
await _initializeRecaptchaConfig(auth);
771772
auth.tenantId = 'tenant-id';
772773
mockEndpointWithParams(
773774
Endpoint.GET_RECAPTCHA_CONFIG,
@@ -778,7 +779,7 @@ describe('core/auth/auth_impl', () => {
778779
},
779780
recaptchaConfigResponseOff
780781
);
781-
await auth.initializeRecaptchaConfig();
782+
await _initializeRecaptchaConfig(auth);
782783

783784
auth.tenantId = null;
784785
expect(auth._getRecaptchaConfig()).to.eql(cachedRecaptchaConfigEnforce);

packages/auth/src/core/auth/auth_impl.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ import { _assert } from '../util/assert';
6161
import { _getInstance } from '../util/instantiator';
6262
import { _getUserLanguage } from '../util/navigator';
6363
import { _getClientVersion } from '../util/version';
64-
import { HttpHeader, RecaptchaClientType, RecaptchaVersion } from '../../api';
65-
import { getRecaptchaConfig } from '../../api/authentication/recaptcha';
66-
import { RecaptchaEnterpriseVerifier } from '../../platform_browser/recaptcha/recaptcha_enterprise_verifier';
64+
import { HttpHeader } from '../../api';
6765
import { AuthMiddlewareQueue } from './middleware';
6866
import { RecaptchaConfig } from '../../platform_browser/recaptcha/recaptcha';
6967
import { _logWarn } from '../util/log';
@@ -395,25 +393,6 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
395393
});
396394
}
397395

398-
async initializeRecaptchaConfig(): Promise<void> {
399-
const response = await getRecaptchaConfig(this, {
400-
clientType: RecaptchaClientType.WEB,
401-
version: RecaptchaVersion.ENTERPRISE
402-
});
403-
404-
const config = new RecaptchaConfig(response);
405-
if (this.tenantId == null) {
406-
this._agentRecaptchaConfig = config;
407-
} else {
408-
this._tenantRecaptchaConfigs[this.tenantId] = config;
409-
}
410-
411-
if (config.emailPasswordEnabled) {
412-
const verifier = new RecaptchaEnterpriseVerifier(this);
413-
void verifier.verify();
414-
}
415-
}
416-
417396
_getRecaptchaConfig(): RecaptchaConfig | null {
418397
if (this.tenantId == null) {
419398
return this._agentRecaptchaConfig;

packages/auth/src/core/credentials/email.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { EmailAuthCredential } from './email';
3838
import { MockGreCAPTCHATopLevel } from '../../platform_browser/recaptcha/recaptcha_mock';
3939
import * as jsHelpers from '../../platform_browser/load_js';
4040
import { ServerError } from '../../api/errors';
41+
import { _initializeRecaptchaConfig } from '../../platform_browser/recaptcha/recaptcha_enterprise_verifier';
4142

4243
use(chaiAsPromised);
4344

@@ -135,7 +136,7 @@ describe('core/credentials/email', () => {
135136
},
136137
recaptchaConfigResponseEnforce
137138
);
138-
await auth.initializeRecaptchaConfig();
139+
await _initializeRecaptchaConfig(auth);
139140

140141
const idTokenResponse = await credential._getIdTokenResponse(auth);
141142
expect(idTokenResponse.idToken).to.eq('id-token');
@@ -169,7 +170,7 @@ describe('core/credentials/email', () => {
169170
},
170171
recaptchaConfigResponseOff
171172
);
172-
await auth.initializeRecaptchaConfig();
173+
await _initializeRecaptchaConfig(auth);
173174

174175
const idTokenResponse = await credential._getIdTokenResponse(auth);
175176
expect(idTokenResponse.idToken).to.eq('id-token');
@@ -202,7 +203,7 @@ describe('core/credentials/email', () => {
202203
},
203204
recaptchaConfigResponseEnforce
204205
);
205-
await auth.initializeRecaptchaConfig();
206+
await _initializeRecaptchaConfig(auth);
206207
auth._agentRecaptchaConfig!.siteKey = 'cached-site-key';
207208

208209
await expect(credential._getIdTokenResponse(auth)).to.be.rejectedWith(

packages/auth/src/core/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
ErrorFn,
2626
Unsubscribe
2727
} from '../model/public_types';
28-
import { _castAuth } from '../core/auth/auth_impl';
28+
import { _initializeRecaptchaConfig } from '../platform_browser/recaptcha/recaptcha_enterprise_verifier';
2929

3030
export {
3131
debugErrorMap,
@@ -92,8 +92,7 @@ export function setPersistence(
9292
* @public
9393
*/
9494
export function initializeRecaptchaConfig(auth: Auth): Promise<void> {
95-
const authInternal = _castAuth(auth);
96-
return authInternal.initializeRecaptchaConfig();
95+
return _initializeRecaptchaConfig(auth);
9796
}
9897

9998
/**

packages/auth/src/core/strategies/email_and_password.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import {
5050
verifyPasswordResetCode
5151
} from './email_and_password';
5252
import { MockGreCAPTCHATopLevel } from '../../platform_browser/recaptcha/recaptcha_mock';
53+
import { _initializeRecaptchaConfig } from '../../platform_browser/recaptcha/recaptcha_enterprise_verifier';
5354

5455
use(chaiAsPromised);
5556
use(sinonChai);
@@ -202,7 +203,7 @@ describe('core/strategies/sendPasswordResetEmail', () => {
202203
},
203204
recaptchaConfigResponseEnforce
204205
);
205-
await auth.initializeRecaptchaConfig();
206+
await _initializeRecaptchaConfig(auth);
206207

207208
const apiMock = mockEndpoint(Endpoint.SEND_OOB_CODE, {
208209
email
@@ -230,7 +231,7 @@ describe('core/strategies/sendPasswordResetEmail', () => {
230231
},
231232
recaptchaConfigResponseOff
232233
);
233-
await auth.initializeRecaptchaConfig();
234+
await _initializeRecaptchaConfig(auth);
234235

235236
const apiMock = mockEndpoint(Endpoint.SEND_OOB_CODE, {
236237
email
@@ -299,7 +300,7 @@ describe('core/strategies/sendPasswordResetEmail', () => {
299300
},
300301
recaptchaConfigResponseEnforce
301302
);
302-
await auth.initializeRecaptchaConfig();
303+
await _initializeRecaptchaConfig(auth);
303304

304305
mockEndpoint(Endpoint.SEND_OOB_CODE, { email });
305306
const response = await sendPasswordResetEmail(auth, email);
@@ -617,7 +618,7 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
617618
},
618619
recaptchaConfigResponseEnforce
619620
);
620-
await auth.initializeRecaptchaConfig();
621+
await _initializeRecaptchaConfig(auth);
621622

622623
const { _tokenResponse, user, operationType } =
623624
(await createUserWithEmailAndPassword(
@@ -648,7 +649,7 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
648649
},
649650
recaptchaConfigResponseEnforce
650651
);
651-
await auth.initializeRecaptchaConfig();
652+
await _initializeRecaptchaConfig(auth);
652653

653654
const { _tokenResponse, user, operationType } =
654655
(await createUserWithEmailAndPassword(
@@ -726,7 +727,7 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
726727
},
727728
recaptchaConfigResponseEnforce
728729
);
729-
await auth.initializeRecaptchaConfig();
730+
await _initializeRecaptchaConfig(auth);
730731

731732
const { _tokenResponse, user, operationType } =
732733
(await createUserWithEmailAndPassword(

packages/auth/src/core/strategies/email_link.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
} from './email_link';
4747
import { MockGreCAPTCHATopLevel } from '../../platform_browser/recaptcha/recaptcha_mock';
4848
import * as jsHelpers from '../../platform_browser/load_js';
49+
import { _initializeRecaptchaConfig } from '../../platform_browser/recaptcha/recaptcha_enterprise_verifier';
4950

5051
use(chaiAsPromised);
5152
use(sinonChai);
@@ -210,7 +211,7 @@ describe('core/strategies/sendSignInLinkToEmail', () => {
210211
},
211212
recaptchaConfigResponseEnforce
212213
);
213-
await auth.initializeRecaptchaConfig();
214+
await _initializeRecaptchaConfig(auth);
214215

215216
const apiMock = mockEndpoint(Endpoint.SEND_OOB_CODE, {
216217
email
@@ -239,7 +240,7 @@ describe('core/strategies/sendSignInLinkToEmail', () => {
239240
},
240241
recaptchaConfigResponseOff
241242
);
242-
await auth.initializeRecaptchaConfig();
243+
await _initializeRecaptchaConfig(auth);
243244

244245
const apiMock = mockEndpoint(Endpoint.SEND_OOB_CODE, {
245246
email
@@ -282,7 +283,7 @@ describe('core/strategies/sendSignInLinkToEmail', () => {
282283
},
283284
recaptchaConfigResponseEnforce
284285
);
285-
await auth.initializeRecaptchaConfig();
286+
await _initializeRecaptchaConfig(auth);
286287
auth._agentRecaptchaConfig!.siteKey = 'wrong-site-key';
287288

288289
mockEndpoint(Endpoint.SEND_OOB_CODE, {
@@ -352,7 +353,7 @@ describe('core/strategies/sendSignInLinkToEmail', () => {
352353
},
353354
recaptchaConfigResponseEnforce
354355
);
355-
await auth.initializeRecaptchaConfig();
356+
await _initializeRecaptchaConfig(auth);
356357

357358
mockEndpoint(Endpoint.SEND_OOB_CODE, { email });
358359
const response = await sendSignInLinkToEmail(auth, email, {

packages/auth/src/model/auth.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,4 @@ export interface AuthInternal extends Auth {
9797

9898
useDeviceLanguage(): void;
9999
signOut(): Promise<void>;
100-
initializeRecaptchaConfig(): Promise<void>;
101100
}

packages/auth/src/platform_browser/recaptcha/recaptcha_enterprise_verifier.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,24 @@ export async function injectRecaptchaFields<T>(
174174
});
175175
return newRequest;
176176
}
177+
178+
export async function _initializeRecaptchaConfig(auth: Auth): Promise<void> {
179+
const authInternal = _castAuth(auth);
180+
181+
const response = await getRecaptchaConfig(authInternal, {
182+
clientType: RecaptchaClientType.WEB,
183+
version: RecaptchaVersion.ENTERPRISE
184+
});
185+
186+
const config = new RecaptchaConfig(response);
187+
if (authInternal.tenantId == null) {
188+
authInternal._agentRecaptchaConfig = config;
189+
} else {
190+
authInternal._tenantRecaptchaConfigs[authInternal.tenantId] = config;
191+
}
192+
193+
if (config.emailPasswordEnabled) {
194+
const verifier = new RecaptchaEnterpriseVerifier(authInternal);
195+
void verifier.verify();
196+
}
197+
}

0 commit comments

Comments
 (0)