Skip to content

Commit 466c8f5

Browse files
committed
adding singin for totp in demoapp
1 parent 583216f commit 466c8f5

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
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: 39 additions & 2 deletions
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
}
@@ -1134,6 +1135,7 @@ function onSelectMultiFactorHint(index) {
11341135
// Hide all forms for handling each type of second factors.
11351136
// Currently only phone is supported.
11361137
$('#multi-factor-phone').addClass('hidden');
1138+
$('#multi-factor-totp').addClass('hidden');
11371139
if (
11381140
!multiFactorErrorResolver ||
11391141
typeof multiFactorErrorResolver.hints[index] === 'undefined'
@@ -1153,7 +1155,16 @@ function onSelectMultiFactorHint(index) {
11531155
// Clear all input.
11541156
$('#multi-factor-sign-in-verification-id').val('');
11551157
$('#multi-factor-sign-in-verification-code').val('');
1156-
} else {
1158+
} else if(multiFactorErrorResolver.hints[index].factorId === 'totp'){
1159+
// Save selected second factor.
1160+
selectedMultiFactorHint = multiFactorErrorResolver.hints[index];
1161+
1162+
// Show sign-in with totp second factor menu.
1163+
$('#multi-factor-totp').removeClass('hidden');
1164+
// Clear all input.
1165+
$('#multi-factor-totp-sign-in-verification-code').val('');
1166+
}
1167+
else {
11571168
// 2nd factor not found or not supported by app.
11581169
alertError('Selected 2nd factor is not supported!');
11591170
}
@@ -1207,6 +1218,25 @@ function onFinalizeSignInWithPhoneMultiFactor(event) {
12071218
}, onAuthError);
12081219
}
12091220

1221+
/**
1222+
* Completes sign-in with the 2nd factor totp assertion.
1223+
* @param {!jQuery.Event} event The jQuery event object.
1224+
*/
1225+
function onFinalizeSignInWithTotpMultiFactor(event){
1226+
event.preventDefault();
1227+
// Make sure a second factor is selected.
1228+
const otp = $('#multi-factor-totp-sign-in-verification-code').val();
1229+
if (!otp || !selectedMultiFactorHint || !multiFactorErrorResolver) {
1230+
return;
1231+
}
1232+
1233+
const assertion = TotpMultiFactorGenerator.assertionForSignIn(selectedMultiFactorHint.uid, otp)
1234+
multiFactorErrorResolver.resolveSignIn(assertion).then(userCredential => {
1235+
onAuthUserCredentialSuccess(userCredential);
1236+
$('#multiFactorModal').modal('hide');
1237+
}, onAuthError)
1238+
}
1239+
12101240
/**
12111241
* Adds a new row to insert an OAuth custom parameter key/value pair.
12121242
* @param {!jQuery.Event} _event The jQuery event object.
@@ -1336,7 +1366,6 @@ function signInWithPopupRedirect(provider) {
13361366
customParameters[key] = value;
13371367
}
13381368
});
1339-
console.log('customParameters: ', customParameters);
13401369
// For older jscore versions that do not support this.
13411370
if (provider.setCustomParameters) {
13421371
// Set custom parameters on current provider.
@@ -1985,6 +2014,14 @@ function initApp() {
19852014
$('#sign-in-with-phone-multi-factor').click(
19862015
onFinalizeSignInWithPhoneMultiFactor
19872016
);
2017+
2018+
2019+
// Completes multi-factor sign-in with supplied SMS code.
2020+
$('#sign-in-with-totp-multi-factor').click(
2021+
onFinalizeSignInWithTotpMultiFactor
2022+
);
2023+
2024+
19882025
// Starts multi-factor enrollment with phone number.
19892026
$('#enroll-mfa-verify-phone-number').click(onStartEnrollWithPhoneMultiFactor);
19902027
// Completes multi-factor enrollment with supplied SMS code.

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

Lines changed: 1 addition & 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
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,10 @@ export class TotpMultiFactorAssertionImpl
164164
auth,
165165
AuthErrorCode.ARGUMENT_ERROR
166166
);
167+
const totpVerificationInfo = {'verificationCode': this.otp};
167168
return finalizeSignInTotpMfa(auth, {
168169
mfaPendingCredential,
169-
verificationCode: this.otp,
170+
totpVerificationInfo: totpVerificationInfo,
170171
mfaEnrollmentId: this.enrollmentId
171172
});
172173
}

0 commit comments

Comments
 (0)