15
15
* limitations under the License.
16
16
*/
17
17
18
- import { expect , use } from 'chai' ;
19
- import chaiAsPromised from 'chai-as-promised' ;
20
- import sinonChai from 'sinon-chai' ;
21
-
22
- //import { mockTotp } from '../../helpers/integration/helpers';
23
- import { Auth , createUserWithEmailAndPassword , multiFactor , signInWithEmailAndPassword , UserCredential , getMultiFactorResolver } from '@firebase/auth' ;
24
- import { FirebaseError , getApp } from '@firebase/app' ;
18
+ import { expect , use } from 'chai' ;
19
+ import chaiAsPromised from 'chai-as-promised' ;
20
+ import sinonChai from 'sinon-chai' ;
21
+ import {
22
+ Auth ,
23
+ multiFactor ,
24
+ signInWithEmailAndPassword ,
25
+ getMultiFactorResolver
26
+ } from '@firebase/auth' ;
27
+ import { FirebaseError } from '@firebase/app' ;
25
28
import {
26
29
cleanUpTestInstance ,
27
30
getTestInstance ,
28
31
getTotpCode ,
29
32
delay ,
30
33
email
31
34
} from '../../helpers/integration/helpers' ;
32
- import { MultiFactorAssertionImpl } from '../../../src/mfa/mfa_assertion' ;
33
-
34
- import { TotpMultiFactorGenerator , TotpSecret } from '../../../src/mfa/assertions/totp' ;
35
- import * as MFA from '../../../src/api/account_management/mfa' ;
36
-
37
35
36
+ import {
37
+ TotpMultiFactorGenerator ,
38
+ TotpSecret
39
+ } from '../../../src/mfa/assertions/totp' ;
38
40
39
41
use ( chaiAsPromised ) ;
40
42
use ( sinonChai ) ;
41
43
42
44
describe ( ' Integration tests: Mfa TOTP' , ( ) => {
43
-
44
-
45
- let auth : Auth ;
46
- let idToken : string ;
47
- let signUpCred : UserCredential ;
48
- let totpSecret : TotpSecret ;
49
- let assertion : MultiFactorAssertionImpl ;
50
- let _request : MFA . StartTotpMfaEnrollmentRequest ;
51
- let startMfaResponse : MFA . StartTotpMfaEnrollmentResponse ;
52
- let displayName : string ;
45
+ let auth : Auth ;
46
+ let totpSecret : TotpSecret ;
47
+ let displayName : string ;
53
48
beforeEach ( async ( ) => {
54
49
auth = getTestInstance ( ) ;
55
50
displayName = 'totp-integration-test' ;
56
51
} ) ;
57
-
52
+
58
53
afterEach ( async ( ) => {
59
54
await cleanUpTestInstance ( auth , 'totp' ) ;
60
-
61
55
} ) ;
62
56
63
57
it ( 'should not enroll if incorrect totp supplied' , async ( ) => {
64
58
let session ;
65
- //await expect(createUserWithEmailAndPassword(auth, email, 'password')).to.be.rejectedWith('auth/email-already-in-use');
66
-
67
59
const cr = await signInWithEmailAndPassword ( auth , email , 'password' ) ;
68
- const mfaUser = multiFactor ( cr . user ) ;
69
- session = await mfaUser . getSession ( ) ;
70
- totpSecret = await TotpMultiFactorGenerator . generateSecret (
71
- session
72
- ) ;
73
- const totpVerificationCode = getTotpCode ( totpSecret . secretKey , totpSecret . codeIntervalSeconds , totpSecret . codeLength , totpSecret . hashingAlgorithm ) ;
74
-
75
- const multiFactorAssertion = TotpMultiFactorGenerator . assertionForEnrollment (
76
- totpSecret ,
77
- totpVerificationCode + '0'
60
+ const mfaUser = multiFactor ( cr . user ) ;
61
+ session = await mfaUser . getSession ( ) ;
62
+ totpSecret = await TotpMultiFactorGenerator . generateSecret ( session ) ;
63
+ const totpVerificationCode = getTotpCode (
64
+ totpSecret . secretKey ,
65
+ totpSecret . codeIntervalSeconds ,
66
+ totpSecret . codeLength ,
67
+ totpSecret . hashingAlgorithm
78
68
) ;
79
69
80
- await expect ( mfaUser . enroll ( multiFactorAssertion , displayName ) ) . to . be . rejectedWith ( 'auth/invalid-verification-code' ) ;
70
+ const multiFactorAssertion =
71
+ TotpMultiFactorGenerator . assertionForEnrollment (
72
+ totpSecret ,
73
+ totpVerificationCode + '0'
74
+ ) ;
75
+
76
+ await expect (
77
+ mfaUser . enroll ( multiFactorAssertion , displayName )
78
+ ) . to . be . rejectedWith ( 'auth/invalid-verification-code' ) ;
81
79
await auth . signOut ( ) ;
82
- } )
83
- it ( 'should enroll using correct otp' , async ( ) => {
80
+ } ) ;
81
+ it ( 'should enroll using correct otp' , async ( ) => {
82
+ let session ;
83
+ const cr = await signInWithEmailAndPassword ( auth , email , 'password' ) ;
84
84
85
- let session ;
86
- //await expect(createUserWithEmailAndPassword(auth, email, 'password')).to.be.rejectedWith('auth/email-already-in-use');
87
- const cr = await signInWithEmailAndPassword ( auth , email , 'password' ) ;
85
+ const mfaUser = multiFactor ( cr . user ) ;
88
86
89
- const mfaUser = multiFactor ( cr . user ) ;
90
-
91
- session = await mfaUser . getSession ( ) ;
87
+ session = await mfaUser . getSession ( ) ;
92
88
93
-
94
- totpSecret = await TotpMultiFactorGenerator . generateSecret (
95
- session
96
- ) ;
97
-
98
- const totpVerificationCode = getTotpCode ( totpSecret . secretKey , totpSecret . codeIntervalSeconds , totpSecret . codeLength , totpSecret . hashingAlgorithm ) ;
89
+ totpSecret = await TotpMultiFactorGenerator . generateSecret ( session ) ;
90
+
91
+ const totpVerificationCode = getTotpCode (
92
+ totpSecret . secretKey ,
93
+ totpSecret . codeIntervalSeconds ,
94
+ totpSecret . codeLength ,
95
+ totpSecret . hashingAlgorithm
96
+ ) ;
99
97
100
- const multiFactorAssertion = TotpMultiFactorGenerator . assertionForEnrollment (
98
+ const multiFactorAssertion =
99
+ TotpMultiFactorGenerator . assertionForEnrollment (
101
100
totpSecret ,
102
101
totpVerificationCode
103
102
) ;
104
- await expect ( mfaUser . enroll ( multiFactorAssertion , displayName ) ) . to . be . fulfilled ;
105
-
103
+ await expect ( mfaUser . enroll ( multiFactorAssertion , displayName ) ) . to . be
104
+ . fulfilled ;
105
+
106
+ await auth . signOut ( ) ;
107
+ } ) ;
108
+
109
+ it ( 'should not allow sign-in with incorrect totp' , async ( ) => {
110
+ let resolver ;
111
+ // Added a delay so that getTotpCode() actually generates a new totp code
112
+ await delay ( 30 * 1000 ) ;
113
+ try {
114
+ await signInWithEmailAndPassword ( auth , email , 'password' ) ;
115
+
116
+ throw new Error ( 'Signin should not have been successful' ) ;
117
+ } catch ( error ) {
118
+ expect ( error ) . to . be . an . instanceOf ( FirebaseError ) ;
119
+ expect ( ( error as any ) . code ) . to . eql ( 'auth/multi-factor-auth-required' ) ;
120
+
121
+ resolver = getMultiFactorResolver ( auth , error as any ) ;
122
+ expect ( resolver . hints ) . to . have . length ( 1 ) ;
123
+
124
+ const totpVerificationCode = getTotpCode (
125
+ totpSecret . secretKey ,
126
+ totpSecret . codeIntervalSeconds ,
127
+ totpSecret . codeLength ,
128
+ totpSecret . hashingAlgorithm
129
+ ) ;
130
+ const assertion = TotpMultiFactorGenerator . assertionForSignIn (
131
+ resolver . hints [ 0 ] . uid ,
132
+ totpVerificationCode + '0'
133
+ ) ;
134
+
135
+ await expect ( resolver . resolveSignIn ( assertion ) ) . to . be . rejectedWith (
136
+ 'auth/invalid-verification-code'
137
+ ) ;
138
+
106
139
await auth . signOut ( ) ;
140
+ }
141
+ } ) . timeout ( 31000 ) ;
107
142
108
- } )
143
+ it ( 'should allow sign-in with for correct totp and unenroll successfully' , async ( ) => {
144
+ let resolver ;
109
145
110
- it ( 'should not allow sign-in with incorrect totp' , async ( ) => {
111
- let session ;
112
- let cr ;
113
- let resolver ;
114
- //await expect(createUserWithEmailAndPassword(auth, email, 'password')).to.be.rejectedWith('auth/email-already-in-use');
115
- // Added a delay so that getTotpCode() actually generates a new totp code
116
- await delay ( 30 * 1000 ) ;
117
- try {
146
+ await delay ( 30 * 1000 ) ;
147
+ // Added a delay so that getTotpCode() actually generates a new totp code
118
148
119
- const userCredential = await signInWithEmailAndPassword ( auth , email , 'password' ) ;
149
+ try {
150
+ await signInWithEmailAndPassword ( auth , email , 'password' ) ;
120
151
121
- throw new Error ( 'Signin should not have been successful' ) ;
152
+ throw new Error ( 'Signin should not have been successful' ) ;
153
+ } catch ( error ) {
154
+ expect ( error ) . to . be . an . instanceOf ( FirebaseError ) ;
155
+ expect ( ( error as any ) . code ) . to . eql ( 'auth/multi-factor-auth-required' ) ;
122
156
123
- } catch ( error ) {
157
+ resolver = getMultiFactorResolver ( auth , error as any ) ;
158
+ expect ( resolver . hints ) . to . have . length ( 1 ) ;
159
+
160
+ const totpVerificationCode = getTotpCode (
161
+ totpSecret . secretKey ,
162
+ totpSecret . codeIntervalSeconds ,
163
+ totpSecret . codeLength ,
164
+ totpSecret . hashingAlgorithm
165
+ ) ;
166
+ const assertion = TotpMultiFactorGenerator . assertionForSignIn (
167
+ resolver . hints [ 0 ] . uid ,
168
+ totpVerificationCode
169
+ ) ;
170
+ const userCredential = await resolver . resolveSignIn ( assertion ) ;
124
171
125
-
126
- expect ( ( error as any ) . code ) . to . eql ( 'auth/multi-factor-auth-required' ) ;
172
+ const mfaUser = multiFactor ( userCredential . user ) ;
127
173
128
- resolver = getMultiFactorResolver ( auth , error as any ) ;
129
- expect ( resolver . hints ) . to . have . length ( 1 ) ;
174
+ await expect ( mfaUser . unenroll ( resolver . hints [ 0 ] . uid ) ) . to . be . fulfilled ;
130
175
131
- const totpVerificationCode = getTotpCode ( totpSecret . secretKey , totpSecret . codeIntervalSeconds , totpSecret . codeLength , totpSecret . hashingAlgorithm ) ;
132
- const assertion = TotpMultiFactorGenerator . assertionForSignIn (
133
- resolver . hints [ 0 ] . uid ,
134
- totpVerificationCode + '0'
135
- ) ;
136
-
137
- await expect ( resolver . resolveSignIn ( assertion ) ) . to . be . rejectedWith ( 'auth/invalid-verification-code' ) ;
138
-
139
- await auth . signOut ( ) ;
140
-
141
- }
142
-
143
-
144
- } ) . timeout ( 31000 ) ;
145
-
146
- it ( 'should allow sign-in with for correct totp and unenroll successfully' , async ( ) => {
147
-
148
- let resolver ;
149
-
150
- await delay ( 30 * 1000 ) ;
151
-
152
- //await expect(createUserWithEmailAndPassword(auth, email, 'password')).to.be.rejectedWith('auth/email-already-in-use');
153
- // Added a delay so that getTotpCode() actually generates a new totp code
154
-
155
- try {
156
-
157
- const userCredential = await signInWithEmailAndPassword ( auth , email , 'password' ) ;
158
-
159
-
160
- throw new Error ( 'Signin should not have been successful' ) ;
161
-
162
- } catch ( error ) {
163
-
164
- expect ( error ) . to . be . an . instanceOf ( FirebaseError ) ;
165
- expect ( ( error as any ) . code ) . to . eql ( 'auth/multi-factor-auth-required' ) ;
166
-
167
- resolver = getMultiFactorResolver ( auth , error as any ) ;
168
- expect ( resolver . hints ) . to . have . length ( 1 ) ;
169
-
170
- const totpVerificationCode = getTotpCode ( totpSecret . secretKey , totpSecret . codeIntervalSeconds , totpSecret . codeLength , totpSecret . hashingAlgorithm ) ;
171
- const assertion = TotpMultiFactorGenerator . assertionForSignIn (
172
- resolver . hints [ 0 ] . uid ,
173
- totpVerificationCode
174
- ) ;
175
- const userCredential = await resolver . resolveSignIn ( assertion ) ;
176
-
177
- const mfaUser = multiFactor ( userCredential . user ) ;
178
-
179
- await expect ( mfaUser . unenroll ( resolver . hints [ 0 ] . uid ) ) . to . be . fulfilled ;
180
-
181
- await auth . signOut ( ) ;
182
-
176
+ await auth . signOut ( ) ;
183
177
}
184
- } ) . timeout ( 35000 ) ;
185
- } )
178
+ } ) . timeout ( 35000 ) ;
179
+ } ) ;
0 commit comments