@@ -49,6 +49,8 @@ import {
49
49
signInWithCredential ,
50
50
signInWithCustomToken ,
51
51
signInWithEmailAndPassword ,
52
+ TotpMultiFactorGenerator ,
53
+ TotpSecret ,
52
54
unlink ,
53
55
updateEmail ,
54
56
updatePassword ,
@@ -97,6 +99,7 @@ let multiFactorErrorResolver = null;
97
99
let selectedMultiFactorHint = null ;
98
100
let recaptchaSize = 'normal' ;
99
101
let webWorker = null ;
102
+ let totpSecret = null ;
100
103
101
104
// The corresponding Font Awesome icons for each provider.
102
105
const providersIcons = {
@@ -652,6 +655,50 @@ function onFinalizeEnrollWithPhoneMultiFactor() {
652
655
} , onAuthError ) ;
653
656
}
654
657
658
+ async function onStartEnrollWithTotpMultiFactor ( ) {
659
+ console . log ( 'Starting TOTP enrollment!' ) ;
660
+ if ( ! activeUser ( ) ) {
661
+ alertError ( 'No active user found.' ) ;
662
+ return ;
663
+ }
664
+ try {
665
+ multiFactorSession = await multiFactor ( activeUser ( ) ) . getSession ( ) ;
666
+ totpSecret = await TotpMultiFactorGenerator . generateSecret (
667
+ multiFactorSession
668
+ ) ;
669
+ const url = totpSecret . generateQrCodeUrl ( 'test' , 'testissuer' ) ;
670
+ console . log ( 'TOTP URL is ' + url ) ;
671
+ // Use the QRServer API documented at https://goqr.me/api/doc/
672
+ const qrCodeUrl = `https://api.qrserver.com/v1/create-qr-code/?data=${ url } &size=30x30` ;
673
+ $ ( 'img.totp-qr-image' ) . attr ( 'src' , qrCodeUrl ) . show ( ) ;
674
+ $ ( 'p.totp-text' ) . show ( ) ;
675
+ } catch ( e ) {
676
+ onAuthError ( e ) ;
677
+ }
678
+ }
679
+
680
+ async function onFinalizeEnrollWithTotpMultiFactor ( ) {
681
+ const verificationCode = $ ( '#enroll-mfa-totp-verification-code' ) . val ( ) ;
682
+ if ( ! activeUser ( ) || ! totpSecret || ! verificationCode ) {
683
+ alertError ( ' Missing active user OR TOTP secret OR verification code.' ) ;
684
+ return ;
685
+ }
686
+
687
+ const multiFactorAssertion = TotpMultiFactorGenerator . assertionForEnrollment (
688
+ totpSecret ,
689
+ verificationCode
690
+ ) ;
691
+ const displayName = $ ( '#enroll-mfa-totp-display-name' ) . val ( ) || undefined ;
692
+
693
+ try {
694
+ await multiFactor ( activeUser ( ) ) . enroll ( multiFactorAssertion , displayName ) ;
695
+ refreshUserData ( ) ;
696
+ alertSuccess ( 'TOTP MFA enrolled!' ) ;
697
+ } catch ( e ) {
698
+ onAuthError ( e ) ;
699
+ }
700
+ }
701
+
655
702
/**
656
703
* Signs in or links a provider's credential, based on current tab opened.
657
704
* @param {!AuthCredential } credential The provider's credential.
@@ -1944,6 +1991,10 @@ function initApp() {
1944
1991
$ ( '#enroll-mfa-confirm-phone-verification' ) . click (
1945
1992
onFinalizeEnrollWithPhoneMultiFactor
1946
1993
) ;
1994
+ // Starts multi-factor enrollment with TOTP.
1995
+ $ ( '#enroll-mfa-totp-start' ) . click ( onStartEnrollWithTotpMultiFactor ) ;
1996
+ // Completes multi-factor enrollment with supplied OTP(One-Time Password).
1997
+ $ ( '#enroll-mfa-totp-finalize' ) . click ( onFinalizeEnrollWithTotpMultiFactor ) ;
1947
1998
}
1948
1999
1949
2000
$ ( initApp ) ;
0 commit comments