@@ -37,6 +37,8 @@ import {
37
37
TotpMultiFactorGenerator ,
38
38
TotpSecret
39
39
} from '../../../src/mfa/assertions/totp' ;
40
+ import { _emulatorUrl } from '../../../src/core/util/emulator' ;
41
+ import { getEmulatorUrl } from '../../helpers/integration/settings' ;
40
42
41
43
use ( chaiAsPromised ) ;
42
44
use ( sinonChai ) ;
@@ -46,116 +48,130 @@ describe(' Integration tests: Mfa TOTP', () => {
46
48
let totpSecret : TotpSecret ;
47
49
let displayName : string ;
48
50
let totpTimestamp : Date ;
51
+ let emulatorUrl : string | null ;
49
52
beforeEach ( async ( ) => {
50
- auth = getTestInstance ( ) ;
51
- displayName = 'totp-integration-test' ;
53
+ emulatorUrl = getEmulatorUrl ( ) ;
54
+ if ( ! emulatorUrl ) {
55
+ auth = getTestInstance ( ) ;
56
+ displayName = 'totp-integration-test' ;
57
+ }
52
58
} ) ;
53
59
54
60
afterEach ( async ( ) => {
55
- await cleanUpTestInstance ( auth ) ;
61
+ if ( ! emulatorUrl ) {
62
+ await cleanUpTestInstance ( auth ) ;
63
+ }
56
64
} ) ;
57
65
58
66
it ( 'should not enroll if incorrect totp supplied' , async ( ) => {
59
- const cr = await signInWithEmailAndPassword ( auth , email , 'password' ) ;
60
- const mfaUser = multiFactor ( cr . user ) ;
61
- const session = await mfaUser . getSession ( ) ;
62
- totpSecret = await TotpMultiFactorGenerator . generateSecret ( session ) ;
63
- const multiFactorAssertion =
64
- TotpMultiFactorGenerator . assertionForEnrollment (
65
- totpSecret ,
66
- incorrectTotpCode
67
- ) ;
68
-
69
- await expect (
70
- mfaUser . enroll ( multiFactorAssertion , displayName )
71
- ) . to . be . rejectedWith ( 'auth/invalid-verification-code' ) ;
67
+ if ( ! emulatorUrl ) {
68
+ const cr = await signInWithEmailAndPassword ( auth , email , 'password' ) ;
69
+ const mfaUser = multiFactor ( cr . user ) ;
70
+ const session = await mfaUser . getSession ( ) ;
71
+ totpSecret = await TotpMultiFactorGenerator . generateSecret ( session ) ;
72
+ const multiFactorAssertion =
73
+ TotpMultiFactorGenerator . assertionForEnrollment (
74
+ totpSecret ,
75
+ incorrectTotpCode
76
+ ) ;
77
+
78
+ await expect (
79
+ mfaUser . enroll ( multiFactorAssertion , displayName )
80
+ ) . to . be . rejectedWith ( 'auth/invalid-verification-code' ) ;
81
+ }
72
82
} ) ;
73
83
74
84
it ( 'should enroll using correct otp' , async ( ) => {
75
- const cr = await signInWithEmailAndPassword ( auth , email , 'password' ) ;
76
-
77
- const mfaUser = multiFactor ( cr . user ) ;
85
+ if ( ! emulatorUrl ) {
86
+ const cr = await signInWithEmailAndPassword ( auth , email , 'password' ) ;
78
87
79
- const session = await mfaUser . getSession ( ) ;
88
+ const mfaUser = multiFactor ( cr . user ) ;
80
89
81
- totpSecret = await TotpMultiFactorGenerator . generateSecret ( session ) ;
90
+ const session = await mfaUser . getSession ( ) ;
82
91
83
- totpTimestamp = new Date ( ) ;
92
+ totpSecret = await TotpMultiFactorGenerator . generateSecret ( session ) ;
84
93
85
- const totpVerificationCode = getTotpCode (
86
- totpSecret . secretKey ,
87
- totpSecret . codeIntervalSeconds ,
88
- totpSecret . codeLength ,
89
- totpTimestamp
90
- ) ;
94
+ totpTimestamp = new Date ( ) ;
91
95
92
- const multiFactorAssertion =
93
- TotpMultiFactorGenerator . assertionForEnrollment (
94
- totpSecret ,
95
- totpVerificationCode
96
+ const totpVerificationCode = getTotpCode (
97
+ totpSecret . secretKey ,
98
+ totpSecret . codeIntervalSeconds ,
99
+ totpSecret . codeLength ,
100
+ totpTimestamp
96
101
) ;
97
- await expect ( mfaUser . enroll ( multiFactorAssertion , displayName ) ) . to . be
98
- . fulfilled ;
102
+
103
+ const multiFactorAssertion =
104
+ TotpMultiFactorGenerator . assertionForEnrollment (
105
+ totpSecret ,
106
+ totpVerificationCode
107
+ ) ;
108
+ await expect ( mfaUser . enroll ( multiFactorAssertion , displayName ) ) . to . be
109
+ . fulfilled ;
110
+ }
99
111
} ) ;
100
112
101
113
it ( 'should not allow sign-in with incorrect totp' , async ( ) => {
102
114
let resolver ;
103
115
104
- try {
105
- await signInWithEmailAndPassword ( auth , email , 'password' ) ;
116
+ if ( ! emulatorUrl ) {
117
+ try {
118
+ await signInWithEmailAndPassword ( auth , email , 'password' ) ;
106
119
107
- throw new Error ( 'Signin should not have been successful' ) ;
108
- } catch ( error ) {
109
- expect ( error ) . to . be . an . instanceOf ( FirebaseError ) ;
110
- expect ( ( error as any ) . code ) . to . eql ( 'auth/multi-factor-auth-required' ) ;
120
+ throw new Error ( 'Signin should not have been successful' ) ;
121
+ } catch ( error ) {
122
+ expect ( error ) . to . be . an . instanceOf ( FirebaseError ) ;
123
+ expect ( ( error as any ) . code ) . to . eql ( 'auth/multi-factor-auth-required' ) ;
111
124
112
- resolver = getMultiFactorResolver ( auth , error as any ) ;
113
- expect ( resolver . hints ) . to . have . length ( 1 ) ;
125
+ resolver = getMultiFactorResolver ( auth , error as any ) ;
126
+ expect ( resolver . hints ) . to . have . length ( 1 ) ;
114
127
115
- const assertion = TotpMultiFactorGenerator . assertionForSignIn (
116
- resolver . hints [ 0 ] . uid ,
117
- incorrectTotpCode
118
- ) ;
128
+ const assertion = TotpMultiFactorGenerator . assertionForSignIn (
129
+ resolver . hints [ 0 ] . uid ,
130
+ incorrectTotpCode
131
+ ) ;
119
132
120
- await expect ( resolver . resolveSignIn ( assertion ) ) . to . be . rejectedWith (
121
- 'auth/invalid-verification-code'
122
- ) ;
133
+ await expect ( resolver . resolveSignIn ( assertion ) ) . to . be . rejectedWith (
134
+ 'auth/invalid-verification-code'
135
+ ) ;
136
+ }
123
137
}
124
138
} ) ;
125
139
126
140
it ( 'should allow sign-in with for correct totp and unenroll successfully' , async ( ) => {
127
141
let resolver ;
128
- try {
129
- await signInWithEmailAndPassword ( auth , email , 'password' ) ;
130
-
131
- throw new Error ( 'Signin should not have been successful' ) ;
132
- } catch ( error ) {
133
- expect ( error ) . to . be . an . instanceOf ( FirebaseError ) ;
134
- expect ( ( error as any ) . code ) . to . eql ( 'auth/multi-factor-auth-required' ) ;
135
-
136
- resolver = getMultiFactorResolver ( auth , error as any ) ;
137
- expect ( resolver . hints ) . to . have . length ( 1 ) ;
138
-
139
- totpTimestamp . setSeconds ( totpTimestamp . getSeconds ( ) + 30 ) ;
140
-
141
- const totpVerificationCode = getTotpCode (
142
- totpSecret . secretKey ,
143
- totpSecret . codeIntervalSeconds ,
144
- totpSecret . codeLength ,
145
- totpTimestamp
146
- ) ;
147
-
148
- const assertion = TotpMultiFactorGenerator . assertionForSignIn (
149
- resolver . hints [ 0 ] . uid ,
150
- totpVerificationCode
151
- ) ;
152
- const userCredential = await resolver . resolveSignIn ( assertion ) ;
153
-
154
- const mfaUser = multiFactor ( userCredential . user ) ;
155
-
156
- await expect ( mfaUser . unenroll ( resolver . hints [ 0 ] . uid ) ) . to . be . fulfilled ;
157
- await expect ( signInWithEmailAndPassword ( auth , email , 'password' ) ) . to . be
158
- . fulfilled ;
142
+ if ( ! emulatorUrl ) {
143
+ try {
144
+ await signInWithEmailAndPassword ( auth , email , 'password' ) ;
145
+
146
+ throw new Error ( 'Signin should not have been successful' ) ;
147
+ } catch ( error ) {
148
+ expect ( error ) . to . be . an . instanceOf ( FirebaseError ) ;
149
+ expect ( ( error as any ) . code ) . to . eql ( 'auth/multi-factor-auth-required' ) ;
150
+
151
+ resolver = getMultiFactorResolver ( auth , error as any ) ;
152
+ expect ( resolver . hints ) . to . have . length ( 1 ) ;
153
+
154
+ totpTimestamp . setSeconds ( totpTimestamp . getSeconds ( ) + 30 ) ;
155
+
156
+ const totpVerificationCode = getTotpCode (
157
+ totpSecret . secretKey ,
158
+ totpSecret . codeIntervalSeconds ,
159
+ totpSecret . codeLength ,
160
+ totpTimestamp
161
+ ) ;
162
+
163
+ const assertion = TotpMultiFactorGenerator . assertionForSignIn (
164
+ resolver . hints [ 0 ] . uid ,
165
+ totpVerificationCode
166
+ ) ;
167
+ const userCredential = await resolver . resolveSignIn ( assertion ) ;
168
+
169
+ const mfaUser = multiFactor ( userCredential . user ) ;
170
+
171
+ await expect ( mfaUser . unenroll ( resolver . hints [ 0 ] . uid ) ) . to . be . fulfilled ;
172
+ await expect ( signInWithEmailAndPassword ( auth , email , 'password' ) ) . to . be
173
+ . fulfilled ;
174
+ }
159
175
}
160
176
} ) ;
161
177
} ) ;
0 commit comments