Skip to content

Commit f949ee9

Browse files
Cleanup TOTP code to include Firebase User reference in MultiFactorSession. (#7346)
* cleanup TOTP code to include Firebase user reference in MultiFactorSession * removed helper comments * Added fakeUid const to organize code * edited test to match changes in MultiFactorSession * resolved TestAuth incompatible type error * resolved more TestAuth incompatible type error * resubmitting * fixed formatting & resolved issue with undefined * fixed format issue * fixed formatting * tried to resolve issue with user being undefined * fixed if statement formatting * addressed feedback: changed expect to compare user * fixed formatting * fixed the issue with undefined * instantiate user * organized import statements * changed import formatting * resubmitting for import formatting issue * ran yarn format
1 parent 38ab766 commit f949ee9

File tree

6 files changed

+34
-20
lines changed

6 files changed

+34
-20
lines changed

packages/auth/src/mfa/assertions/totp.test.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import { MultiFactorAssertionImpl } from '../mfa_assertion';
3838

3939
use(chaiAsPromised);
4040

41+
const fakeUid: string = 'uid';
42+
4143
describe('core/mfa/assertions/totp/TotpMultiFactorGenerator', () => {
4244
let auth: TestAuth;
4345
let session: MultiFactorSessionImpl;
@@ -82,7 +84,7 @@ describe('core/mfa/assertions/totp/TotpMultiFactorGenerator', () => {
8284
});
8385
afterEach(mockFetch.tearDown);
8486

85-
it('should throw error if auth instance is not found in mfaSession', async () => {
87+
it('should throw error if user instance is not found in mfaSession', async () => {
8688
try {
8789
session = MultiFactorSessionImpl._fromIdtoken(
8890
'enrollment-id-token',
@@ -100,9 +102,10 @@ describe('core/mfa/assertions/totp/TotpMultiFactorGenerator', () => {
100102
);
101103

102104
auth = await testAuth();
105+
const user = await testUser(auth, fakeUid);
103106
session = MultiFactorSessionImpl._fromIdtoken(
104107
'enrollment-id-token',
105-
auth
108+
user
106109
);
107110
const secret = await TotpMultiFactorGenerator.generateSecret(session);
108111
expect(mock.calls[0].request).to.eql({
@@ -159,10 +162,11 @@ describe('core/mfa/totp/assertions/TotpMultiFactorAssertionImpl', () => {
159162
afterEach(mockFetch.tearDown);
160163

161164
describe('enroll', () => {
165+
const user = testUser(auth, fakeUid);
162166
beforeEach(() => {
163167
session = MultiFactorSessionImpl._fromIdtoken(
164168
'enrollment-id-token',
165-
auth
169+
user
166170
);
167171
});
168172

@@ -180,7 +184,8 @@ describe('core/mfa/totp/assertions/TotpMultiFactorAssertionImpl', () => {
180184
sessionInfo: 'verification-id'
181185
}
182186
});
183-
expect(session.auth).to.eql(auth);
187+
expect(session.user).to.not.be.undefined;
188+
expect(session.user).to.eql(user);
184189
});
185190

186191
context('with display name', () => {
@@ -203,7 +208,8 @@ describe('core/mfa/totp/assertions/TotpMultiFactorAssertionImpl', () => {
203208
sessionInfo: 'verification-id'
204209
}
205210
});
206-
expect(session.auth).to.eql(auth);
211+
expect(session.user).to.not.be.undefined;
212+
expect(session.user).to.eql(user);
207213
});
208214
});
209215
});
@@ -299,7 +305,7 @@ describe('core/mfa/assertions/totp/TotpSecret', async () => {
299305
describe('generateQrCodeUrl', () => {
300306
beforeEach(async () => {
301307
await auth.updateCurrentUser(
302-
testUser(_castAuth(auth), 'uid', fakeEmail, true)
308+
testUser(_castAuth(auth), fakeUid, fakeEmail, true)
303309
);
304310
});
305311

packages/auth/src/mfa/assertions/totp.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,16 @@ export class TotpMultiFactorGenerator {
9191
): Promise<TotpSecret> {
9292
const mfaSession = session as MultiFactorSessionImpl;
9393
_assert(
94-
typeof mfaSession.auth !== 'undefined',
94+
typeof mfaSession.user?.auth !== 'undefined',
9595
AuthErrorCode.INTERNAL_ERROR
9696
);
97-
const response = await startEnrollTotpMfa(mfaSession.auth, {
97+
const response = await startEnrollTotpMfa(mfaSession.user.auth, {
9898
idToken: mfaSession.credential,
9999
totpEnrollmentInfo: {}
100100
});
101101
return TotpSecret._fromStartTotpMfaEnrollmentResponse(
102102
response,
103-
mfaSession.auth
103+
mfaSession.user.auth
104104
);
105105
}
106106

packages/auth/src/mfa/mfa_session.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
import { AuthInternal } from '../model/auth';
17+
18+
import { UserInternal } from '../model/user';
1819
import { MultiFactorSession } from '../model/public_types';
1920

2021
export const enum MultiFactorSessionType {
@@ -33,17 +34,17 @@ export class MultiFactorSessionImpl implements MultiFactorSession {
3334
private constructor(
3435
readonly type: MultiFactorSessionType,
3536
readonly credential: string,
36-
readonly auth?: AuthInternal
37+
readonly user?: UserInternal
3738
) {}
3839

3940
static _fromIdtoken(
4041
idToken: string,
41-
auth?: AuthInternal
42+
user?: UserInternal
4243
): MultiFactorSessionImpl {
4344
return new MultiFactorSessionImpl(
4445
MultiFactorSessionType.ENROLL,
4546
idToken,
46-
auth
47+
user
4748
);
4849
}
4950

packages/auth/src/mfa/mfa_user.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('core/mfa/mfa_user/MultiFactorUser', () => {
8888
const mfaSession = (await mfaUser.getSession()) as MultiFactorSessionImpl;
8989
expect(mfaSession.type).to.eq(MultiFactorSessionType.ENROLL);
9090
expect(mfaSession.credential).to.eq('access-token');
91-
expect(mfaSession.auth).to.eq(auth);
91+
expect(mfaSession.user?.auth).to.eq(auth);
9292
});
9393
});
9494

packages/auth/src/mfa/mfa_user.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class MultiFactorUserImpl implements MultiFactorUser {
5050
async getSession(): Promise<MultiFactorSession> {
5151
return MultiFactorSessionImpl._fromIdtoken(
5252
await this.user.getIdToken(),
53-
this.user.auth
53+
this.user
5454
);
5555
}
5656

packages/auth/src/platform_browser/mfa/assertions/phone.test.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ import { expect, use } from 'chai';
2020
import chaiAsPromised from 'chai-as-promised';
2121

2222
import { mockEndpoint } from '../../../../test/helpers/api/helper';
23-
import { testAuth, TestAuth } from '../../../../test/helpers/mock_auth';
23+
import {
24+
testAuth,
25+
TestAuth,
26+
testUser
27+
} from '../../../../test/helpers/mock_auth';
2428
import * as mockFetch from '../../../../test/helpers/mock_fetch';
2529
import { Endpoint } from '../../../api';
2630
import { FinalizeMfaResponse } from '../../../api/authentication/mfa';
@@ -57,10 +61,11 @@ describe('platform_browser/mfa/phone', () => {
5761
afterEach(mockFetch.tearDown);
5862

5963
describe('enroll', () => {
64+
const user = testUser(auth, 'uid');
6065
beforeEach(() => {
6166
session = MultiFactorSessionImpl._fromIdtoken(
6267
'enrollment-id-token',
63-
auth
68+
user
6469
);
6570
});
6671

@@ -78,7 +83,8 @@ describe('platform_browser/mfa/phone', () => {
7883
sessionInfo: 'verification-id'
7984
}
8085
});
81-
expect(session.auth).to.eql(auth);
86+
expect(session.user).to.not.be.undefined;
87+
expect(session.user).to.eql(user);
8288
});
8389

8490
context('with display name', () => {
@@ -101,7 +107,8 @@ describe('platform_browser/mfa/phone', () => {
101107
sessionInfo: 'verification-id'
102108
}
103109
});
104-
expect(session.auth).to.eql(auth);
110+
expect(session.user).to.not.be.undefined;
111+
expect(session.user).to.eql(user);
105112
});
106113
});
107114
});
@@ -124,7 +131,7 @@ describe('platform_browser/mfa/phone', () => {
124131
sessionInfo: 'verification-id'
125132
}
126133
});
127-
expect(session.auth).to.eql(undefined);
134+
expect(session.user).to.be.undefined;
128135
});
129136
});
130137
});

0 commit comments

Comments
 (0)