Skip to content

Commit d79f71d

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 d79f71d

File tree

2 files changed

+14
-32
lines changed

2 files changed

+14
-32
lines changed

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

Lines changed: 7 additions & 21 deletions
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,
@@ -156,7 +153,7 @@ describe('core/mfa/totp/assertions/TotpMultiFactorAssertionImpl', () => {
156153
auth = await testAuth();
157154
secret = TotpSecret.fromStartTotpMfaEnrollmentResponse(
158155
startEnrollmentResponse,
159-
auth.name
156+
auth,
160157
);
161158
assertion = TotpMultiFactorAssertionImpl._fromSecret(secret, '123456');
162159
});
@@ -213,7 +210,7 @@ describe('core/mfa/totp/assertions/TotpMultiFactorAssertionImpl', () => {
213210
});
214211
});
215212

216-
describe('core/mfa/assertions/totp/TotpSecret', () => {
213+
describe('core/mfa/assertions/totp/TotpSecret', async () => {
217214
const serverResponse: StartTotpMfaEnrollmentResponse = {
218215
totpSessionInfo: {
219216
sharedSecretKey: 'key123',
@@ -224,11 +221,13 @@ describe('core/mfa/assertions/totp/TotpSecret', () => {
224221
finalizeEnrollmentTime: 1662586196
225222
}
226223
};
224+
// this is the name used by the fake app in testAuth().
227225
const fakeAppName: AppName = 'test-app';
228226
const fakeEmail: string = 'user@email';
227+
let auth = await testAuth();
229228
const secret = TotpSecret.fromStartTotpMfaEnrollmentResponse(
230229
serverResponse,
231-
fakeAppName
230+
auth,
232231
);
233232

234233
describe('fromStartTotpMfaEnrollmentResponse', () => {
@@ -240,19 +239,8 @@ describe('core/mfa/assertions/totp/TotpSecret', () => {
240239
});
241240
});
242241
describe('generateQrCodeUrl', () => {
243-
let app: FirebaseApp;
244-
let auth: Auth;
245242

246243
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);
256244
await auth.updateCurrentUser(
257245
testUser(_castAuth(auth), 'uid', fakeEmail, true)
258246
);
@@ -266,8 +254,6 @@ describe('core/mfa/assertions/totp/TotpSecret', () => {
266254
});
267255
it('only accountName provided', () => {
268256
const url = secret.generateQrCodeUrl('user@myawesomeapp', '');
269-
const auth2 = getAuth(app);
270-
console.log('Current user is ' + auth2);
271257
expect(url).to.eq(
272258
`otpauth://totp/${fakeAppName}:user@myawesomeapp?secret=key123&issuer=${fakeAppName}&algorithm=SHA1&digits=6`
273259
);

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

Lines changed: 7 additions & 11 deletions
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}.
@@ -99,7 +97,7 @@ export class TotpMultiFactorGenerator {
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)