Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(auth): Better type heirarchies for Auth API
  • Loading branch information
hiranya911 committed May 24, 2021
commit 4d09b827fac4cb929ae46bf89c5995e3c25e6fca
13 changes: 9 additions & 4 deletions src/auth/auth-api-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1480,7 +1480,12 @@ export abstract class AbstractAuthRequestHandler {
}

// Build the signupNewUser request.
const request: any = deepCopy(properties);
type SignUpNewUserRequest = CreateRequest & {
photoUrl?: string | null;
localId?: string;
mfaInfo?: AuthFactorInfo[];
};
const request: SignUpNewUserRequest = deepCopy(properties);
// Rewrite photoURL to photoUrl.
if (typeof request.photoURL !== 'undefined') {
request.photoUrl = request.photoURL;
Expand All @@ -1496,14 +1501,14 @@ export abstract class AbstractAuthRequestHandler {
if (validator.isNonEmptyArray(request.multiFactor.enrolledFactors)) {
const mfaInfo: AuthFactorInfo[] = [];
try {
request.multiFactor.enrolledFactors.forEach((multiFactorInfo: any) => {
request.multiFactor.enrolledFactors.forEach((multiFactorInfo) => {
// Enrollment time and uid are not allowed for signupNewUser endpoint.
// They will automatically be provisioned server side.
if (multiFactorInfo.enrollmentTime) {
if ('enrollmentTime' in multiFactorInfo) {
throw new FirebaseAuthError(
AuthClientErrorCode.INVALID_ARGUMENT,
'"enrollmentTime" is not supported when adding second factors via "createUser()"');
} else if (multiFactorInfo.uid) {
} else if ('uid' in multiFactorInfo) {
throw new FirebaseAuthError(
AuthClientErrorCode.INVALID_ARGUMENT,
'"uid" is not supported when adding second factors via "createUser()"');
Expand Down
52 changes: 35 additions & 17 deletions src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ export namespace auth {
* Interface representing common properties of a user enrolled second factor
* for an `UpdateRequest`.
*/
export interface UpdateMultiFactorInfoRequest {
export interface BaseUpdateMultiFactorInfoRequest {

/**
* The ID of the enrolled second factor. This ID is unique to the user. When not provided,
Expand All @@ -356,25 +356,31 @@ export namespace auth {
* The optional date the second factor was enrolled, formatted as a UTC string.
*/
enrollmentTime?: string;

/**
* The type identifier of the second factor. For SMS second factors, this is `phone`.
*/
factorId: string;
}

/**
* Interface representing a phone specific user enrolled second factor
* for an `UpdateRequest`.
*/
export interface UpdatePhoneMultiFactorInfoRequest extends UpdateMultiFactorInfoRequest {
export interface UpdatePhoneMultiFactorInfoRequest extends BaseUpdateMultiFactorInfoRequest {

/**
* The type identifier of the second factor. For SMS second factors, this is `phone`.
*/
factorId: 'phone';

/**
* The phone number associated with a phone second factor.
*/
phoneNumber: string;
}

/**
* Type representing the properties of a user enrolled second factor
* for an `UpdateRequest`.
*/
export type UpdateMultiFactorInfoRequest = | UpdatePhoneMultiFactorInfoRequest;

/**
* Interface representing the properties to update on the provided user.
*/
Expand Down Expand Up @@ -446,31 +452,37 @@ export namespace auth {
* Interface representing base properties of a user enrolled second factor for a
* `CreateRequest`.
*/
export interface CreateMultiFactorInfoRequest {
export interface BaseCreateMultiFactorInfoRequest {

/**
* The optional display name for an enrolled second factor.
*/
displayName?: string;

/**
* The type identifier of the second factor. For SMS second factors, this is `phone`.
*/
factorId: string;
}

/**
* Interface representing a phone specific user enrolled second factor for a
* `CreateRequest`.
*/
export interface CreatePhoneMultiFactorInfoRequest extends CreateMultiFactorInfoRequest {
export interface CreatePhoneMultiFactorInfoRequest extends BaseCreateMultiFactorInfoRequest {

/**
* The type identifier of the second factor. For SMS second factors, this is `phone`.
*/
factorId: 'phone';

/**
* The phone number associated with a phone second factor.
*/
phoneNumber: string;
}

/**
* Type representing the properties of a user enrolled second factor
* for an `CreateRequest`.
*/
export type CreateMultiFactorInfoRequest = | CreatePhoneMultiFactorInfoRequest;

/**
* Interface representing the properties to set on a new user record to be
* created.
Expand Down Expand Up @@ -1221,7 +1233,7 @@ export namespace auth {
/**
* The base Auth provider configuration interface.
*/
export interface AuthProviderConfig {
export interface BaseAuthProviderConfig {

/**
* The provider ID defined by the developer.
Expand Down Expand Up @@ -1249,7 +1261,7 @@ export namespace auth {
* Auth provider configuration interface. A SAML provider can be created via
* {@link auth.Auth.createProviderConfig `createProviderConfig()`}.
*/
export interface SAMLAuthProviderConfig extends AuthProviderConfig {
export interface SAMLAuthProviderConfig extends BaseAuthProviderConfig {

/**
* The SAML IdP entity identifier.
Expand Down Expand Up @@ -1294,7 +1306,7 @@ export namespace auth {
* provider configuration interface. An OIDC provider can be created via
* {@link auth.Auth.createProviderConfig `createProviderConfig()`}.
*/
export interface OIDCAuthProviderConfig extends AuthProviderConfig {
export interface OIDCAuthProviderConfig extends BaseAuthProviderConfig {

/**
* This is the required client ID used to confirm the audience of an OIDC
Expand Down Expand Up @@ -1323,6 +1335,12 @@ export namespace auth {
issuer: string;
}

/**
* The Auth provider configuration type.
* {@link auth.Auth.createProviderConfig `createProviderConfig()`}.
*/
export type AuthProviderConfig = SAMLAuthProviderConfig | OIDCAuthProviderConfig;

/**
* The request interface for updating a SAML Auth provider. This is used
* when updating a SAML provider's configuration via
Expand Down
4 changes: 2 additions & 2 deletions test/integration/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,14 +638,14 @@ describe('admin.auth', () => {
uid: 'mfaUid1',
phoneNumber: '+16505550001',
displayName: 'Work phone number',
factorId: 'phone',
factorId: 'phone' as const,
enrollmentTime: now,
},
{
uid: 'mfaUid2',
phoneNumber: '+16505550002',
displayName: 'Personal phone number',
factorId: 'phone',
factorId: 'phone' as const,
enrollmentTime: now,
},
];
Expand Down
12 changes: 3 additions & 9 deletions test/unit/auth/auth-api-request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1422,15 +1422,9 @@ AUTH_REQUEST_HANDLER_TESTS.forEach((handler) => {
uid: 'mfaUid1',
phoneNumber: '+16505550001',
displayName: 'Corp phone number',
factorId: 'phone',
factorId: 'phone' as const,
enrollmentTime: new Date().toUTCString(),
},
{
uid: 'mfaUid2',
phoneNumber: '+16505550002',
displayName: 'Personal phone number',
factorId: 'phone',
},
],
},
customClaims: { admin: true },
Expand Down Expand Up @@ -2659,11 +2653,11 @@ AUTH_REQUEST_HANDLER_TESTS.forEach((handler) => {
{
phoneNumber: '+16505557348',
displayName: 'Spouse\'s phone number',
factorId: 'phone',
factorId: 'phone' as const,
},
{
phoneNumber: '+16505551000',
factorId: 'phone',
factorId: 'phone' as const,
},
],
},
Expand Down
4 changes: 2 additions & 2 deletions test/unit/auth/user-import-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ describe('UserImportBuilder', () => {
uid: 'enrolledSecondFactor1',
phoneNumber: '+16505557348',
displayName: 'Spouse\'s phone number',
factorId: 'phone',
factorId: 'phone' as const,
enrollmentTime: now.toUTCString(),
},
{
uid: 'enrolledSecondFactor2',
phoneNumber: '+16505551000',
factorId: 'phone',
factorId: 'phone' as const,
},
],
},
Expand Down