Skip to content

Commit 21c7deb

Browse files
committed
Project config - Recaptcha config (#1595)
* Recaptcha config changes in project config. - Implemented getProjectConfig. - Implemented updateProjectConfig. - Updated error code. - Add Term of Service consents.
1 parent ad904c4 commit 21c7deb

9 files changed

+200
-13
lines changed

etc/firebase-admin.auth.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ export class PhoneMultiFactorInfo extends MultiFactorInfo {
345345
export class ProjectConfig {
346346
get multiFactorConfig(): MultiFactorConfig | undefined;
347347
readonly smsRegionConfig?: SmsRegionConfig;
348+
get recaptchaConfig(): RecaptchaConfig | undefined;
348349
toJSON(): object;
349350
}
350351

@@ -478,6 +479,7 @@ export interface UpdatePhoneMultiFactorInfoRequest extends BaseUpdateMultiFactor
478479
export interface UpdateProjectConfigRequest {
479480
multiFactorConfig?: MultiFactorConfig;
480481
smsRegionConfig?: SmsRegionConfig;
482+
recaptchaConfig?: RecaptchaConfig;
481483
}
482484

483485
// @public

src/auth/auth-config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,9 @@ export interface RecaptchaKey {
17731773

17741774
/**
17751775
* The request interface for updating a reCAPTCHA Config.
1776+
* By enabling reCAPTCHA Enterprise Integration you are
1777+
* agreeing to reCAPTCHA Enterprise
1778+
* {@link https://cloud.google.com/terms/service-terms | Term of Service}.
17761779
*/
17771780
export interface RecaptchaConfig {
17781781
/**

src/auth/project-config-manager.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {
2727
*/
2828
export class ProjectConfigManager {
2929
private readonly authRequestHandler: AuthRequestHandler;
30-
3130
/**
3231
* Initializes a ProjectConfigManager instance for a specified FirebaseApp.
3332
*

src/auth/project-config.ts

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
MultiFactorConfig,
2222
MultiFactorAuthConfig,
2323
MultiFactorAuthServerConfig,
24+
RecaptchaConfig,
25+
RecaptchaAuthConfig,
2426
} from './auth-config';
2527
import { deepCopy } from '../utils/deep-copy';
2628

@@ -36,6 +38,14 @@ export interface UpdateProjectConfigRequest {
3638
* The multi-factor auth configuration to update on the project.
3739
*/
3840
multiFactorConfig?: MultiFactorConfig;
41+
42+
/**
43+
* The recaptcha configuration to update on the project.
44+
* By enabling reCAPTCHA Enterprise Integration you are
45+
* agreeing to reCAPTCHA Enterprise
46+
* {@link https://cloud.google.com/terms/service-terms | Term of Service}.
47+
*/
48+
recaptchaConfig?: RecaptchaConfig;
3949
}
4050

4151
/**
@@ -45,6 +55,7 @@ export interface UpdateProjectConfigRequest {
4555
export interface ProjectConfigServerResponse {
4656
smsRegionConfig?: SmsRegionConfig;
4757
mfa?: MultiFactorAuthServerConfig;
58+
recaptchaConfig?: RecaptchaConfig;
4859
}
4960

5061
/**
@@ -54,6 +65,7 @@ export interface ProjectConfigServerResponse {
5465
export interface ProjectConfigClientRequest {
5566
smsRegionConfig?: SmsRegionConfig;
5667
mfa?: MultiFactorAuthServerConfig;
68+
recaptchaConfig?: RecaptchaConfig;
5769
}
5870

5971
/**
@@ -66,10 +78,21 @@ export class ProjectConfig {
6678
* This is based on the calling code of the destination phone number.
6779
*/
6880
public readonly smsRegionConfig?: SmsRegionConfig;
81+
6982
/**
7083
* The project's multi-factor auth configuration.
7184
* Supports only phone and TOTP.
72-
*/ private readonly multiFactorConfig_?: MultiFactorConfig;
85+
*/
86+
private readonly multiFactorConfig_?: MultiFactorConfig;
87+
88+
/**
89+
* The recaptcha configuration to update on the project config.
90+
* By enabling reCAPTCHA Enterprise Integration you are
91+
* agreeing to reCAPTCHA Enterprise
92+
* {@link https://cloud.google.com/terms/service-terms | Term of Service}.
93+
*/
94+
private readonly recaptchaConfig_?: RecaptchaAuthConfig;
95+
7396
/**
7497
* The multi-factor auth configuration.
7598
*/
@@ -92,6 +115,7 @@ export class ProjectConfig {
92115
const validKeys = {
93116
smsRegionConfig: true,
94117
multiFactorConfig: true,
118+
recaptchaConfig: true,
95119
}
96120
// Check for unsupported top level attributes.
97121
for (const key in request) {
@@ -111,13 +135,17 @@ export class ProjectConfig {
111135
if (typeof request.multiFactorConfig !== 'undefined') {
112136
MultiFactorAuthConfig.validate(request.multiFactorConfig);
113137
}
138+
// Validate reCAPTCHA config attribute.
139+
if (typeof request.recaptchaConfig !== 'undefined') {
140+
RecaptchaAuthConfig.validate(request.recaptchaConfig);
141+
}
114142
}
115143

116144
/**
117145
* Build the corresponding server request for a UpdateProjectConfigRequest object.
118146
* @param configOptions - The properties to convert to a server request.
119147
* @returns The equivalent server request.
120-
*
148+
*
121149
* @internal
122150
*/
123151
public static buildServerRequest(configOptions: UpdateProjectConfigRequest): ProjectConfigClientRequest {
@@ -133,7 +161,13 @@ export class ProjectConfig {
133161
delete request.multiFactorConfig;
134162
return request as ProjectConfigClientRequest;
135163
}
136-
164+
165+
/**
166+
* The recaptcha configuration.
167+
*/
168+
get recaptchaConfig(): RecaptchaConfig | undefined {
169+
return this.recaptchaConfig_;
170+
}
137171
/**
138172
* The Project Config object constructor.
139173
*
@@ -150,6 +184,9 @@ export class ProjectConfig {
150184
if (typeof response.mfa !== 'undefined') {
151185
this.multiFactorConfig_ = new MultiFactorAuthConfig(response.mfa);
152186
}
187+
if (typeof response.recaptchaConfig !== 'undefined') {
188+
this.recaptchaConfig_ = new RecaptchaAuthConfig(response.recaptchaConfig);
189+
}
153190
}
154191
/**
155192
* Returns a JSON-serializable representation of this object.
@@ -161,13 +198,17 @@ export class ProjectConfig {
161198
const json = {
162199
smsRegionConfig: deepCopy(this.smsRegionConfig),
163200
multiFactorConfig: deepCopy(this.multiFactorConfig),
201+
recaptchaConfig: this.recaptchaConfig_?.toJSON(),
164202
};
165203
if (typeof json.smsRegionConfig === 'undefined') {
166204
delete json.smsRegionConfig;
167205
}
168206
if (typeof json.multiFactorConfig === 'undefined') {
169207
delete json.multiFactorConfig;
170208
}
209+
if (typeof json.recaptchaConfig === 'undefined') {
210+
delete json.recaptchaConfig;
211+
}
171212
return json;
172213
}
173214
}

src/auth/tenant.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ export interface UpdateTenantRequest {
6262

6363
/**
6464
* The recaptcha configuration to update on the tenant.
65+
* By enabling reCAPTCHA Enterprise Integration you are
66+
* agreeing to reCAPTCHA Enterprise
67+
* {@link https://cloud.google.com/terms/service-terms | Term of Service}.
6568
*/
6669
recaptchaConfig?: RecaptchaConfig;
6770
}
@@ -137,9 +140,12 @@ export class Tenant {
137140
private readonly emailSignInConfig_?: EmailSignInConfig;
138141
private readonly multiFactorConfig_?: MultiFactorAuthConfig;
139142

140-
/*
141-
* The map conatining the reCAPTCHA config.
142-
*/
143+
/**
144+
* The map conatining the reCAPTCHA config.
145+
* By enabling reCAPTCHA Enterprise Integration you are
146+
* agreeing to reCAPTCHA Enterprise
147+
* {@link https://cloud.google.com/terms/service-terms | Term of Service}.
148+
*/
143149
private readonly recaptchaConfig_?: RecaptchaAuthConfig;
144150
/**
145151
* The SMS Regions Config to update a tenant.

src/utils/error.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,14 @@ export class AuthClientErrorCode {
737737
code: 'user-not-disabled',
738738
message: 'The user must be disabled in order to bulk delete it (or you must pass force=true).',
739739
};
740+
public static INVALID_RECAPTCHA_ACTION = {
741+
code: 'invalid-recaptcha-action',
742+
message: 'reCAPTCHA action must be "BLOCK".'
743+
}
744+
public static INVALID_RECAPTCHA_ENFORCEMENT_STATE = {
745+
code: 'invalid-recaptcha-enforcement-state',
746+
message: 'reCAPTCHA enforcement state must be either "OFF", "AUDIT" or "ENFORCE".'
747+
}
740748
}
741749

742750
/**
@@ -996,6 +1004,10 @@ const AUTH_SERVER_TO_CLIENT_CODE: ServerToClientCode = {
9961004
USER_DISABLED: 'USER_DISABLED',
9971005
// Password provided is too weak.
9981006
WEAK_PASSWORD: 'INVALID_PASSWORD',
1007+
// Unrecognized reCAPTCHA action.
1008+
INVALID_RECAPTCHA_ACTION: 'INVALID_RECAPTCHA_ACTION',
1009+
// Unrecognized reCAPTCHA enforcement state.
1010+
INVALID_RECAPTCHA_ENFORCEMENT_STATE: 'INVALID_RECAPTCHA_ENFORCEMENT_STATE',
9991011
};
10001012

10011013
/** @const {ServerToClientCode} Messaging server to client enum error codes. */

test/unit/auth/project-config-manager.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ describe('ProjectConfigManager', () => {
5151
allowedRegions: [ 'AC', 'AD' ],
5252
},
5353
},
54+
recaptchaConfig: {
55+
emailPasswordEnforcementState: 'AUDIT',
56+
managedRules: [ {
57+
endScore: 0.2,
58+
action: 'BLOCK'
59+
} ],
60+
recaptchaKeys: [ {
61+
type: 'WEB',
62+
key: 'test-key-1' }
63+
],
64+
}
5465
};
5566

5667
before(() => {
@@ -131,6 +142,13 @@ describe('ProjectConfigManager', () => {
131142
disallowedRegions: [ 'AC', 'AD' ],
132143
},
133144
},
145+
recaptchaConfig: {
146+
emailPasswordEnforcementState: 'AUDIT',
147+
managedRules: [ {
148+
endScore: 0.2,
149+
action: 'BLOCK'
150+
} ],
151+
}
134152
};
135153
const expectedProjectConfig = new ProjectConfig(GET_CONFIG_RESPONSE);
136154
const expectedError = new FirebaseAuthError(
@@ -193,4 +211,4 @@ describe('ProjectConfigManager', () => {
193211
});
194212
});
195213
});
196-
});
214+
});

0 commit comments

Comments
 (0)