Skip to content

Commit 81788fd

Browse files
committed
removed delay function and used timestamp for totp generator
1 parent 0a20bcf commit 81788fd

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,18 @@ function stubConsoleToSilenceEmulatorWarnings(): sinon.SinonStub {
9999
export function getTotpCode(
100100
sharedSecretKey: string,
101101
periodSec: number,
102-
verificationCodeLength: number
102+
verificationCodeLength: number,
103+
timestamp: Date
103104
): string {
104105
const token = totp(sharedSecretKey, {
105106
period: periodSec,
106107
digits: verificationCodeLength,
107-
algorithm: 'SHA-1'
108+
algorithm: 'SHA-1',
109+
timestamp: timestamp
108110
});
109111

110112
return token;
111113
}
112-
113-
export function delay(dt: number): Promise<void> {
114-
return new Promise(resolve => setTimeout(resolve, dt));
115-
}
116-
117114
export const email = '[email protected]';
118115
//1000000 is always incorrect since it has 7 digits and we expect 6.
119116
export const incorrectTotpCode = '1000000';

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

+11-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import {
2929
cleanUpTestInstance,
3030
getTestInstance,
3131
getTotpCode,
32-
delay,
3332
email,
3433
incorrectTotpCode
3534
} from '../../helpers/integration/helpers';
@@ -46,6 +45,7 @@ describe(' Integration tests: Mfa TOTP', () => {
4645
let auth: Auth;
4746
let totpSecret: TotpSecret;
4847
let displayName: string;
48+
let totpTimestamp: Date;
4949
beforeEach(async () => {
5050
auth = getTestInstance();
5151
displayName = 'totp-integration-test';
@@ -80,10 +80,13 @@ describe(' Integration tests: Mfa TOTP', () => {
8080

8181
totpSecret = await TotpMultiFactorGenerator.generateSecret(session);
8282

83+
totpTimestamp = new Date();
84+
8385
const totpVerificationCode = getTotpCode(
8486
totpSecret.secretKey,
8587
totpSecret.codeIntervalSeconds,
86-
totpSecret.codeLength
88+
totpSecret.codeLength,
89+
totpTimestamp
8790
);
8891

8992
const multiFactorAssertion =
@@ -122,11 +125,6 @@ describe(' Integration tests: Mfa TOTP', () => {
122125

123126
it('should allow sign-in with for correct totp and unenroll successfully', async () => {
124127
let resolver;
125-
126-
await delay(30 * 1000);
127-
//TODO(bhparijat) generate the otp code for the next time window by passing the appropriate
128-
//timestamp to avoid the 30s delay. The delay is needed because the otp code used for enrollment
129-
//cannot be reused for signing in.
130128
try {
131129
await signInWithEmailAndPassword(auth, email, 'password');
132130

@@ -138,11 +136,15 @@ describe(' Integration tests: Mfa TOTP', () => {
138136
resolver = getMultiFactorResolver(auth, error as any);
139137
expect(resolver.hints).to.have.length(1);
140138

139+
totpTimestamp.setSeconds(totpTimestamp.getSeconds() + 30);
140+
141141
const totpVerificationCode = getTotpCode(
142142
totpSecret.secretKey,
143143
totpSecret.codeIntervalSeconds,
144-
totpSecret.codeLength
144+
totpSecret.codeLength,
145+
totpTimestamp
145146
);
147+
146148
const assertion = TotpMultiFactorGenerator.assertionForSignIn(
147149
resolver.hints[0].uid,
148150
totpVerificationCode
@@ -153,5 +155,5 @@ describe(' Integration tests: Mfa TOTP', () => {
153155

154156
await expect(mfaUser.unenroll(resolver.hints[0].uid)).to.be.fulfilled;
155157
}
156-
}).timeout(32000);
158+
});
157159
});

0 commit comments

Comments
 (0)