Skip to content

Commit a412656

Browse files
committed
test cases working with verified email
1 parent ba2d6f6 commit a412656

File tree

2 files changed

+145
-67
lines changed

2 files changed

+145
-67
lines changed

packages/auth/test/helpers/integration/helpers.d.ts

Whitespace-only changes.

packages/auth/test/integration/flows/totp.test.ts

Lines changed: 145 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -21,127 +21,205 @@
2121
import sinonChai from 'sinon-chai';
2222

2323
//import { mockTotp } from '../../helpers/integration/helpers';
24-
import {Auth, createUserWithEmailAndPassword, multiFactor, signInAnonymously, signInWithEmailAndPassword, UserCredential} from '@firebase/auth';
25-
24+
import {Auth, createUserWithEmailAndPassword, multiFactor, signInWithEmailAndPassword, UserCredential, sendEmailVerification, applyActionCode, getMultiFactorResolver} from '@firebase/auth';
25+
import { FirebaseError, getApp } from '@firebase/app';
2626
import {
2727
cleanUpTestInstance,
28+
code,
2829
getTestInstance,
29-
mockTotp,
30+
getTotpCode,
31+
delay,
3032
randomEmail
3133
} from '../../helpers/integration/helpers';
3234
import { MultiFactorAssertionImpl } from '../../../src/mfa/mfa_assertion';
3335

3436
import { MultiFactorSessionImpl } from '../../../src/mfa/mfa_session';
35-
import { TotpMultiFactorAssertionImpl, TotpMultiFactorGenerator, TotpSecret } from '../../../src/mfa/assertions/totp';
37+
import { TotpMultiFactorGenerator, TotpSecret } from '../../../src/mfa/assertions/totp';
3638
import * as MFA from '../../../src/api/account_management/mfa';
37-
import { FirebaseError } from '@firebase/util';
39+
import { async } from '@firebase/util';
40+
import { UserCredentialImpl } from '../../../src/core/user/user_credential_impl';
41+
import { resolve } from 'dns';
42+
import { UserCredentialInternal } from '../../../internal';
3843

3944

4045

4146
use(chaiAsPromised);
4247
use(sinonChai);
4348

