Skip to content

Commit c39c189

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 7e90c9a commit c39c189

File tree

9 files changed

+194
-13
lines changed

9 files changed

+194
-13
lines changed

etc/firebase-admin.auth.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ export class PhoneMultiFactorInfo extends MultiFactorInfo {
337337
// @public
338338
export class ProjectConfig {
339339
readonly smsRegionConfig?: SmsRegionConfig;
340+
get recaptchaConfig(): RecaptchaConfig | undefined;
340341
toJSON(): object;
341342
}
342343

@@ -464,6 +465,7 @@ export interface UpdatePhoneMultiFactorInfoRequest extends BaseUpdateMultiFactor
464465
// @public
465466
export interface UpdateProjectConfigRequest {
466467
smsRegionConfig?: SmsRegionConfig;
468+
recaptchaConfig?: RecaptchaConfig;
467469
}
468470

469471
// @public

src/auth/auth-config.ts

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

16461646
/**
16471647
* The request interface for updating a reCAPTCHA Config.
1648+
* By enabling reCAPTCHA Enterprise Integration you are
1649+
* agreeing to reCAPTCHA Enterprise
1650+
* {@link https://cloud.google.com/terms/service-terms | Term of Service}.
16481651
*/
16491652
export interface RecaptchaConfig {
16501653
/**

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: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { AuthClientErrorCode, FirebaseAuthError } from '../utils/error';
1818
import {
1919
SmsRegionsAuthConfig,
2020
SmsRegionConfig,
21+
RecaptchaConfig,
22+
RecaptchaAuthConfig,
2123
} from './auth-config';
2224
import { deepCopy } from '../utils/deep-copy';
2325

@@ -29,6 +31,13 @@ export interface UpdateProjectConfigRequest {
2931
* The SMS configuration to update on the project.
3032
*/
3133
smsRegionConfig?: SmsRegionConfig;
34+
/**
35+
* The recaptcha configuration to update on the project.
36+
* By enabling reCAPTCHA Enterprise Integration you are
37+
* agreeing to reCAPTCHA Enterprise
38+
* {@link https://cloud.google.com/terms/service-terms | Term of Service}.
39+
*/
40+
recaptchaConfig?: RecaptchaConfig;
3241
}
3342

3443
/**
@@ -37,6 +46,7 @@ export interface UpdateProjectConfigRequest {
3746
*/
3847
export interface ProjectConfigServerResponse {
3948
smsRegionConfig?: SmsRegionConfig;
49+
recaptchaConfig?: RecaptchaConfig;
4050
}
4151

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

5061
/**
@@ -57,6 +68,13 @@ export class ProjectConfig {
5768
* This is based on the calling code of the destination phone number.
5869
*/
5970
public readonly smsRegionConfig?: SmsRegionConfig;
71+
/**
72+
* The recaptcha configuration to update on the project config.
73+
* By enabling reCAPTCHA Enterprise Integration you are
74+
* agreeing to reCAPTCHA Enterprise
75+
* {@link https://cloud.google.com/terms/service-terms | Term of Service}.
76+
*/
77+
private readonly recaptchaConfig_?: RecaptchaAuthConfig;
6078

6179
/**
6280
* Validates a project config options object. Throws an error on failure.
@@ -72,6 +90,7 @@ export class ProjectConfig {
7290
}
7391
const validKeys = {
7492
smsRegionConfig: true,
93+
recaptchaConfig: true,
7594
}
7695
// Check for unsupported top level attributes.
7796
for (const key in request) {
@@ -86,20 +105,31 @@ export class ProjectConfig {
86105
if (typeof request.smsRegionConfig !== 'undefined') {
87106
SmsRegionsAuthConfig.validate(request.smsRegionConfig);
88107
}
108+
109+
// Validate reCAPTCHA config attribute.
110+
if (typeof request.recaptchaConfig !== 'undefined') {
111+
RecaptchaAuthConfig.validate(request.recaptchaConfig);
112+
}
89113
}
90114

91115
/**
92116
* Build the corresponding server request for a UpdateProjectConfigRequest object.
93117
* @param configOptions - The properties to convert to a server request.
94118
* @returns The equivalent server request.
95-
*
119+
*
96120
* @internal
97121
*/
98122
public static buildServerRequest(configOptions: UpdateProjectConfigRequest): ProjectConfigClientRequest {
99-
ProjectConfig.validate(configOptions);
123+
ProjectConfig.validate(configOptions);
100124
return configOptions as ProjectConfigClientRequest;
101125
}
102-
126+
127+
/**
128+
* The recaptcha configuration.
129+
*/
130+
get recaptchaConfig(): RecaptchaConfig | undefined {
131+
return this.recaptchaConfig_;
132+
}
103133
/**
104134
* The Project Config object constructor.
105135
*
@@ -111,6 +141,9 @@ export class ProjectConfig {
111141
if (typeof response.smsRegionConfig !== 'undefined') {
112142
this.smsRegionConfig = response.smsRegionConfig;
113143
}
144+
if (typeof response.recaptchaConfig !== 'undefined') {
145+
this.recaptchaConfig_ = new RecaptchaAuthConfig(response.recaptchaConfig);
146+
}
114147
}
115148
/**
116149
* Returns a JSON-serializable representation of this object.
@@ -121,10 +154,14 @@ export class ProjectConfig {
121154
// JSON serialization
122155
const json = {
123156
smsRegionConfig: deepCopy(this.smsRegionConfig),
157+
recaptchaConfig: this.recaptchaConfig_?.toJSON(),
124158
};
125159
if (typeof json.smsRegionConfig === 'undefined') {
126160
delete json.smsRegionConfig;
127161
}
162+
if (typeof json.recaptchaConfig === 'undefined') {
163+
delete json.recaptchaConfig;
164+
}
128165
return json;
129166
}
130167
}

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)