Skip to content

Commit

Permalink
Include a reference to AuthInternal in MultiFactorSessionImpl. (#6594)
Browse files Browse the repository at this point in the history
* Include a reference to AuthInternal in MultiFactorSessionImpl.

This is needed for TOTP support where the function to generate TOTP Secret (by invoking startEnrollment API) needs the Auth reference, but rather than pass in a parameter, we can derive it from the multiFactorSession that is already passed in as a param. This simplifies the API for the developer, by requiring one less paramter.

* Added changeset.
  • Loading branch information
prameshj committed Sep 12, 2022
1 parent 7c0c640 commit e06d906
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-mirrors-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/auth': patch
---

Included a reference to AuthInternal in MultiFactorSessionImpl.
15 changes: 12 additions & 3 deletions packages/auth/src/mfa/mfa_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AuthInternal } from '../model/auth';
import { MultiFactorSession } from '../model/public_types';

export const enum MultiFactorSessionType {
Expand All @@ -31,11 +32,19 @@ interface SerializedMultiFactorSession {
export class MultiFactorSessionImpl implements MultiFactorSession {
private constructor(
readonly type: MultiFactorSessionType,
readonly credential: string
readonly credential: string,
readonly auth?: AuthInternal
) {}

static _fromIdtoken(idToken: string): MultiFactorSessionImpl {
return new MultiFactorSessionImpl(MultiFactorSessionType.ENROLL, idToken);
static _fromIdtoken(
idToken: string,
auth?: AuthInternal
): MultiFactorSessionImpl {
return new MultiFactorSessionImpl(
MultiFactorSessionType.ENROLL,
idToken,
auth
);
}

static _fromMfaPendingCredential(
Expand Down
6 changes: 6 additions & 0 deletions packages/auth/src/mfa/mfa_user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ describe('core/mfa/mfa_user/MultiFactorUser', () => {
expect(mfaSession.type).to.eq(MultiFactorSessionType.ENROLL);
expect(mfaSession.credential).to.eq('access-token');
});
it('should contain a reference to auth', async () => {
const mfaSession = (await mfaUser.getSession()) as MultiFactorSessionImpl;
expect(mfaSession.type).to.eq(MultiFactorSessionType.ENROLL);
expect(mfaSession.credential).to.eq('access-token');
expect(mfaSession.auth).to.eq(auth);
});
});

describe('enroll', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/auth/src/mfa/mfa_user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export class MultiFactorUserImpl implements MultiFactorUser {
}

async getSession(): Promise<MultiFactorSession> {
return MultiFactorSessionImpl._fromIdtoken(await this.user.getIdToken());
return MultiFactorSessionImpl._fromIdtoken(
await this.user.getIdToken(),
this.user.auth
);
}

async enroll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ describe('platform_browser/mfa/phone', () => {

describe('enroll', () => {
beforeEach(() => {
session = MultiFactorSessionImpl._fromIdtoken('enrollment-id-token');
session = MultiFactorSessionImpl._fromIdtoken(
'enrollment-id-token',
auth
);
});

it('should finalize the MFA enrollment', async () => {
Expand All @@ -75,6 +78,7 @@ describe('platform_browser/mfa/phone', () => {
sessionInfo: 'verification-id'
}
});
expect(session.auth).to.eql(auth);
});

context('with display name', () => {
Expand All @@ -97,6 +101,7 @@ describe('platform_browser/mfa/phone', () => {
sessionInfo: 'verification-id'
}
});
expect(session.auth).to.eql(auth);
});
});
});
Expand All @@ -119,6 +124,7 @@ describe('platform_browser/mfa/phone', () => {
sessionInfo: 'verification-id'
}
});
expect(session.auth).to.eql(undefined);
});
});
});
Expand Down

0 comments on commit e06d906

Please sign in to comment.