Skip to content

Commit

Permalink
refactor(core): rename the sign-in-experience-settings class
Browse files Browse the repository at this point in the history
rename the sign-in-experience-settings class
  • Loading branch information
simeng-li committed Jul 12, 2024
1 parent 68742cb commit 0785343
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import assertThat from '#src/utils/assert-that.js';

import type { Interaction } from '../types.js';

import { SignInExperienceSettings } from './sign-in-experience-settings.js';
import { SignInExperienceValidator } from './validators/sign-in-experience-validator.js';
import {
buildVerificationRecord,
verificationRecordDataGuard,
Expand Down Expand Up @@ -38,6 +38,8 @@ const interactionStorageGuard = z.object({
* @see {@link https://github.com/logto-io/rfcs | Logto RFCs} for more information about RFC 0004.
*/
export default class ExperienceInteraction {
// Interaction data

/** The interaction event for the current interaction. */
private _interactionEvent?: InteractionEvent;
/** The user verification record list for the current interaction. */
Expand All @@ -46,7 +48,10 @@ export default class ExperienceInteraction {
private userId?: string;
/** The user provided profile data in the current interaction that needs to be stored to database. */
private readonly profile?: Record<string, unknown>; // TODO: Fix the type
private readonly signInExperienceSettings: SignInExperienceSettings;

// Validators

private readonly signInExperienceValidator: SignInExperienceValidator;

/**
* Create a new `ExperienceInteraction` instance.
Expand All @@ -61,7 +66,7 @@ export default class ExperienceInteraction {
) {
const { libraries, queries } = tenant;

this.signInExperienceSettings = new SignInExperienceSettings(libraries, queries);
this.signInExperienceValidator = new SignInExperienceValidator(libraries, queries);

Check warning on line 70 in packages/core/src/routes/experience/classes/experience-interaction.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/experience/classes/experience-interaction.ts#L69-L70

Added lines #L69 - L70 were not covered by tests
if (!interactionDetails) {
return;
Expand Down Expand Up @@ -98,12 +103,12 @@ export default class ExperienceInteraction {
/**
* Set the interaction event for the current interaction
*
* @throws RequestError with 403 if the interaction event is not allowed by the `SignInExperienceSettings`
* @throws RequestError with 403 if the interaction event is not allowed by the `SignInExperienceValidator`
* @throws RequestError with 400 if the interaction event is `ForgotPassword` and the current interaction event is not `ForgotPassword`
* @throws RequestError with 400 if the interaction event is not `ForgotPassword` and the current interaction event is `ForgotPassword`
*/
public async setInteractionEvent(interactionEvent: InteractionEvent) {
await this.signInExperienceSettings.guardInteractionEvent(interactionEvent);
await this.signInExperienceValidator.guardInteractionEvent(interactionEvent);

// `ForgotPassword` interaction event can not interchanged with other events
if (this.interactionEvent) {
Expand All @@ -122,7 +127,7 @@ export default class ExperienceInteraction {
* Identify the user using the verification record.
*
* - Check if the verification record exists.
* - Verify the verification record with `SignInExperienceSettings`.
* - Verify the verification record with `SignInExperienceValidator`.
* - Create a new user using the verification record if the current interaction event is `Register`.
* - Identify the user using the verification record if the current interaction event is `SignIn` or `ForgotPassword`.
* - Set the user id to the current interaction.
Expand All @@ -146,7 +151,7 @@ export default class ExperienceInteraction {
new RequestError({ code: 'session.verification_session_not_found', status: 404 })
);

await this.signInExperienceSettings.verifyIdentificationMethod(
await this.signInExperienceValidator.verifyIdentificationMethod(
this.interactionEvent,
verificationRecord
);

Check warning on line 157 in packages/core/src/routes/experience/classes/experience-interaction.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/experience/classes/experience-interaction.ts#L154-L157

Added lines #L154 - L157 were not covered by tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import { mockSignInExperience } from '#src/__mocks__/sign-in-experience.js';
import RequestError from '#src/errors/RequestError/index.js';
import { MockTenant } from '#src/test-utils/tenant.js';

import { SignInExperienceSettings } from './sign-in-experience-settings.js';
import { CodeVerification } from './verifications/code-verification.js';
import { EnterpriseSsoVerification } from './verifications/enterprise-sso-verification.js';
import { type VerificationRecord } from './verifications/index.js';
import { PasswordVerification } from './verifications/password-verification.js';
import { SocialVerification } from './verifications/social-verification.js';
import { CodeVerification } from '../verifications/code-verification.js';
import { EnterpriseSsoVerification } from '../verifications/enterprise-sso-verification.js';
import { type VerificationRecord } from '../verifications/index.js';
import { PasswordVerification } from '../verifications/password-verification.js';
import { SocialVerification } from '../verifications/social-verification.js';

import { SignInExperienceValidator } from './sign-in-experience-validator.js';

const { jest } = import.meta;

Expand Down Expand Up @@ -78,15 +79,15 @@ const socialVerificationRecord = new SocialVerification(mockTenant.libraries, mo
},
});

describe('SignInExperienceSettings', () => {
describe('SignInExperienceValidator', () => {
describe('guardInteractionEvent', () => {
it('SignInMode.Register', async () => {
const signInExperience = {
signInMode: SignInMode.Register,
};
signInExperiences.findDefaultSignInExperience.mockResolvedValueOnce(signInExperience);

const signInExperienceSettings = new SignInExperienceSettings(
const signInExperienceSettings = new SignInExperienceValidator(
mockTenant.libraries,
mockTenant.queries
);
Expand All @@ -110,7 +111,7 @@ describe('SignInExperienceSettings', () => {
};
signInExperiences.findDefaultSignInExperience.mockResolvedValueOnce(signInExperience);

const signInExperienceSettings = new SignInExperienceSettings(
const signInExperienceSettings = new SignInExperienceValidator(
mockTenant.libraries,
mockTenant.queries
);
Expand All @@ -133,7 +134,7 @@ describe('SignInExperienceSettings', () => {
};
signInExperiences.findDefaultSignInExperience.mockResolvedValueOnce(signInExperience);

const signInExperienceSettings = new SignInExperienceSettings(
const signInExperienceSettings = new SignInExperienceValidator(
mockTenant.libraries,
mockTenant.queries
);
Expand Down Expand Up @@ -290,7 +291,7 @@ describe('SignInExperienceSettings', () => {
it.each(cases)('guard verification record %p', async ({ verificationRecord, accepted }) => {
signInExperiences.findDefaultSignInExperience.mockResolvedValueOnce(signInExperience);

const signInExperienceSettings = new SignInExperienceSettings(
const signInExperienceSettings = new SignInExperienceValidator(
mockTenant.libraries,
mockTenant.queries
);
Expand Down Expand Up @@ -408,7 +409,7 @@ describe('SignInExperienceSettings', () => {
it.each(cases)('guard verification record %p', async ({ verificationRecord, accepted }) => {
signInExperiences.findDefaultSignInExperience.mockResolvedValueOnce(signInExperience);

const signInExperienceSettings = new SignInExperienceSettings(
const signInExperienceSettings = new SignInExperienceValidator(
mockTenant.libraries,
mockTenant.queries
);
Expand Down Expand Up @@ -450,7 +451,7 @@ describe('SignInExperienceSettings', () => {
it('email password verification record', async () => {
ssoConnectors.getAvailableSsoConnectors.mockResolvedValueOnce([mockSsoConnector]);

const signInExperienceSettings = new SignInExperienceSettings(
const signInExperienceSettings = new SignInExperienceValidator(
mockTenant.libraries,
mockTenant.queries
);
Expand All @@ -466,7 +467,7 @@ describe('SignInExperienceSettings', () => {
it('email verification code verification record', async () => {
ssoConnectors.getAvailableSsoConnectors.mockResolvedValueOnce([mockSsoConnector]);

const signInExperienceSettings = new SignInExperienceSettings(
const signInExperienceSettings = new SignInExperienceValidator(
mockTenant.libraries,
mockTenant.queries
);
Expand All @@ -482,7 +483,7 @@ describe('SignInExperienceSettings', () => {
it('social verification record', async () => {
ssoConnectors.getAvailableSsoConnectors.mockResolvedValueOnce([mockSsoConnector]);

const signInExperienceSettings = new SignInExperienceSettings(
const signInExperienceSettings = new SignInExperienceValidator(
mockTenant.libraries,
mockTenant.queries
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type Libraries from '#src/tenants/Libraries.js';
import type Queries from '#src/tenants/Queries.js';
import assertThat from '#src/utils/assert-that.js';

import { type VerificationRecord } from './verifications/index.js';
import { type VerificationRecord } from '../verifications/index.js';

const getEmailIdentifierFromVerificationRecord = (verificationRecord: VerificationRecord) => {
switch (verificationRecord.type) {
Expand All @@ -32,7 +32,14 @@ const getEmailIdentifierFromVerificationRecord = (verificationRecord: Verificati
}
};

export class SignInExperienceSettings {
/**
* SignInExperienceValidator class provides all the sign-in experience settings validation logic.
*
* - Guard the interaction event based on the sign-in experience settings
* - Guard the identification method based on the sign-in experience settings
* - Guard the email identifier with SSO enabled domains
*/
export class SignInExperienceValidator {
private signInExperienceDataCache?: SignInExperience;

constructor(
Expand Down

0 comments on commit 0785343

Please sign in to comment.