Skip to content

sign-in flow for totp #6626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 13, 2022
4 changes: 4 additions & 0 deletions common/api-review/auth.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -765,18 +765,22 @@ export interface TotpMultiFactorInfo extends MultiFactorInfo {

// @public
export class TotpSecret {
// (undocumented)
readonly codeIntervalSeconds: number;
// (undocumented)
readonly codeLength: number;
// Warning: (ae-forgotten-export) The symbol "StartTotpMfaEnrollmentResponse" needs to be exported by the entry point index.d.ts
//
// @internal (undocumented)
static _fromStartTotpMfaEnrollmentResponse(response: StartTotpMfaEnrollmentResponse, auth: AuthInternal): TotpSecret;
generateQrCodeUrl(accountName?: string, issuer?: string): string;
// (undocumented)
readonly hashingAlgorithm: string;
// Warning: (ae-forgotten-export) The symbol "TotpVerificationInfo" needs to be exported by the entry point index.d.ts
//
// @internal (undocumented)
_makeTotpVerificationInfo(otp: string): TotpVerificationInfo;
// (undocumented)
readonly secretKey: string;
}

Expand Down
57 changes: 56 additions & 1 deletion packages/auth/src/api/authentication/mfa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from '../index';
import { Auth } from '../../model/public_types';
import { IdTokenResponse } from '../../model/id_token';
import { MfaEnrollment } from '../account_management/mfa';
import { MfaEnrollment, TotpVerificationInfo } from '../account_management/mfa';
import { SignInWithIdpResponse } from './idp';
import {
SignInWithPhoneNumberRequest,
Expand Down Expand Up @@ -51,13 +51,28 @@ export interface StartPhoneMfaSignInRequest {
};
tenantId?: string;
}
export interface StartTotpMfaSignInRequest {
mfaPendingCredential: string;
mfaEnrollmentId: string;
TotpSignInInfo: {
verificationCode: string;
};
tenantId?: string;
}


export interface StartPhoneMfaSignInResponse {
phoneResponseInfo: {
sessionInfo: string;
};
}

export interface StartTotpMfaSignInResponse {
TotpSignInInfo: {
verificationCode: string;
};
}

export function startSignInPhoneMfa(
auth: Auth,
request: StartPhoneMfaSignInRequest
Expand All @@ -73,14 +88,38 @@ export function startSignInPhoneMfa(
);
}

export function startSignInTotpMfa(
auth: Auth,
request: StartTotpMfaSignInRequest
): Promise<StartTotpMfaSignInResponse> {
return _performApiRequest<
StartTotpMfaSignInRequest,
StartTotpMfaSignInResponse
>(
auth,
HttpMethod.POST,
Endpoint.START_MFA_SIGN_IN,
_addTidIfNecessary(auth, request)
);
}

export interface FinalizePhoneMfaSignInRequest {
mfaPendingCredential: string;
phoneVerificationInfo: SignInWithPhoneNumberRequest;
tenantId?: string;
}

export interface FinalizeTotpMfaSignInRequest {
mfaPendingCredential: string;
verificationCode: string
tenantId?: string;
}

export interface FinalizePhoneMfaSignInResponse extends FinalizeMfaResponse {}

export interface FinalizeTotpMfaSignInResponse extends FinalizeMfaResponse {}


export function finalizeSignInPhoneMfa(
auth: Auth,
request: FinalizePhoneMfaSignInRequest
Expand All @@ -96,6 +135,22 @@ export function finalizeSignInPhoneMfa(
);
}

export function finalizeSignInTotpMfa(
auth: Auth,
request: FinalizeTotpMfaSignInRequest
): Promise<FinalizeTotpMfaSignInResponse> {
return _performApiRequest<
FinalizeTotpMfaSignInRequest,
FinalizeTotpMfaSignInResponse
>(
auth,
HttpMethod.POST,
Endpoint.FINALIZE_MFA_SIGN_IN,
_addTidIfNecessary(auth, request)
);
}


/**
* @internal
*/
Expand Down
17 changes: 13 additions & 4 deletions packages/auth/src/mfa/assertions/totp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
StartTotpMfaEnrollmentResponse,
TotpVerificationInfo
} from '../../api/account_management/mfa';
import { FinalizeMfaResponse } from '../../api/authentication/mfa';
import { FinalizeMfaResponse, finalizeSignInTotpMfa } from '../../api/authentication/mfa';
import { MultiFactorAssertionImpl } from '../../mfa/mfa_assertion';
import { MultiFactorSessionImpl } from '../mfa_session';
import { AuthErrorCode } from '../../core/errors';
Expand Down Expand Up @@ -155,10 +155,19 @@ export class TotpMultiFactorAssertionImpl

/** @internal */
_finalizeSignIn(
_auth: AuthInternal,
_mfaPendingCredential: string
auth: AuthInternal,
mfaPendingCredential: string
): Promise<FinalizeMfaResponse> {
throw new Error('method not implemented');
_assert(
typeof this.enrollmentId !== 'undefined'
&& typeof this.otp !== 'undefined',
auth,
AuthErrorCode.ARGUMENT_ERROR
);
return finalizeSignInTotpMfa(auth, {
mfaPendingCredential,
verificationCode: this.otp,
});
}
}

Expand Down