|
1 | 1 | import { ConnectorType } from '@logto/connector-kit'; |
2 | | -import { InteractionEvent, MfaFactor, MfaPolicy, SignInIdentifier } from '@logto/schemas'; |
| 2 | +import { |
| 3 | + AlternativeSignUpIdentifier, |
| 4 | + InteractionEvent, |
| 5 | + MfaFactor, |
| 6 | + MfaPolicy, |
| 7 | + SignInIdentifier, |
| 8 | +} from '@logto/schemas'; |
3 | 9 | import { authenticator } from 'otplib'; |
4 | 10 |
|
5 | 11 | import { deleteUser } from '#src/api/admin-user.js'; |
@@ -214,6 +220,75 @@ describe('Register interaction - optional additional MFA suggestion', () => { |
214 | 220 | await deleteUser(userId); |
215 | 221 | }); |
216 | 222 |
|
| 223 | + it('should suggest additional MFA when email or phone is required as a secondary identifier', async () => { |
| 224 | + await updateSignInExperience({ |
| 225 | + signUp: { |
| 226 | + identifiers: [SignInIdentifier.Username], |
| 227 | + password: true, |
| 228 | + verify: true, |
| 229 | + secondaryIdentifiers: [ |
| 230 | + { |
| 231 | + identifier: AlternativeSignUpIdentifier.EmailOrPhone, |
| 232 | + verify: true, |
| 233 | + }, |
| 234 | + ], |
| 235 | + }, |
| 236 | + signIn: { |
| 237 | + methods: [ |
| 238 | + { |
| 239 | + identifier: SignInIdentifier.Username, |
| 240 | + password: true, |
| 241 | + verificationCode: false, |
| 242 | + isPasswordPrimary: false, |
| 243 | + }, |
| 244 | + ], |
| 245 | + }, |
| 246 | + mfa: { |
| 247 | + factors: [MfaFactor.EmailVerificationCode, MfaFactor.TOTP], |
| 248 | + policy: MfaPolicy.Mandatory, |
| 249 | + }, |
| 250 | + }); |
| 251 | + |
| 252 | + const { username, password, primaryEmail } = generateNewUserProfile({ |
| 253 | + username: true, |
| 254 | + password: true, |
| 255 | + primaryEmail: true, |
| 256 | + }); |
| 257 | + |
| 258 | + const client = await initExperienceClient({ interactionEvent: InteractionEvent.Register }); |
| 259 | + |
| 260 | + await client.updateProfile({ type: SignInIdentifier.Username, value: username }); |
| 261 | + await client.updateProfile({ type: 'password', value: password }); |
| 262 | + |
| 263 | + await fulfillUserEmail(client, primaryEmail); |
| 264 | + |
| 265 | + await client.identifyUser(); |
| 266 | + |
| 267 | + await expectRejects<{ |
| 268 | + availableFactors: MfaFactor[]; |
| 269 | + skippable: boolean; |
| 270 | + maskedIdentifiers?: Record<string, string>; |
| 271 | + suggestion?: boolean; |
| 272 | + }>(client.submitInteraction(), { |
| 273 | + code: 'session.mfa.suggest_additional_mfa', |
| 274 | + status: 422, |
| 275 | + expectData: (data) => { |
| 276 | + expect(data.availableFactors).toEqual([MfaFactor.TOTP, MfaFactor.EmailVerificationCode]); |
| 277 | + expect(data.maskedIdentifiers).toBeDefined(); |
| 278 | + expect(data.maskedIdentifiers?.[MfaFactor.EmailVerificationCode]).toMatch(/\*{4}/); |
| 279 | + expect(data.skippable).toBe(true); |
| 280 | + expect(data.suggestion).toBe(true); |
| 281 | + }, |
| 282 | + }); |
| 283 | + |
| 284 | + await client.skipMfaSuggestion(); |
| 285 | + |
| 286 | + const { redirectTo } = await client.submitInteraction(); |
| 287 | + const userId = await processSession(client, redirectTo); |
| 288 | + await logoutClient(client); |
| 289 | + await deleteUser(userId); |
| 290 | + }); |
| 291 | + |
217 | 292 | it('should not suggest MFA after fulfilling phone verification when both email and SMS factors are enabled', async () => { |
218 | 293 | // Configure MFA with email, phone, and TOTP factors |
219 | 294 | await updateSignInExperience({ |
|
0 commit comments