Skip to content

Commit

Permalink
Include a reference to AuthInternal in MultiFactorSessionImpl.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
prameshj committed Sep 9, 2022
1 parent 87c90a7 commit 0f84366
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
8 changes: 5 additions & 3 deletions packages/auth/src/mfa/mfa_session.ts
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,12 @@ 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
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
2 changes: 1 addition & 1 deletion packages/auth/src/mfa/mfa_user.ts
Expand Up @@ -49,7 +49,7 @@ 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
Expand Up @@ -58,7 +58,7 @@ 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 +75,7 @@ describe('platform_browser/mfa/phone', () => {
sessionInfo: 'verification-id'
}
});
expect(session.auth).to.eql(auth);
});

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

0 comments on commit 0f84366

Please sign in to comment.