Skip to content

Commit

Permalink
refactor(core): refactor the interaction class
Browse files Browse the repository at this point in the history
 refactor the interaction class
  • Loading branch information
simeng-li committed Jul 4, 2024
1 parent a15fa78 commit e5c6c7b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class ExperienceInteraction {
/** The interaction event for the current interaction. */
private interactionEvent?: InteractionEvent;
/** The user verification record list for the current interaction. */
private readonly verificationRecords: Set<VerificationRecord>;
private readonly verificationRecords: Map<VerificationType, VerificationRecord>;
/** The accountId of the user for the current interaction. Only available once the user is identified. */
private accountId?: string;
/** The user provided profile data in the current interaction that needs to be stored to database. */
Expand All @@ -66,9 +66,12 @@ export default class ExperienceInteraction {
this.accountId = accountId; // TODO: @simeng-li replace with userId

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

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

[no-warning-comments] Unexpected 'todo' comment: 'TODO: @simeng-li replace with userId'.
this.profile = profile;

this.verificationRecords = new Set(
verificationRecords.map((record) => buildVerificationRecord(libraries, queries, record))
);
this.verificationRecords = new Map();

for (const record of verificationRecords) {
const instance = buildVerificationRecord(libraries, queries, record);
this.verificationRecords.set(instance.type, instance);
}
}

Check warning on line 75 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#L50-L75

Added lines #L50 - L75 were not covered by tests

/** Set the interaction event for the current interaction */
Expand All @@ -88,6 +91,15 @@ export default class ExperienceInteraction {
})
);

// Throws an 409 error if the current session has already identified a different user
if (this.accountId) {
assertThat(
this.accountId === verificationRecord.verifiedUserId,
new RequestError({ code: 'session.identity_conflict', status: 409 })
);
return;
}

this.accountId = verificationRecord.verifiedUserId;
}

Check warning on line 104 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#L85-L104

Added lines #L85 - L104 were not covered by tests

Expand All @@ -98,13 +110,7 @@ export default class ExperienceInteraction {
public appendVerificationRecord(record: VerificationRecord) {
const { type } = record;

const existingRecord = this.getVerificationRecordByType(type);

if (existingRecord) {
this.verificationRecords.delete(existingRecord);
}

this.verificationRecords.add(record);
this.verificationRecords.set(type, record);
}

Check warning on line 114 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#L111-L114

Added lines #L111 - L114 were not covered by tests

public getVerificationRecordById(verificationId: string) {
Expand All @@ -128,7 +134,7 @@ export default class ExperienceInteraction {
);
}

Check warning on line 135 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#L122-L135

Added lines #L122 - L135 were not covered by tests

/** Submit the current interaction result to the OIDC provider and clear the session */
/** Submit the current interaction result to the OIDC provider and clear the interaction data */
public async submit() {
// TODO: refine the error code

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

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

[no-warning-comments] Unexpected 'todo' comment: 'TODO: refine the error code'.
assertThat(this.accountId, 'session.verification_session_not_found');
Expand All @@ -143,11 +149,7 @@ export default class ExperienceInteraction {
}

Check warning on line 149 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#L139-L149

Added lines #L139 - L149 were not covered by tests

private get verificationRecordsArray() {
return Array.from(this.verificationRecords);
}

private getVerificationRecordByType(type: VerificationType) {
return this.verificationRecordsArray.find((record) => record.type === type);
return [...this.verificationRecords.values()];
}

Check warning on line 153 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#L152-L153

Added lines #L152 - L153 were not covered by tests

/** Convert the current interaction to JSON, so that it can be stored as the OIDC provider interaction result */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
passwordVerificationRecordDataGuard,
type PasswordVerificationRecordData,
} from './password-verification.js';
import { type VerificationRecord } from './verification-record.js';

export { type VerificationRecord } from './verification-record.js';

Expand All @@ -21,11 +22,11 @@ export const verificationRecordDataGuard = z.discriminatedUnion('type', [
/**
* The factory method to build a new VerificationRecord instance based on the provided VerificationRecordData.
*/
export const buildVerificationRecord = (
export const buildVerificationRecord = <T extends VerificationRecordData>(
libraries: Libraries,
queries: Queries,
data: VerificationRecordData
) => {
data: T
): VerificationRecord<T['type']> => {
switch (data.type) {
case VerificationType.Password: {
return new PasswordVerification(libraries, queries, data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ export class PasswordVerification implements VerificationRecord<VerificationType
}

Check warning on line 90 in packages/core/src/routes/experience/classes/verifications/password-verification.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/experience/classes/verifications/password-verification.ts#L84-L90

Added lines #L84 - L90 were not covered by tests

toJson(): PasswordVerificationRecordData {
const { id, type, identifier, userId } = this;

return {
id: this.id,
type: this.type,
identifier: this.identifier,
userId: this.userId,
id,
type,
identifier,
userId,
};
}

Check warning on line 101 in packages/core/src/routes/experience/classes/verifications/password-verification.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/experience/classes/verifications/password-verification.ts#L93-L101

Added lines #L93 - L101 were not covered by tests
}
2 changes: 2 additions & 0 deletions packages/phrases/src/locales/en/errors/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const session = {
interaction_not_found:
'Interaction session not found. Please go back and start the session again.',
not_supported_for_forgot_password: 'This operation is not supported for forgot password.',
identity_conflict:
'Identity mismatch detected. Please initiate a new session to proceed with a different identity.',
mfa: {
require_mfa_verification: 'Mfa verification is required to sign in.',
mfa_sign_in_only: 'Mfa is only available for sign-in interaction.',
Expand Down

0 comments on commit e5c6c7b

Please sign in to comment.