Skip to content

Commit c8f5246

Browse files
author
chuanr
committed
Apply patch
1 parent 37de4d0 commit c8f5246

34 files changed

+1726
-63
lines changed

common/api-review/auth.api.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export interface Auth {
8585
readonly config: Config;
8686
readonly currentUser: User | null;
8787
readonly emulatorConfig: EmulatorConfig | null;
88+
initializeRecaptchaConfig(): Promise<void>;
8889
languageCode: string | null;
8990
readonly name: string;
9091
onAuthStateChanged(nextOrObserver: NextOrObserver<User | null>, error?: ErrorFn, completed?: CompleteFn): Unsubscribe;
@@ -226,6 +227,8 @@ export const AuthErrorCodes: {
226227
readonly WEAK_PASSWORD: "auth/weak-password";
227228
readonly WEB_STORAGE_UNSUPPORTED: "auth/web-storage-unsupported";
228229
readonly ALREADY_INITIALIZED: "auth/already-initialized";
230+
readonly RECAPTCHA_CHECK_FAILED: "auth/recaptcha-check-failed";
231+
readonly RECAPTCHA_NOT_ENABLED: "auth/recaptcha-not-enabled";
229232
};
230233

231234
// @public
@@ -422,6 +425,9 @@ export const indexedDBLocalPersistence: Persistence;
422425
// @public
423426
export function initializeAuth(app: FirebaseApp, deps?: Dependencies): Auth;
424427

428+
// @public
429+
export function initializeRecaptchaConfig(auth: Auth): Promise<void>;
430+
425431
// @public
426432
export const inMemoryPersistence: Persistence;
427433

@@ -660,6 +666,11 @@ export function reauthenticateWithPopup(user: User, provider: AuthProvider, reso
660666
// @public
661667
export function reauthenticateWithRedirect(user: User, provider: AuthProvider, resolver?: PopupRedirectResolver): Promise<never>;
662668

669+
// @public (undocumented)
670+
export interface RecaptchaConfig {
671+
emailPasswordEnabled: boolean;
672+
}
673+
663674
// @public
664675
export interface RecaptchaParameters {
665676
// (undocumented)

docs-devsite/auth.auth.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface Auth
3838
| Method | Description |
3939
| --- | --- |
4040
| [beforeAuthStateChanged(callback, onAbort)](./auth.auth.md#authbeforeauthstatechanged) | Adds a blocking callback that runs before an auth state change sets a new user. |
41+
| [initializeRecaptchaConfig()](./auth.auth.md#authinitializerecaptchaconfig) | Initializes the reCAPTCHA configuration on the <code>Auth</code> instance. |
4142
| [onAuthStateChanged(nextOrObserver, error, completed)](./auth.auth.md#authonauthstatechanged) | Adds an observer for changes to the user's sign-in state. |
4243
| [onIdTokenChanged(nextOrObserver, error, completed)](./auth.auth.md#authonidtokenchanged) | Adds an observer for changes to the signed-in user's ID token. |
4344
| [setPersistence(persistence)](./auth.auth.md#authsetpersistence) | Changes the type of persistence on the <code>Auth</code> instance. |
@@ -165,6 +166,33 @@ beforeAuthStateChanged(callback: (user: User | null) => void | Promise<void>, on
165166

166167
[Unsubscribe](./util.md#unsubscribe)
167168

169+
## Auth.initializeRecaptchaConfig()
170+
171+
Initializes the reCAPTCHA configuration on the `Auth` instance.
172+
173+
This will initialize reCAPTCHA config of the current Auth session which affects the future auth requests.
174+
175+
The reCAPTCHA config indicates whether the reCAPTCHA verification flow should be triggered for a specific auth provider. Note that this only affect the client auth request but won't override the actual enablement state on the server side.
176+
177+
For example, assume that reCAPTCHA verfication is enabled for Email provider via Cloud console or Admin SDKs. If the enablement is initialized to false via `initializeRecaptchaConfig()`<!-- -->, the auth flow will be started without the reCAPTCHA verfication. This will result in a `reCAPTCHA token missing` error while the SDK will automatically start the auth flow again with the reCAPTCHA verfication flow. Developers can avoid such round trip by enabling the reCAPTCHA flow with this method.
178+
179+
<b>Signature:</b>
180+
181+
```typescript
182+
initializeRecaptchaConfig(): Promise<void>;
183+
```
184+
<b>Returns:</b>
185+
186+
Promise&lt;void&gt;
187+
188+
### Example
189+
190+
191+
```javascript
192+
auth.initializeRecaptchaConfig();
193+
194+
```
195+
168196
## Auth.onAuthStateChanged()
169197

170198
Adds an observer for changes to the user's sign-in state.

docs-devsite/auth.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Firebase Authentication
3131
| [fetchSignInMethodsForEmail(auth, email)](./auth.md#fetchsigninmethodsforemail) | Gets the list of possible sign in methods for the given email address. |
3232
| [getMultiFactorResolver(auth, error)](./auth.md#getmultifactorresolver) | Provides a [MultiFactorResolver](./auth.multifactorresolver.md#multifactorresolver_interface) suitable for completion of a multi-factor flow. |
3333
| [getRedirectResult(auth, resolver)](./auth.md#getredirectresult) | Returns a [UserCredential](./auth.usercredential.md#usercredential_interface) from the redirect-based sign-in flow. |
34+
| [initializeRecaptchaConfig(auth)](./auth.md#initializerecaptchaconfig) | Initializes the reCAPTCHA configuration on the <code>Auth</code> instance. |
3435
| [isSignInWithEmailLink(auth, emailLink)](./auth.md#issigninwithemaillink) | Checks if an incoming link is a sign-in with email link suitable for [signInWithEmailLink()](./auth.md#signinwithemaillink)<!-- -->. |
3536
| [onAuthStateChanged(auth, nextOrObserver, error, completed)](./auth.md#onauthstatechanged) | Adds an observer for changes to the user's sign-in state. |
3637
| [onIdTokenChanged(auth, nextOrObserver, error, completed)](./auth.md#onidtokenchanged) | Adds an observer for changes to the signed-in user's ID token. |
@@ -131,6 +132,7 @@ Firebase Authentication
131132
| [PhoneSingleFactorInfoOptions](./auth.phonesinglefactorinfooptions.md#phonesinglefactorinfooptions_interface) | Options used for single-factor sign-in. |
132133
| [PopupRedirectResolver](./auth.popupredirectresolver.md#popupredirectresolver_interface) | A resolver used for handling DOM specific operations like [signInWithPopup()](./auth.md#signinwithpopup) or [signInWithRedirect()](./auth.md#signinwithredirect)<!-- -->. |
133134
| [ReactNativeAsyncStorage](./auth.reactnativeasyncstorage.md#reactnativeasyncstorage_interface) | Interface for a supplied <code>AsyncStorage</code>. |
135+
| [RecaptchaConfig](./auth.recaptchaconfig.md#recaptchaconfig_interface) | |
134136
| [RecaptchaParameters](./auth.recaptchaparameters.md#recaptchaparameters_interface) | Interface representing reCAPTCHA parameters.<!-- -->See the \[reCAPTCHA docs\](https://developers.google.com/recaptcha/docs/display\#render\_param) for the list of accepted parameters. All parameters are accepted except for <code>sitekey</code>: Firebase Auth provisions a reCAPTCHA for each project and will configure the site key upon rendering.<!-- -->For an invisible reCAPTCHA, set the <code>size</code> key to <code>invisible</code>. |
135137
| [TotpMultiFactorAssertion](./auth.totpmultifactorassertion.md#totpmultifactorassertion_interface) | The class for asserting ownership of a TOTP second factor. Provided by [TotpMultiFactorGenerator.assertionForEnrollment()](./auth.totpmultifactorgenerator.md#totpmultifactorgeneratorassertionforenrollment) and [TotpMultiFactorGenerator.assertionForSignIn()](./auth.totpmultifactorgenerator.md#totpmultifactorgeneratorassertionforsignin)<!-- -->. |
136138
| [TotpMultiFactorInfo](./auth.totpmultifactorinfo.md#totpmultifactorinfo_interface) | The subclass of the [MultiFactorInfo](./auth.multifactorinfo.md#multifactorinfo_interface) interface for TOTP second factors. The <code>factorId</code> of this second factor is [FactorId](./auth.md#factorid)<!-- -->.TOTP. |
@@ -486,6 +488,36 @@ const operationType = result.operationType;
486488

487489
```
488490

491+
## initializeRecaptchaConfig()
492+
493+
Initializes the reCAPTCHA configuration on the `Auth` instance.
494+
495+
This will pull the reCAPTCHA config to the current Auth session and affect future auth requests, which indicates whether the reCAPTCHA verification flow should be triggered for a specific auth provider. If initializeRecaptchaConfig() is not invoked, the auth flow will start without reCAPTCHA verification. But if reCAPTCHA verification is required, the reCAPTCHA config will be automatically pulled internally and the flows will restart. Thus, calling this optional method will reduce the latency of auth flows.
496+
497+
<b>Signature:</b>
498+
499+
```typescript
500+
export declare function initializeRecaptchaConfig(auth: Auth): Promise<void>;
501+
```
502+
503+
### Parameters
504+
505+
| Parameter | Type | Description |
506+
| --- | --- | --- |
507+
| auth | [Auth](./auth.auth.md#auth_interface) | The [Auth](./auth.auth.md#auth_interface) instance. |
508+
509+
<b>Returns:</b>
510+
511+
Promise&lt;void&gt;
512+
513+
### Example
514+
515+
516+
```javascript
517+
initializeRecaptchaConfig(auth);
518+
519+
```
520+
489521
## isSignInWithEmailLink()
490522

491523
Checks if an incoming link is a sign-in with email link suitable for [signInWithEmailLink()](./auth.md#signinwithemaillink)<!-- -->.
@@ -1795,6 +1827,8 @@ AUTH_ERROR_CODES_MAP_DO_NOT_USE_INTERNALLY: {
17951827
readonly WEAK_PASSWORD: "auth/weak-password";
17961828
readonly WEB_STORAGE_UNSUPPORTED: "auth/web-storage-unsupported";
17971829
readonly ALREADY_INITIALIZED: "auth/already-initialized";
1830+
readonly RECAPTCHA_CHECK_FAILED: "auth/recaptcha-check-failed";
1831+
readonly RECAPTCHA_NOT_ENABLED: "auth/recaptcha-not-enabled";
17981832
}
17991833
```
18001834

docs-devsite/auth.recaptchaconfig.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Project: /docs/reference/js/_project.yaml
2+
Book: /docs/reference/_book.yaml
3+
page_type: reference
4+
5+
{% comment %}
6+
DO NOT EDIT THIS FILE!
7+
This is generated by the JS SDK team, and any local changes will be
8+
overwritten. Changes should be made in the source code at
9+
https://github.com/firebase/firebase-js-sdk
10+
{% endcomment %}
11+
12+
# RecaptchaConfig interface
13+
14+
<b>Signature:</b>
15+
16+
```typescript
17+
export interface RecaptchaConfig
18+
```
19+
20+
## Properties
21+
22+
| Property | Type | Description |
23+
| --- | --- | --- |
24+
| [emailPasswordEnabled](./auth.recaptchaconfig.md#recaptchaconfigemailpasswordenabled) | boolean | The reCAPTCHA enablement status of the [EmailAuthProvider](./auth.emailauthprovider.md#emailauthprovider_class) for the current tenant. |
25+
26+
## RecaptchaConfig.emailPasswordEnabled
27+
28+
The reCAPTCHA enablement status of the [EmailAuthProvider](./auth.emailauthprovider.md#emailauthprovider_class) for the current tenant.
29+
30+
<b>Signature:</b>
31+
32+
```typescript
33+
emailPasswordEnabled: boolean;
34+
```

packages/auth/demo/public/index.html

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,20 @@
229229
</button>
230230
</form>
231231

232-
<!-- Set Tenant -->
233-
<div class="group">Set Tenant</div>
232+
<!-- Recaptcha Configs -->
233+
<div class="group">Recaptcha Configs</div>
234234
<form class="form form-bordered no-submit">
235-
<input type="text" id="set-tenant"
236-
class="form-control"
237-
placeholder="Tenant" />
238-
<button class="btn btn-block btn-primary"
239-
id="set-tenant-btn">
240-
Set Tenant
235+
<input type="text" id="tenant-id" class="form-control"
236+
placeholder="Tenant ID" />
237+
<button class="btn btn-block btn-primary set-tenant-id"
238+
data-expired=false>
239+
Set Tenant ID
241240
</button>
242241
</form>
242+
<button class="btn btn-block btn-primary"
243+
id="initialize-recaptcha-config">
244+
Initialize reCAPTCHA Config
245+
</button>
243246

244247
<!-- Sign up -->
245248
<div class="group">Sign Up</div>

packages/auth/demo/src/index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ import {
7171
reauthenticateWithRedirect,
7272
getRedirectResult,
7373
browserPopupRedirectResolver,
74-
connectAuthEmulator
74+
connectAuthEmulator,
75+
initializeRecaptchaConfig
7576
} from '@firebase/auth';
7677

7778
import { config } from './config';
@@ -480,6 +481,18 @@ function onSignInAnonymously() {
480481
signInAnonymously(auth).then(onAuthUserCredentialSuccess, onAuthError);
481482
}
482483

484+
function onSetTenantID(_event) {
485+
const tenantId = $('#tenant-id').val();
486+
auth.tenantId = tenantId;
487+
if (tenantId === '') {
488+
auth.tenantId = null;
489+
}
490+
}
491+
492+
function onInitializeRecaptchaConfig() {
493+
initializeRecaptchaConfig(auth);
494+
}
495+
483496
/**
484497
* Signs in with a generic IdP credential.
485498
*/
@@ -2018,6 +2031,8 @@ function initApp() {
20182031
);
20192032
$('.sign-in-with-custom-token').click(onSignInWithCustomToken);
20202033
$('#sign-in-anonymously').click(onSignInAnonymously);
2034+
$('.set-tenant-id').click(onSetTenantID);
2035+
$('#initialize-recaptcha-config').click(onInitializeRecaptchaConfig);
20212036
$('#sign-in-with-generic-idp-credential').click(
20222037
onSignInWithGenericIdPCredential
20232038
);

packages/auth/src/api/authentication/email_and_password.test.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ import chaiAsPromised from 'chai-as-promised';
2121
import { ActionCodeOperation } from '../../model/public_types';
2222
import { FirebaseError } from '@firebase/util';
2323

24-
import { Endpoint, HttpHeader } from '../';
24+
import {
25+
Endpoint,
26+
HttpHeader,
27+
RecaptchaClientType,
28+
RecaptchaVersion
29+
} from '../';
2530
import { mockEndpoint } from '../../../test/helpers/api/helper';
2631
import { testAuth, TestAuth } from '../../../test/helpers/mock_auth';
2732
import * as mockFetch from '../../../test/helpers/mock_fetch';
@@ -44,7 +49,10 @@ describe('api/authentication/signInWithPassword', () => {
4449
const request = {
4550
returnSecureToken: true,
4651
47-
password: 'my-password'
52+
password: 'my-password',
53+
captchaResponse: 'recaptcha-token',
54+
clientType: RecaptchaClientType.WEB,
55+
recaptchaVersion: RecaptchaVersion.ENTERPRISE
4856
};
4957

5058
let auth: TestAuth;
@@ -187,7 +195,10 @@ describe('api/authentication/sendEmailVerification', () => {
187195
describe('api/authentication/sendPasswordResetEmail', () => {
188196
const request: PasswordResetRequest = {
189197
requestType: ActionCodeOperation.PASSWORD_RESET,
190-
198+
199+
captchaResp: 'recaptcha-token',
200+
clientType: RecaptchaClientType.WEB,
201+
recaptchaVersion: RecaptchaVersion.ENTERPRISE
191202
};
192203

193204
let auth: TestAuth;
@@ -245,7 +256,10 @@ describe('api/authentication/sendPasswordResetEmail', () => {
245256
describe('api/authentication/sendSignInLinkToEmail', () => {
246257
const request: EmailSignInRequest = {
247258
requestType: ActionCodeOperation.EMAIL_SIGNIN,
248-
259+
260+
captchaResp: 'recaptcha-token',
261+
clientType: RecaptchaClientType.WEB,
262+
recaptchaVersion: RecaptchaVersion.ENTERPRISE
249263
};
250264

251265
let auth: TestAuth;

packages/auth/src/api/authentication/email_and_password.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { ActionCodeOperation, Auth } from '../../model/public_types';
2020
import {
2121
Endpoint,
2222
HttpMethod,
23+
RecaptchaClientType,
24+
RecaptchaVersion,
2325
_addTidIfNecessary,
2426
_performApiRequest,
2527
_performSignInRequest
@@ -31,6 +33,9 @@ export interface SignInWithPasswordRequest {
3133
email: string;
3234
password: string;
3335
tenantId?: string;
36+
captchaResponse?: string;
37+
clientType?: RecaptchaClientType;
38+
recaptchaVersion?: RecaptchaVersion;
3439
}
3540

3641
export interface SignInWithPasswordResponse extends IdTokenResponse {
@@ -76,11 +81,16 @@ export interface PasswordResetRequest extends GetOobCodeRequest {
7681
requestType: ActionCodeOperation.PASSWORD_RESET;
7782
email: string;
7883
captchaResp?: string;
84+
clientType?: RecaptchaClientType;
85+
recaptchaVersion?: RecaptchaVersion;
7986
}
8087

8188
export interface EmailSignInRequest extends GetOobCodeRequest {
8289
requestType: ActionCodeOperation.EMAIL_SIGNIN;
8390
email: string;
91+
captchaResp?: string;
92+
clientType?: RecaptchaClientType;
93+
recaptchaVersion?: RecaptchaVersion;
8494
}
8595

8696
export interface VerifyAndChangeEmailRequest extends GetOobCodeRequest {

0 commit comments

Comments
 (0)