44-
const TOTP_COMB_A = {
45-
46-
response: { sharedSecretKey: 'secretKey3',
47-
verificationCodeLength: 30,
48-
hashingAlgorithm: 'sha1',
49-
periodSec:30,
50-
sessionInfo: 'testsSessionInfo',
51-
finalizeEnrollmentTime: Date.now()
52-
},
53-
code: '...'
54-
};
55-
56-
const TOTP_COMB_B = {
57-
58-
response: { sharedSecretKey: 'secretKey2',
59-
verificationCodeLength: 30,
60-
hashingAlgorithm: 'sha1',
61-
periodSec: 30,
62-
sessionInfo: 'testsSessionInfo',
63-
finalizeEnrollmentTime: Date.now()
64-
},
65-
code: '...'
66-
};
67-
6849
describe(' Integration tests: Mfa TOTP', () => {
50+
51+
6952
let auth: Auth;
7053
let idToken: string;
7154
let signUpCred: UserCredential;
55+
let totpSecret: TotpSecret;
7256
let email: string;
7357
let assertion: MultiFactorAssertionImpl;
7458
let _request: MFA.StartTotpMfaEnrollmentRequest;
7559
let startMfaResponse: MFA.StartTotpMfaEnrollmentResponse;
7660
let displayName: string;
7761
beforeEach(async () => {
7862
auth = getTestInstance();
79-
email =randomEmail();
80-
idToken = 'testIdToken';
81-
signUpCred = await createUserWithEmailAndPassword(
82-
auth,
83-
email,
84-
'password'
85-
);
86-
await auth.signOut();
63+
email = '[email protected]';
64+
displayName = 'totp-integration-test';
65+
// signUpCred = await createUserWithEmailAndPassword(
66+
67+
// auth,
68+
// email,
69+
// 'password'
70+
// );
71+
// await auth.signOut();
8772
});
8873

8974
afterEach(async () => {
9075
await cleanUpTestInstance(auth);
9176

9277
});
93-
it('should verify using otp', async () => {
9478

79+
it('should not enroll if incorrect totp supplied', async () => {
80+
let session;
81+
console.log(email);
82+
console.log('session info for: ', getApp().options.projectId);
83+
console.log('auth current User:', auth.currentUser);
84+
const cr = await signInWithEmailAndPassword(auth, email, 'password');
85+
86+
console.log('signed In for totp');
87+
const mfaUser = multiFactor(cr.user);
88+
89+
console.log('session info for: ');
90+
session = await mfaUser.getSession();
91+
console.log(JSON.stringify(session));
92+
93+
94+
totpSecret = await TotpMultiFactorGenerator.generateSecret(
95+
session
96+
);
97+
98+
console.log("**** totpSecret****");
99+
console.log(totpSecret.secretKey);
100+
console.log(totpSecret.codeLength);
101+
console.log(totpSecret.codeIntervalSeconds);
102+
console.log(totpSecret.hashingAlgorithm);
103+
104+
const totpVerificationCode = getTotpCode(totpSecret.secretKey, totpSecret.codeIntervalSeconds, totpSecret.codeLength, totpSecret.hashingAlgorithm);
105+
106+
const multiFactorAssertion = TotpMultiFactorGenerator.assertionForEnrollment(
107+
totpSecret,
108+
totpVerificationCode + '0'
109+
);
110+
111+
console.log(totpVerificationCode);
112+
await expect(mfaUser.enroll(multiFactorAssertion, displayName)).to.be.rejectedWith('auth/invalid-verification-code');
113+
await auth.signOut();
114+
})
115+
it('should enroll using correct otp', async () => {
116+
117+
let session;
95118
console.log(email);
119+
console.log('session info for: ', getApp().options.projectId);
120+
console.log('auth current User:', auth.currentUser);
96121
const cr = await signInWithEmailAndPassword(auth, email, 'password');
97122

98-
startMfaResponse = { totpSessionInfo: TOTP_COMB_A.response}
99123

124+
// await sendEmailVerification(cr.user);
100125

101-
102-
const mfaUser = multiFactor(cr.user);
103-
sinon.spy(MultiFactorSessionImpl, '_fromIdtoken');
104-
105-
sinon.stub(mfaUser, 'getSession').returns(
106-
Promise.resolve(MultiFactorSessionImpl._fromIdtoken(idToken, auth as any)));
107-
108-
sinon.stub(MFA, 'startEnrollTotpMfa').callsFake((_auth,_request)=>{
126+
// //Apply the email verification code
127+
// await applyActionCode(auth, (await code(email)).oobCode);
128+
// await cr.user.reload();
129+
// expect(cr.user.emailVerified).to.be.true;
109130

110-
return Promise.resolve(startMfaResponse)
111-
})
131+
console.log('signed In for totp');
132+
const mfaUser = multiFactor(cr.user);
112133

113-
134+
console.log('session info for: ');
114135

115-
const session = await mfaUser.getSession();
116-
117-
console.log(session);
136+
137+
session = await mfaUser.getSession();
138+
139+
140+
console.log('session');
141+
console.log(JSON.stringify(session));
142+
118143

119-
const totpSecret = await TotpMultiFactorGenerator.generateSecret(
144+
totpSecret = await TotpMultiFactorGenerator.generateSecret(
120145
session
121146
);
122147

123-
console.log("**** totpSecret"+ totpSecret);
124-
// https://stackoverflow.com/questions/48931815/sinon-stub-not-replacing-function
125-
// https://stackoverflow.com/questions/61051247/chai-spies-expect-to-have-been-called-is-failing-on-local-methods
126-
expect(MultiFactorSessionImpl._fromIdtoken).to.have.been.calledOnce;
127-
//expect(TotpSecret._fromStartTotpMfaEnrollmentResponse).to.have.been.calledOnce;
128-
expect(MFA.startEnrollTotpMfa).to.have.been.calledOnce;
148+
console.log("**** totpSecret****");
149+
150+
console.log(totpSecret.secretKey);
151+
console.log(totpSecret.codeLength);
152+
console.log(totpSecret.codeIntervalSeconds);
153+
console.log(totpSecret.hashingAlgorithm);
129154

130-
expect(await MFA.startEnrollTotpMfa(auth as any, _request)).to.eql(startMfaResponse)
131-
132-
expect(totpSecret.secretKey).to.eql(startMfaResponse.totpSessionInfo.sharedSecretKey)
133-
expect(totpSecret.codeLength).to.eql(startMfaResponse.totpSessionInfo.verificationCodeLength)
134155

135-
const totpVerificationCode = await mockTotp(totpSecret.secretKey, totpSecret.codeLength, totpSecret.codeIntervalSeconds);
156+
157+
158+
const totpVerificationCode = getTotpCode(totpSecret.secretKey, totpSecret.codeIntervalSeconds, totpSecret.codeLength, totpSecret.hashingAlgorithm);
136159

137160
const multiFactorAssertion = TotpMultiFactorGenerator.assertionForEnrollment(
138161
totpSecret,
139162
totpVerificationCode
140163
);
141164
console.log(totpVerificationCode);
142-
// auth/invalid-idToken
143-
await expect(mfaUser.enroll(multiFactorAssertion, displayName)).to.be.rejectedWith('auth/invalid-user-token')
165+
166+
await expect(mfaUser.enroll(multiFactorAssertion, displayName)).to.be.fulfilled;
144167

168+
await auth.signOut();
169+
170+
//await expect(signInWithEmailAndPassword(auth, email, 'password')).to.be.rejectedWith('auth/multi-factor-auth-required');
171+
172+
173+
//await expect().to.be.rejectedWith(FirebaseError);
174+
175+
})
176+
177+
it('should allow sign-in for correct totp', async () => {
178+
let session;
179+
let cr;
180+
let resolver;
181+
console.log(email);
182+
console.log('session info for: ', getApp().options.projectId);
183+
184+
await delay(15*1000);
185+
try{
186+
187+
const userCredential = await signInWithEmailAndPassword(auth, email, 'password');
188+
189+
console.log('success: ', userCredential);
190+
191+
throw new Error('Signin should not have been successful');
192+
193+
} catch(error ){
194+
195+
196+
console.log('error occured: ', (error as any).code);
197+
expect((error as any).code).to.eql('auth/multi-factor-auth-required');
198+
199+
resolver = getMultiFactorResolver(auth,error as any);
200+
console.log(resolver.hints, totpSecret.secretKey);
201+
expect(resolver.hints).to.have.length(1);
202+
203+
const totpVerificationCode = getTotpCode(totpSecret.secretKey, totpSecret.codeIntervalSeconds, totpSecret.codeLength, totpSecret.hashingAlgorithm);
204+
console.log(totpVerificationCode, resolver.hints[0].uid )
205+
const assertion = TotpMultiFactorGenerator.assertionForSignIn(
206+
resolver.hints[0].uid,
207+
totpVerificationCode + '0'
208+
);
209+
210+
console.log(assertion);
211+
212+
213+
await expect(resolver.resolveSignIn(assertion)).to.be.rejectedWith('nothing');
214+
215+
await auth.signOut();
216+
217+
}
218+
219+
220+
})
221+
222+
it('should allow sign-in with for correct totp and unenroll successfully', async() => {
145223

146224
})
147225
})

0 commit comments

Comments
 (0)