Skip to content

Commit 3969d79

Browse files
committed
cleaning formatting errors
1 parent a86fe98 commit 3969d79

File tree

2 files changed

+142
-138
lines changed

2 files changed

+142
-138
lines changed

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ import { Auth, User } from '../../../src/model/public_types';
2222
import { getAuth, connectAuthEmulator } from '../../../'; // Use browser OR node dist entrypoint depending on test env.
2323
import { _generateEventId } from '../../../src/core/util/event_id';
2424
import { getAppConfig, getEmulatorUrl } from './settings';
25-
import { getOobCodes, OobCodeSession, resetEmulator } from './emulator_rest_helpers';
25+
import {
26+
getOobCodes,
27+
OobCodeSession,
28+
resetEmulator
29+
} from './emulator_rest_helpers';
2630
import * as config from '../../../../../config/project.json';
2731

2832
//import * as otpauth from "https://deno.land/x/[email protected]/dist/otpauth.esm.js";
@@ -81,13 +85,15 @@ export function getTestInstance(requireEmulator = false): Auth {
8185
return auth;
8286
}
8387

84-
export async function cleanUpTestInstance(auth: Auth, tests? : string): Promise<void> {
88+
export async function cleanUpTestInstance(
89+
auth: Auth,
90+
tests?: string
91+
): Promise<void> {
8592
await auth.signOut();
8693

87-
if(typeof tests === 'undefined') {
94+
if (typeof tests === 'undefined') {
8895
await (auth as IntegrationTestAuth).cleanUp();
8996
}
90-
9197
}
9298

9399
function stubConsoleToSilenceEmulatorWarnings(): sinon.SinonStub {
@@ -108,19 +114,23 @@ export async function code(toEmail: string): Promise<OobCodeSession> {
108114
return codes.reverse().find(({ email }) => email === toEmail)!;
109115
}
110116

117+
export function getTotpCode(
118+
sharedSecretKey: string,
119+
periodSec: number,
120+
verificationCodeLength: number,
121+
hashingAlgorithm: string
122+
) {
123+
let token = totp(sharedSecretKey, {
124+
period: periodSec,
125+
digits: verificationCodeLength,
126+
algorithm: 'SHA-1'
127+
});
111128

112-
export function getTotpCode(sharedSecretKey: string, periodSec: number, verificationCodeLength: number, hashingAlgorithm: string){
113-
114-
let token = totp(sharedSecretKey, { period: periodSec, digits: verificationCodeLength, algorithm: 'SHA-1'});
115-
116-
117-
return token
118-
129+
return token;
119130
}
120131

121-
export function delay(dt:number){
122-
123-
return new Promise(resolve => setTimeout(resolve, dt));
132+
export function delay(dt: number) {
133+
return new Promise(resolve => setTimeout(resolve, dt));
124134
}
125135

126136
export const email = '[email protected]';

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

Lines changed: 118 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -15,171 +15,165 @@
1515
* limitations under the License.
1616
*/
1717

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';
2528
import {
2629
cleanUpTestInstance,
2730
getTestInstance,
2831
getTotpCode,
2932
delay,
3033
email
3134
} 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-
3735

36+
import {
37+
TotpMultiFactorGenerator,
38+
TotpSecret
39+
} from '../../../src/mfa/assertions/totp';
3840

3941
use(chaiAsPromised);
4042
use(sinonChai);
4143

4244
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;
5348
beforeEach(async () => {
5449
auth = getTestInstance();
5550
displayName = 'totp-integration-test';
5651
});
57-
52+
5853
afterEach(async () => {
5954
await cleanUpTestInstance(auth, 'totp');
60-
6155
});
6256

6357
it('should not enroll if incorrect totp supplied', async () => {
6458
let session;
65-
//await expect(createUserWithEmailAndPassword(auth, email, 'password')).to.be.rejectedWith('auth/email-already-in-use');
66-
6759
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
7868
);
7969

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');
8179
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');
8484

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);
8886

89-
const mfaUser = multiFactor(cr.user);
90-
91-
session = await mfaUser.getSession();
87+
session = await mfaUser.getSession();
9288

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+
);
9997

100-
const multiFactorAssertion = TotpMultiFactorGenerator.assertionForEnrollment(
98+
const multiFactorAssertion =
99+
TotpMultiFactorGenerator.assertionForEnrollment(
101100
totpSecret,
102101
totpVerificationCode
103102
);
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+
106139
await auth.signOut();
140+
}
141+
}).timeout(31000);
107142

108-
})
143+
it('should allow sign-in with for correct totp and unenroll successfully', async () => {
144+
let resolver;
109145

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
118148

119-
const userCredential = await signInWithEmailAndPassword(auth, email, 'password');
149+
try {
150+
await signInWithEmailAndPassword(auth, email, 'password');
120151

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');
122156

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);
124171

125-
126-
expect((error as any).code).to.eql('auth/multi-factor-auth-required');
172+
const mfaUser = multiFactor(userCredential.user);
127173

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;
130175

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();
183177
}
184-
}).timeout(35000);
185-
})
178+
}).timeout(35000);
179+
});

0 commit comments

Comments
 (0)