Skip to content

Commit e91db4a

Browse files
committed
Able to send otp to backend and get authState changed in demo app
1 parent 440b9af commit e91db4a

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

packages/auth/demo/public/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,17 @@ <h4 class="modal-title">Select a second factor to sign in with</h4>
755755
</button>
756756
</div>
757757
</form>
758+
<!-- For handling sign-in with TOTP 2nd factor. -->
759+
<form class="form form-bordered no-submit hidden" id="multi-factor-totp">
760+
<div class="form">
761+
<input type="text" id="multi-factor-totp-sign-in-verification-code"
762+
class="form-control" placeholder="Totp Verification code" />
763+
<button class="btn btn-block btn-primary"
764+
id="sign-in-with-totp-multi-factor">
765+
Complete sign In
766+
</button>
767+
</div>
768+
</form>
758769
</div>
759770
<div class="modal-footer">
760771
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>

packages/auth/demo/src/index.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,7 @@ function handleMultiFactorSignIn(resolver) {
10681068
);
10691069
// Hide phone form (other second factor types could be supported).
10701070
$('#multi-factor-phone').addClass('hidden');
1071+
$('#multi-factor-totp').addClass('hidden');
10711072
// Show second factor recovery dialog.
10721073
$('#multiFactorModal').modal();
10731074
}
@@ -1086,6 +1087,7 @@ function handleMultiFactorSignIn(resolver) {
10861087
function showMultiFactors($listGroup, multiFactorInfo, onClick, onDelete) {
10871088
// Append entry to list.
10881089
$listGroup.empty();
1090+
console.log("showMultiFactors");
10891091
$.each(multiFactorInfo, i => {
10901092
// Append entry to list.
10911093
const info = multiFactorInfo[i];
@@ -1133,7 +1135,9 @@ function showMultiFactors($listGroup, multiFactorInfo, onClick, onDelete) {
11331135
function onSelectMultiFactorHint(index) {
11341136
// Hide all forms for handling each type of second factors.
11351137
// Currently only phone is supported.
1138+
console.log("onSelectMultiFactorHint called", multiFactorErrorResolver.hints[index].factorId);
11361139
$('#multi-factor-phone').addClass('hidden');
1140+
$('#multi-factor-totp').addClass('hidden');
11371141
if (
11381142
!multiFactorErrorResolver ||
11391143
typeof multiFactorErrorResolver.hints[index] === 'undefined'
@@ -1154,7 +1158,14 @@ function onSelectMultiFactorHint(index) {
11541158
$('#multi-factor-sign-in-verification-id').val('');
11551159
$('#multi-factor-sign-in-verification-code').val('');
11561160
} else if(multiFactorErrorResolver.hints[index].factorId === 'totp'){
1157-
alertError('Working on Totp');
1161+
// Save selected second factor.
1162+
selectedMultiFactorHint = multiFactorErrorResolver.hints[index];
1163+
1164+
// Show sign-in with totp second factor menu.
1165+
$('#multi-factor-totp').removeClass('hidden');
1166+
// Clear all input.
1167+
$('#multi-factor-totp-sign-in-verification-code').val('');
1168+
console.log("code",$('#multi-factor-totp-sign-in-verification-code').val());
11581169
}
11591170
else {
11601171
// 2nd factor not found or not supported by app.
@@ -1210,6 +1221,28 @@ function onFinalizeSignInWithPhoneMultiFactor(event) {
12101221
}, onAuthError);
12111222
}
12121223

1224+
/**
1225+
* Completes sign-in with the 2nd factor totp assertion.
1226+
* @param {!jQuery.Event} event The jQuery event object.
1227+
*/
1228+
function onFinalizeSignInWithTotpMultiFactor(event){
1229+
event.preventDefault();
1230+
// Make sure a second factor is selected.
1231+
console.log(selectedMultiFactorHint, multiFactorErrorResolver);
1232+
const otp = $('#multi-factor-totp-sign-in-verification-code').val();
1233+
console.log("otp ",otp);
1234+
console.log($('#multiFactorModal :input'));
1235+
if (!otp || !selectedMultiFactorHint || !multiFactorErrorResolver) {
1236+
return;
1237+
}
1238+
1239+
const assertion = TotpMultiFactorGenerator.assertionForSignIn(selectedMultiFactorHint.uid, otp)
1240+
multiFactorErrorResolver.resolveSignIn(assertion).then(userCredential => {
1241+
onAuthUserCredentialSuccess(userCredential);
1242+
$('#multiFactorModal').modal('hide');
1243+
}, onAuthError)
1244+
}
1245+
12131246
/**
12141247
* Adds a new row to insert an OAuth custom parameter key/value pair.
12151248
* @param {!jQuery.Event} _event The jQuery event object.
@@ -1988,6 +2021,14 @@ function initApp() {
19882021
$('#sign-in-with-phone-multi-factor').click(
19892022
onFinalizeSignInWithPhoneMultiFactor
19902023
);
2024+
2025+
2026+
// Completes multi-factor sign-in with supplied SMS code.
2027+
$('#sign-in-with-totp-multi-factor').click(
2028+
onFinalizeSignInWithTotpMultiFactor
2029+
);
2030+
2031+
19912032
// Starts multi-factor enrollment with phone number.
19922033
$('#enroll-mfa-verify-phone-number').click(onStartEnrollWithPhoneMultiFactor);
19932034
// Completes multi-factor enrollment with supplied SMS code.

packages/auth/src/api/authentication/mfa.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export interface FinalizePhoneMfaSignInRequest {
111111

112112
export interface FinalizeTotpMfaSignInRequest {
113113
mfaPendingCredential: string;
114-
verificationCode: string
114+
totpVerificationInfo: {verificationCode: string}
115115
tenantId?: string;
116116
mfaEnrollmentId?: string
117117
}
@@ -140,6 +140,11 @@ export function finalizeSignInTotpMfa(
140140
auth: Auth,
141141
request: FinalizeTotpMfaSignInRequest
142142
): Promise<FinalizeTotpMfaSignInResponse> {
143+
144+
console.log("finalizeSignInTotpMfa");
145+
console.log(auth);
146+
console.log(request);
147+
143148
return _performApiRequest<
144149
FinalizeTotpMfaSignInRequest,
145150
FinalizeTotpMfaSignInResponse

packages/auth/src/mfa/assertions/totp.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,20 @@ export class TotpMultiFactorAssertionImpl
158158
auth: AuthInternal,
159159
mfaPendingCredential: string,
160160
): Promise<FinalizeMfaResponse> {
161+
162+
console.log("Secret: ", this.secret);
163+
console.log("_finalizeSingnIn called with ", this.enrollmentId, " ", this.otp);
161164
_assert(
162165
typeof this.enrollmentId !== 'undefined'
163166
&& typeof this.otp !== 'undefined',
164167
auth,
165168
AuthErrorCode.ARGUMENT_ERROR
166169
);
170+
let totpVerificationInfo = {'verificationCode': this.otp};
171+
167172
return finalizeSignInTotpMfa(auth, {
168173
mfaPendingCredential,
169-
verificationCode: this.otp,
174+
totpVerificationInfo: totpVerificationInfo,
170175
mfaEnrollmentId: this.enrollmentId
171176
});
172177
}

0 commit comments

Comments
 (0)