|
| 1 | +import { ConnectorType } from '@logto/connector-kit'; |
| 2 | +import { InteractionEvent, SignInIdentifier } from '@logto/schemas'; |
| 3 | + |
| 4 | +import { deleteUser } from '#src/api/admin-user.js'; |
| 5 | +import { updateSignInExperience } from '#src/api/sign-in-experience.js'; |
| 6 | +import { initExperienceClient, logoutClient, processSession } from '#src/helpers/client.js'; |
| 7 | +import { clearConnectorsByTypes, setEmailConnector } from '#src/helpers/connector.js'; |
| 8 | +import { |
| 9 | + successfullySendMfaVerificationCode, |
| 10 | + successfullySendVerificationCode, |
| 11 | + successfullyVerifyMfaVerificationCode, |
| 12 | + successfullyVerifyVerificationCode, |
| 13 | +} from '#src/helpers/experience/verification-code.js'; |
| 14 | +import { enableMandatoryMfaWithEmail, resetMfaSettings } from '#src/helpers/sign-in-experience.js'; |
| 15 | +import { devFeatureTest, generateEmail, generatePassword } from '#src/utils.js'; |
| 16 | + |
| 17 | +const { describe, it } = devFeatureTest; |
| 18 | + |
| 19 | +describe('Register with email identifier and bind as email MFA automaticly', () => { |
| 20 | + beforeAll(async () => { |
| 21 | + // Use only email connector and allow email sign-up via verification code |
| 22 | + await clearConnectorsByTypes([ConnectorType.Email, ConnectorType.Sms]); |
| 23 | + await setEmailConnector(); |
| 24 | + await updateSignInExperience({ |
| 25 | + signUp: { |
| 26 | + identifiers: [SignInIdentifier.Email], |
| 27 | + password: true, |
| 28 | + verify: true, |
| 29 | + }, |
| 30 | + signIn: { |
| 31 | + methods: [ |
| 32 | + { |
| 33 | + identifier: SignInIdentifier.Email, |
| 34 | + verificationCode: false, |
| 35 | + password: true, |
| 36 | + isPasswordPrimary: false, |
| 37 | + }, |
| 38 | + ], |
| 39 | + }, |
| 40 | + forgotPasswordMethods: [], |
| 41 | + }); |
| 42 | + await enableMandatoryMfaWithEmail(); |
| 43 | + }); |
| 44 | + |
| 45 | + afterAll(async () => { |
| 46 | + await clearConnectorsByTypes([ConnectorType.Email, ConnectorType.Sms]); |
| 47 | + await resetMfaSettings(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('treats MFA step as email factor when sign-up identifier is email', async () => { |
| 51 | + const email = generateEmail(); |
| 52 | + const password = generatePassword(); |
| 53 | + |
| 54 | + // Start register interaction and verify email identifier |
| 55 | + const client = await initExperienceClient({ interactionEvent: InteractionEvent.Register }); |
| 56 | + const { verificationId, code } = await successfullySendVerificationCode(client, { |
| 57 | + identifier: { type: SignInIdentifier.Email, value: email }, |
| 58 | + interactionEvent: InteractionEvent.Register, |
| 59 | + }); |
| 60 | + const verifiedId = await successfullyVerifyVerificationCode(client, { |
| 61 | + identifier: { type: SignInIdentifier.Email, value: email }, |
| 62 | + verificationId, |
| 63 | + code, |
| 64 | + }); |
| 65 | + await client.updateProfile({ type: 'password', value: password }); |
| 66 | + |
| 67 | + // Identify the user (complete sign-up identifier step) |
| 68 | + await client.identifyUser({ verificationId: verifiedId }); |
| 69 | + |
| 70 | + // Now MFA is mandatory with Email factor; send and verify MFA code using email factor |
| 71 | + const { verificationId: mfaVerificationId, code: mfaCode } = |
| 72 | + await successfullySendMfaVerificationCode(client, { |
| 73 | + identifierType: SignInIdentifier.Email, |
| 74 | + expectedIdentifierValue: email, |
| 75 | + }); |
| 76 | + |
| 77 | + await successfullyVerifyMfaVerificationCode(client, { |
| 78 | + identifierType: SignInIdentifier.Email, |
| 79 | + verificationId: mfaVerificationId, |
| 80 | + code: mfaCode, |
| 81 | + }); |
| 82 | + |
| 83 | + // Finish interaction and clean up created user |
| 84 | + const { redirectTo } = await client.submitInteraction(); |
| 85 | + const userId = await processSession(client, redirectTo); |
| 86 | + await logoutClient(client); |
| 87 | + await deleteUser(userId); |
| 88 | + }); |
| 89 | +}); |
0 commit comments