Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(core): add the mfa binding integration tests #6330

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/core/src/routes/experience/classes/mfa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class Mfa {
* @throws {RequestError} with status 400 if the verification record is not verified
* @throws {RequestError} with status 400 if the verification record has no secret
* @throws {RequestError} with status 404 if the verification record is not found
* @throws {RequestError} with status 422 if TOTP is not enabled in the sign-in experience
* @throws {RequestError} with status 400 if TOTP is not enabled in the sign-in experience
* @throws {RequestError} with status 422 if the user already has a TOTP factor
*
* - Any existing TOTP factor will be replaced with the new one.
Expand Down Expand Up @@ -196,7 +196,7 @@ export class Mfa {
* @throws {RequestError} with status 400 if the verification record is not verified
* @throws {RequestError} with status 400 if the verification record has no registration data
* @throws {RequestError} with status 404 if the verification record is not found
* @throws {RequestError} with status 422 if WebAuthn is not enabled in the sign-in experience
* @throws {RequestError} with status 400 if WebAuthn is not enabled in the sign-in experience
*/
async addWebAuthnByVerificationId(verificationId: string) {
const verificationRecord = this.interactionContext.getVerificationRecordByTypeAndId(
Expand All @@ -215,7 +215,7 @@ export class Mfa {
* - Any existing backup code factor will be replaced with the new one.
*
* @throws {RequestError} with status 404 if no pending backup codes are found
* @throws {RequestError} with status 422 if Backup Code is not enabled in the sign-in experience
* @throws {RequestError} with status 400 if Backup Code is not enabled in the sign-in experience
* @throws {RequestError} with status 422 if the backup code is the only MFA factor
*/
async addBackupCodeByVerificationId(verificationId: string) {
Expand All @@ -241,7 +241,7 @@ export class Mfa {
}

/**
* @throws {RequestError} with status 422 if the mfa factors are not enabled in the sign-in experience
* @throws {RequestError} with status 400 if the mfa factors are not enabled in the sign-in experience
*/
async checkAvailability() {
const newBindMfaFactors = deduplicate(this.bindMfaFactorsArray.map(({ type }) => type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,41 @@
}
);

router.post(
`${experienceRoutes.verification}/backup-code/generate`,
koaGuard({
status: [200, 400],
response: z.object({
verificationId: z.string(),
codes: z.array(z.string()),
}),
}),
async (ctx, next) => {
const { experienceInteraction } = ctx;

assertThat(experienceInteraction.identifiedUserId, 'session.identifier_not_found');

const backupCodeVerificationRecord = BackupCodeVerification.create(
libraries,
queries,
experienceInteraction.identifiedUserId
);

const codes = backupCodeVerificationRecord.generate();

ctx.experienceInteraction.setVerificationRecord(backupCodeVerificationRecord);

await ctx.experienceInteraction.save();

ctx.body = {
verificationId: backupCodeVerificationRecord.id,
codes,
};

return next();
}

Check warning on line 87 in packages/core/src/routes/experience/verification-routes/backup-code-verification.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/experience/verification-routes/backup-code-verification.ts#L65-L87

Added lines #L65 - L87 were not covered by tests
);

router.post(
`${experienceRoutes.verification}/backup-code/verify`,
koaGuard({
Expand Down
1 change: 1 addition & 0 deletions packages/integration-tests/src/client/experience/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export const experienceRoutes = {
verification: `${prefix}/verification`,
identification: `${prefix}/identification`,
profile: `${prefix}/profile`,
mfa: `${prefix}/profile/mfa`,
prefix,
};
22 changes: 22 additions & 0 deletions packages/integration-tests/src/client/experience/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
type CreateExperienceApiPayload,
type IdentificationApiPayload,
type InteractionEvent,
type MfaFactor,
type NewPasswordIdentityVerificationPayload,
type PasswordVerificationPayload,
type UpdateProfileApiPayload,
Expand Down Expand Up @@ -178,6 +179,14 @@ export class ExperienceClient extends MockClient {
.json<{ verificationId: string }>();
}

public async generateMfaBackupCodes() {
return api
.post(`${experienceRoutes.verification}/backup-code/generate`, {
headers: { cookie: this.interactionCookie },
})
.json<{ verificationId: string; codes: string[] }>();
}

public async verifyBackupCode(payload: { code: string }) {
return api
.post(`${experienceRoutes.verification}/backup-code/verify`, {
Expand Down Expand Up @@ -211,4 +220,17 @@ export class ExperienceClient extends MockClient {
json: payload,
});
}

public async skipMfaBinding() {
return api.post(`${experienceRoutes.mfa}/mfa-skipped`, {
headers: { cookie: this.interactionCookie },
});
}

public async bindMfa(type: MfaFactor, verificationId: string) {
return api.post(`${experienceRoutes.mfa}`, {
headers: { cookie: this.interactionCookie },
json: { type, verificationId },
});
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { authenticator } from 'otplib';

import { type ExperienceClient } from '#src/client/experience/index.js';

export const successFullyCreateNewTotpSecret = async (client: ExperienceClient) => {
Expand All @@ -23,3 +25,15 @@ export const successfullyVerifyTotp = async (

return verificationId;
};

export const successfullyCreateAndVerifyTotp = async (client: ExperienceClient) => {
const { secret, verificationId } = await successFullyCreateNewTotpSecret(client);
const code = authenticator.generate(secret);

await successfullyVerifyTotp(client, {
code,
verificationId,
});

return verificationId;
};
Loading
Loading