Skip to content

Commit 29271ad

Browse files
authored
fix(auth): Better type hierarchies for Auth API (#1294)
* fix(auth): Better type heirarchies for Auth API * fix: Moved factorId back to the base types * fix: Updated API report * fix: Fixed a grammar error in comment * fix: Update to comment text
1 parent f4a9a16 commit 29271ad

File tree

4 files changed

+62
-42
lines changed

4 files changed

+62
-42
lines changed

etc/firebase-admin.api.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,7 @@ export namespace auth {
113113
tenantManager(): TenantManager;
114114
}
115115
export type AuthFactorType = 'phone';
116-
export interface AuthProviderConfig {
117-
displayName?: string;
118-
enabled: boolean;
119-
providerId: string;
120-
}
116+
export type AuthProviderConfig = SAMLAuthProviderConfig | OIDCAuthProviderConfig;
121117
export interface AuthProviderConfigFilter {
122118
maxResults?: number;
123119
pageToken?: string;
@@ -151,11 +147,23 @@ export namespace auth {
151147
verifyIdToken(idToken: string, checkRevoked?: boolean): Promise<DecodedIdToken>;
152148
verifySessionCookie(sessionCookie: string, checkForRevocation?: boolean): Promise<DecodedIdToken>;
153149
}
154-
export interface CreateMultiFactorInfoRequest {
150+
export interface BaseAuthProviderConfig {
151+
displayName?: string;
152+
enabled: boolean;
153+
providerId: string;
154+
}
155+
export interface BaseCreateMultiFactorInfoRequest {
156+
displayName?: string;
157+
factorId: string;
158+
}
159+
export interface BaseUpdateMultiFactorInfoRequest {
155160
displayName?: string;
161+
enrollmentTime?: string;
156162
factorId: string;
163+
uid?: string;
157164
}
158-
export interface CreatePhoneMultiFactorInfoRequest extends CreateMultiFactorInfoRequest {
165+
export type CreateMultiFactorInfoRequest = CreatePhoneMultiFactorInfoRequest;
166+
export interface CreatePhoneMultiFactorInfoRequest extends BaseCreateMultiFactorInfoRequest {
159167
phoneNumber: string;
160168
}
161169
export interface CreateRequest extends UpdateRequest {
@@ -245,7 +253,7 @@ export namespace auth {
245253
code?: boolean;
246254
idToken?: boolean;
247255
}
248-
export interface OIDCAuthProviderConfig extends AuthProviderConfig {
256+
export interface OIDCAuthProviderConfig extends BaseAuthProviderConfig {
249257
clientId: string;
250258
clientSecret?: string;
251259
issuer: string;
@@ -272,7 +280,7 @@ export namespace auth {
272280
// (undocumented)
273281
providerUid: string;
274282
}
275-
export interface SAMLAuthProviderConfig extends AuthProviderConfig {
283+
export interface SAMLAuthProviderConfig extends BaseAuthProviderConfig {
276284
callbackURL?: string;
277285
idpEntityId: string;
278286
rpEntityId: string;
@@ -323,13 +331,8 @@ export namespace auth {
323331
}
324332
// (undocumented)
325333
export type UpdateAuthProviderRequest = SAMLUpdateAuthProviderRequest | OIDCUpdateAuthProviderRequest;
326-
export interface UpdateMultiFactorInfoRequest {
327-
displayName?: string;
328-
enrollmentTime?: string;
329-
factorId: string;
330-
uid?: string;
331-
}
332-
export interface UpdatePhoneMultiFactorInfoRequest extends UpdateMultiFactorInfoRequest {
334+
export type UpdateMultiFactorInfoRequest = UpdatePhoneMultiFactorInfoRequest;
335+
export interface UpdatePhoneMultiFactorInfoRequest extends BaseUpdateMultiFactorInfoRequest {
333336
phoneNumber: string;
334337
}
335338
export interface UpdateRequest {

src/auth/auth-api-request.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,12 @@ export abstract class AbstractAuthRequestHandler {
14801480
}
14811481

14821482
// Build the signupNewUser request.
1483-
const request: any = deepCopy(properties);
1483+
type SignUpNewUserRequest = CreateRequest & {
1484+
photoUrl?: string | null;
1485+
localId?: string;
1486+
mfaInfo?: AuthFactorInfo[];
1487+
};
1488+
const request: SignUpNewUserRequest = deepCopy(properties);
14841489
// Rewrite photoURL to photoUrl.
14851490
if (typeof request.photoURL !== 'undefined') {
14861491
request.photoUrl = request.photoURL;
@@ -1496,14 +1501,14 @@ export abstract class AbstractAuthRequestHandler {
14961501
if (validator.isNonEmptyArray(request.multiFactor.enrolledFactors)) {
14971502
const mfaInfo: AuthFactorInfo[] = [];
14981503
try {
1499-
request.multiFactor.enrolledFactors.forEach((multiFactorInfo: any) => {
1504+
request.multiFactor.enrolledFactors.forEach((multiFactorInfo) => {
15001505
// Enrollment time and uid are not allowed for signupNewUser endpoint.
15011506
// They will automatically be provisioned server side.
1502-
if (multiFactorInfo.enrollmentTime) {
1507+
if ('enrollmentTime' in multiFactorInfo) {
15031508
throw new FirebaseAuthError(
15041509
AuthClientErrorCode.INVALID_ARGUMENT,
15051510
'"enrollmentTime" is not supported when adding second factors via "createUser()"');
1506-
} else if (multiFactorInfo.uid) {
1511+
} else if ('uid' in multiFactorInfo) {
15071512
throw new FirebaseAuthError(
15081513
AuthClientErrorCode.INVALID_ARGUMENT,
15091514
'"uid" is not supported when adding second factors via "createUser()"');

src/auth/index.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export namespace auth {
112112
}
113113

114114
/**
115-
* Interface representing the common properties of a user enrolled second factor.
115+
* Interface representing the common properties of a user-enrolled second factor.
116116
*/
117117
export interface MultiFactorInfo {
118118

@@ -143,7 +143,7 @@ export namespace auth {
143143
}
144144

145145
/**
146-
* Interface representing a phone specific user enrolled second factor.
146+
* Interface representing a phone specific user-enrolled second factor.
147147
*/
148148
export interface PhoneMultiFactorInfo extends MultiFactorInfo {
149149

@@ -336,10 +336,10 @@ export namespace auth {
336336
}
337337

338338
/**
339-
* Interface representing common properties of a user enrolled second factor
339+
* Interface representing common properties of a user-enrolled second factor
340340
* for an `UpdateRequest`.
341341
*/
342-
export interface UpdateMultiFactorInfoRequest {
342+
export interface BaseUpdateMultiFactorInfoRequest {
343343

344344
/**
345345
* The ID of the enrolled second factor. This ID is unique to the user. When not provided,
@@ -364,17 +364,23 @@ export namespace auth {
364364
}
365365

366366
/**
367-
* Interface representing a phone specific user enrolled second factor
367+
* Interface representing a phone specific user-enrolled second factor
368368
* for an `UpdateRequest`.
369369
*/
370-
export interface UpdatePhoneMultiFactorInfoRequest extends UpdateMultiFactorInfoRequest {
370+
export interface UpdatePhoneMultiFactorInfoRequest extends BaseUpdateMultiFactorInfoRequest {
371371

372372
/**
373373
* The phone number associated with a phone second factor.
374374
*/
375375
phoneNumber: string;
376376
}
377377

378+
/**
379+
* Type representing the properties of a user-enrolled second factor
380+
* for an `UpdateRequest`.
381+
*/
382+
export type UpdateMultiFactorInfoRequest = | UpdatePhoneMultiFactorInfoRequest;
383+
378384
/**
379385
* Interface representing the properties to update on the provided user.
380386
*/
@@ -443,10 +449,10 @@ export namespace auth {
443449
}
444450

445451
/**
446-
* Interface representing base properties of a user enrolled second factor for a
452+
* Interface representing base properties of a user-enrolled second factor for a
447453
* `CreateRequest`.
448454
*/
449-
export interface CreateMultiFactorInfoRequest {
455+
export interface BaseCreateMultiFactorInfoRequest {
450456

451457
/**
452458
* The optional display name for an enrolled second factor.
@@ -460,17 +466,23 @@ export namespace auth {
460466
}
461467

462468
/**
463-
* Interface representing a phone specific user enrolled second factor for a
469+
* Interface representing a phone specific user-enrolled second factor for a
464470
* `CreateRequest`.
465471
*/
466-
export interface CreatePhoneMultiFactorInfoRequest extends CreateMultiFactorInfoRequest {
472+
export interface CreatePhoneMultiFactorInfoRequest extends BaseCreateMultiFactorInfoRequest {
467473

468474
/**
469475
* The phone number associated with a phone second factor.
470476
*/
471477
phoneNumber: string;
472478
}
473479

480+
/**
481+
* Type representing the properties of a user-enrolled second factor
482+
* for a `CreateRequest`.
483+
*/
484+
export type CreateMultiFactorInfoRequest = | CreatePhoneMultiFactorInfoRequest;
485+
474486
/**
475487
* Interface representing the properties to set on a new user record to be
476488
* created.
@@ -1221,7 +1233,7 @@ export namespace auth {
12211233
/**
12221234
* The base Auth provider configuration interface.
12231235
*/
1224-
export interface AuthProviderConfig {
1236+
export interface BaseAuthProviderConfig {
12251237

12261238
/**
12271239
* The provider ID defined by the developer.
@@ -1249,7 +1261,7 @@ export namespace auth {
12491261
* Auth provider configuration interface. A SAML provider can be created via
12501262
* {@link auth.Auth.createProviderConfig `createProviderConfig()`}.
12511263
*/
1252-
export interface SAMLAuthProviderConfig extends AuthProviderConfig {
1264+
export interface SAMLAuthProviderConfig extends BaseAuthProviderConfig {
12531265

12541266
/**
12551267
* The SAML IdP entity identifier.
@@ -1301,7 +1313,7 @@ export namespace auth {
13011313
export interface OAuthResponseType {
13021314
/**
13031315
* Whether ID token is returned from IdP's authorization endpoint.
1304-
*/
1316+
*/
13051317
idToken?: boolean;
13061318

13071319
/**
@@ -1315,7 +1327,7 @@ export namespace auth {
13151327
* provider configuration interface. An OIDC provider can be created via
13161328
* {@link auth.Auth.createProviderConfig `createProviderConfig()`}.
13171329
*/
1318-
export interface OIDCAuthProviderConfig extends AuthProviderConfig {
1330+
export interface OIDCAuthProviderConfig extends BaseAuthProviderConfig {
13191331

13201332
/**
13211333
* This is the required client ID used to confirm the audience of an OIDC
@@ -1347,13 +1359,19 @@ export namespace auth {
13471359
* The OIDC provider's client secret to enable OIDC code flow.
13481360
*/
13491361
clientSecret?: string;
1350-
1362+
13511363
/**
13521364
* The OIDC provider's response object for OAuth authorization flow.
13531365
*/
13541366
responseType?: OAuthResponseType;
13551367
}
13561368

1369+
/**
1370+
* The Auth provider configuration type.
1371+
* {@link auth.Auth.createProviderConfig `createProviderConfig()`}.
1372+
*/
1373+
export type AuthProviderConfig = SAMLAuthProviderConfig | OIDCAuthProviderConfig;
1374+
13571375
/**
13581376
* The request interface for updating a SAML Auth provider. This is used
13591377
* when updating a SAML provider's configuration via
@@ -1440,7 +1458,7 @@ export namespace auth {
14401458
* If not provided, the existing configuration's value is not modified.
14411459
*/
14421460
clientSecret?: string;
1443-
1461+
14441462
/**
14451463
* The OIDC provider's response object for OAuth authorization flow.
14461464
*/

test/unit/auth/auth-api-request.spec.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,12 +1425,6 @@ AUTH_REQUEST_HANDLER_TESTS.forEach((handler) => {
14251425
factorId: 'phone',
14261426
enrollmentTime: new Date().toUTCString(),
14271427
},
1428-
{
1429-
uid: 'mfaUid2',
1430-
phoneNumber: '+16505550002',
1431-
displayName: 'Personal phone number',
1432-
factorId: 'phone',
1433-
},
14341428
],
14351429
},
14361430
customClaims: { admin: true },

0 commit comments

Comments
 (0)