Skip to content

Commit 0c70e50

Browse files
committed
Include a reference to Auth in TotpSecret
This is cleaner than looking up the app and auth instance with getApp and getAuth.
1 parent 3e1ac5e commit 0c70e50

File tree

2 files changed

+16
-37
lines changed

2 files changed

+16
-37
lines changed

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

+8-25
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@ import {
3030
TotpMultiFactorGenerator,
3131
TotpSecret
3232
} from './totp';
33-
import { Auth, FactorId } from '../../model/public_types';
33+
import { FactorId } from '../../model/public_types';
3434
import { AuthErrorCode } from '../../core/errors';
35-
import { FirebaseApp, initializeApp } from '@firebase/app';
3635
import { AppName } from '../../model/auth';
37-
import { getAuth } from '../../platform_node';
38-
import { initializeAuth } from '../../core';
3936
import { _castAuth } from '../../core/auth/auth_impl';
4037

4138
use(chaiAsPromised);
@@ -58,7 +55,7 @@ describe('core/mfa/assertions/totp/TotpMultiFactorGenerator', () => {
5855
auth = await testAuth();
5956
const secret = TotpSecret.fromStartTotpMfaEnrollmentResponse(
6057
startEnrollmentResponse,
61-
auth.name
58+
auth
6259
);
6360
const assertion = TotpMultiFactorGenerator.assertionForEnrollment(
6461
secret,
@@ -90,9 +87,7 @@ describe('core/mfa/assertions/totp/TotpMultiFactorGenerator', () => {
9087
'enrollment-id-token',
9188
undefined
9289
);
93-
await TotpMultiFactorGenerator.generateSecret(
94-
session
95-
);
90+
await TotpMultiFactorGenerator.generateSecret(session);
9691
} catch (e) {
9792
expect(e.code).to.eql(`auth/${AuthErrorCode.INTERNAL_ERROR}`);
9893
}
@@ -156,7 +151,7 @@ describe('core/mfa/totp/assertions/TotpMultiFactorAssertionImpl', () => {
156151
auth = await testAuth();
157152
secret = TotpSecret.fromStartTotpMfaEnrollmentResponse(
158153
startEnrollmentResponse,
159-
auth.name
154+
auth
160155
);
161156
assertion = TotpMultiFactorAssertionImpl._fromSecret(secret, '123456');
162157
});
@@ -213,7 +208,7 @@ describe('core/mfa/totp/assertions/TotpMultiFactorAssertionImpl', () => {
213208
});
214209
});
215210

216-
describe('core/mfa/assertions/totp/TotpSecret', () => {
211+
describe('core/mfa/assertions/totp/TotpSecret', async () => {
217212
const serverResponse: StartTotpMfaEnrollmentResponse = {
218213
totpSessionInfo: {
219214
sharedSecretKey: 'key123',
@@ -224,11 +219,13 @@ describe('core/mfa/assertions/totp/TotpSecret', () => {
224219
finalizeEnrollmentTime: 1662586196
225220
}
226221
};
222+
// this is the name used by the fake app in testAuth().
227223
const fakeAppName: AppName = 'test-app';
228224
const fakeEmail: string = 'user@email';
225+
const auth = await testAuth();
229226
const secret = TotpSecret.fromStartTotpMfaEnrollmentResponse(
230227
serverResponse,
231-
fakeAppName
228+
auth
232229
);
233230

234231
describe('fromStartTotpMfaEnrollmentResponse', () => {
@@ -240,19 +237,7 @@ describe('core/mfa/assertions/totp/TotpSecret', () => {
240237
});
241238
});
242239
describe('generateQrCodeUrl', () => {
243-
let app: FirebaseApp;
244-
let auth: Auth;
245-
246240
beforeEach(async () => {
247-
app = initializeApp(
248-
{
249-
apiKey: 'fake-key',
250-
appId: 'fake-app-id',
251-
authDomain: 'fake-auth-domain'
252-
},
253-
fakeAppName
254-
);
255-
auth = initializeAuth(app);
256241
await auth.updateCurrentUser(
257242
testUser(_castAuth(auth), 'uid', fakeEmail, true)
258243
);
@@ -266,8 +251,6 @@ describe('core/mfa/assertions/totp/TotpSecret', () => {
266251
});
267252
it('only accountName provided', () => {
268253
const url = secret.generateQrCodeUrl('user@myawesomeapp', '');
269-
const auth2 = getAuth(app);
270-
console.log('Current user is ' + auth2);
271254
expect(url).to.eq(
272255
`otpauth://totp/${fakeAppName}:user@myawesomeapp?secret=key123&issuer=${fakeAppName}&algorithm=SHA1&digits=6`
273256
);

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

+8-12
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
MultiFactorSession,
2020
FactorId
2121
} from '../../model/public_types';
22-
import { AppName, AuthInternal } from '../../model/auth';
22+
import { AuthInternal } from '../../model/auth';
2323
import {
2424
finalizeEnrollTotpMfa,
2525
startEnrollTotpMfa,
@@ -31,8 +31,6 @@ import { MultiFactorAssertionImpl } from '../../mfa/mfa_assertion';
3131
import { MultiFactorSessionImpl } from '../mfa_session';
3232
import { AuthErrorCode } from '../../core/errors';
3333
import { _assert } from '../../core/util/assert';
34-
import { getApp } from '@firebase/app';
35-
import { getAuth } from '../../platform_node';
3634

3735
/**
3836
* Provider for generating a {@link TotpMultiFactorAssertion}.
@@ -93,13 +91,13 @@ export class TotpMultiFactorGenerator {
9391
typeof mfaSession.auth !== 'undefined',
9492
AuthErrorCode.INTERNAL_ERROR
9593
);
96-
const response = await startEnrollTotpMfa(mfaSession.auth!, {
94+
const response = await startEnrollTotpMfa(mfaSession.auth, {
9795
idToken: mfaSession.credential,
9896
totpEnrollmentInfo: {}
9997
});
10098
return TotpSecret.fromStartTotpMfaEnrollmentResponse(
10199
response,
102-
mfaSession.auth!.name
100+
mfaSession.auth
103101
);
104102
}
105103

@@ -193,12 +191,12 @@ export class TotpSecret {
193191
// This can be used by callers to show a countdown of when to enter OTP code by.
194192
private readonly finalizeEnrollmentBy: string,
195193
private readonly sessionInfo: string,
196-
private readonly appName: AppName
194+
private readonly auth: AuthInternal
197195
) {}
198196

199197
static fromStartTotpMfaEnrollmentResponse(
200198
response: StartTotpMfaEnrollmentResponse,
201-
appName: AppName
199+
auth: AuthInternal
202200
): TotpSecret {
203201
return new TotpSecret(
204202
response.totpSessionInfo.sharedSecretKey,
@@ -207,7 +205,7 @@ export class TotpSecret {
207205
response.totpSessionInfo.periodSec,
208206
new Date(response.totpSessionInfo.finalizeEnrollmentTime).toUTCString(),
209207
response.totpSessionInfo.sessionInfo,
210-
appName
208+
auth
211209
);
212210
}
213211

@@ -231,13 +229,11 @@ export class TotpSecret {
231229
useDefaults = true;
232230
}
233231
if (useDefaults) {
234-
const app = getApp(this.appName);
235-
const auth = getAuth(app);
236232
if (_isEmptyString(accountName)) {
237-
accountName = auth.currentUser?.email || 'unknownuser';
233+
accountName = this.auth.currentUser?.email || 'unknownuser';
238234
}
239235
if (_isEmptyString(issuer)) {
240-
issuer = app.name;
236+
issuer = this.auth.name;
241237
}
242238
}
243239
return `otpauth://totp/${issuer}:${accountName}?secret=${this.secretKey}&issuer=${issuer}&algorithm=${this.hashingAlgorithm}&digits=${this.codeLength}`;

0 commit comments

Comments
 (0)