@@ -25,7 +25,7 @@ import {
25
25
import { CreateRequest , UpdateRequest } from './user-record' ;
26
26
import {
27
27
UserImportBuilder , UserImportOptions , UserImportRecord ,
28
- UserImportResult ,
28
+ UserImportResult , AuthFactorInfo ,
29
29
} from './user-import-builder' ;
30
30
import * as utils from '../utils/index' ;
31
31
import { ActionCodeSettings , ActionCodeSettingsBuilder } from './action-code-settings-builder' ;
@@ -151,6 +151,66 @@ class TenantAwareAuthResourceUrlBuilder extends AuthResourceUrlBuilder {
151
151
}
152
152
153
153
154
+ /**
155
+ * Validates an AuthFactorInfo object. All unsupported parameters
156
+ * are removed from the original request. If an invalid field is passed
157
+ * an error is thrown.
158
+ *
159
+ * @param request The AuthFactorInfo request object.
160
+ */
161
+ function validateAuthFactorInfo ( request : AuthFactorInfo ) {
162
+ const validKeys = {
163
+ mfaEnrollmentId : true ,
164
+ displayName : true ,
165
+ phoneInfo : true ,
166
+ enrolledAt : true ,
167
+ } ;
168
+ // Remove unsupported keys from the original request.
169
+ for ( const key in request ) {
170
+ if ( ! ( key in validKeys ) ) {
171
+ delete request [ key ] ;
172
+ }
173
+ }
174
+ if ( ! validator . isNonEmptyString ( request . mfaEnrollmentId ) ) {
175
+ throw new FirebaseAuthError (
176
+ AuthClientErrorCode . INVALID_UID ,
177
+ `The second factor "uid" must be a valid non-empty string.` ,
178
+ ) ;
179
+ }
180
+ if ( typeof request . displayName !== 'undefined' &&
181
+ ! validator . isString ( request . displayName ) ) {
182
+ throw new FirebaseAuthError (
183
+ AuthClientErrorCode . INVALID_DISPLAY_NAME ,
184
+ `The second factor "displayName" for "${ request . mfaEnrollmentId } " must be a valid string.` ,
185
+ ) ;
186
+ }
187
+ // enrolledAt must be a valid UTC date string.
188
+ if ( typeof request . enrolledAt !== 'undefined' &&
189
+ ! validator . isISODateString ( request . enrolledAt ) ) {
190
+ throw new FirebaseAuthError (
191
+ AuthClientErrorCode . INVALID_ENROLLMENT_TIME ,
192
+ `The second factor "enrollmentTime" for "${ request . mfaEnrollmentId } " must be a valid ` +
193
+ `UTC date string.` ) ;
194
+ }
195
+ // Validate required fields depending on second factor type.
196
+ if ( typeof request . phoneInfo !== 'undefined' ) {
197
+ // phoneNumber should be a string and a valid phone number.
198
+ if ( ! validator . isPhoneNumber ( request . phoneInfo ) ) {
199
+ throw new FirebaseAuthError (
200
+ AuthClientErrorCode . INVALID_PHONE_NUMBER ,
201
+ `The second factor "phoneNumber" for "${ request . mfaEnrollmentId } " must be a non-empty ` +
202
+ `E.164 standard compliant identifier string.` ) ;
203
+ }
204
+ } else {
205
+ // Invalid second factor. For example, a phone second factor may have been provided without
206
+ // a phone number. A TOTP based second factor may require a secret key, etc.
207
+ throw new FirebaseAuthError (
208
+ AuthClientErrorCode . INVALID_ENROLLED_FACTORS ,
209
+ `MFAInfo object provided is invalid.` ) ;
210
+ }
211
+ }
212
+
213
+
154
214
/**
155
215
* Validates a providerUserInfo object. All unsupported parameters
156
216
* are removed from the original request. If an invalid field is passed
@@ -243,6 +303,7 @@ function validateCreateEditRequest(request: any, uploadAccountRequest: boolean =
243
303
createdAt : uploadAccountRequest ,
244
304
lastLoginAt : uploadAccountRequest ,
245
305
providerUserInfo : uploadAccountRequest ,
306
+ mfaInfo : uploadAccountRequest ,
246
307
} ;
247
308
// Remove invalid keys from original request.
248
309
for ( const key in request ) {
@@ -381,6 +442,15 @@ function validateCreateEditRequest(request: any, uploadAccountRequest: boolean =
381
442
validateProviderUserInfo ( providerUserInfoEntry ) ;
382
443
} ) ;
383
444
}
445
+ // mfaInfo has to be an array of valid AuthFactorInfo requests.
446
+ if ( request . mfaInfo ) {
447
+ if ( ! validator . isArray ( request . mfaInfo ) ) {
448
+ throw new FirebaseAuthError ( AuthClientErrorCode . INVALID_ENROLLED_FACTORS ) ;
449
+ }
450
+ request . mfaInfo . forEach ( ( authFactorInfoEntry : AuthFactorInfo ) => {
451
+ validateAuthFactorInfo ( authFactorInfoEntry ) ;
452
+ } ) ;
453
+ }
384
454
}
385
455
386
456
0 commit comments