metis-desk/modules/identity/src/api/module.ts

42 lines
1.8 KiB
TypeScript
Raw Normal View History

import { randomBytes } from "node:crypto";
import type { Sequelize } from "sequelize";
2026-10-20 11:20:22 +00:00
import type { IdentityContext } from "./application/identity.use-cases";
import { IdentityUseCases } from "./application/identity.use-cases";
import { UserInputMapper } from "./application/user-input.mapper";
import { registerIdentityRoutes } from "./infrastructure/http/identity.routes";
import { requireAuthenticated } from "./infrastructure/http/require-authenticated";
import { migrateIdentity } from "./infrastructure/persistence/migrate-identity";
2026-10-10 14:40:51 +00:00
import { SequelizeSessionStore } from "./infrastructure/persistence/sequelize-session.store";
import { SequelizeUserRepository } from "./infrastructure/persistence/sequelize-user.repository";
import { initializeIdentityModels } from "./infrastructure/persistence/user.model";
import { UserPersistenceMapper } from "./infrastructure/persistence/user-persistence.mapper";
import { ScryptPasswordHasher } from "./infrastructure/security/scrypt-password-hasher";
2026-10-20 11:20:22 +00:00
export { type IdentityContext, SequelizeSessionStore, migrateIdentity };
2026-10-10 10:01:58 +00:00
export async function createIdentityModule(database: Sequelize, origin: string) {
initializeIdentityModels(database);
const passwords = new ScryptPasswordHasher();
const users = new SequelizeUserRepository(database, new UserPersistenceMapper());
const useCases = new IdentityUseCases(
users,
passwords,
new UserInputMapper(passwords),
await passwords.hash(randomBytes(32).toString("hex"))
);
2026-10-10 10:01:58 +00:00
return {
authenticate: requireAuthenticated(useCases),
2026-10-20 11:20:22 +00:00
services: {
sessions: {
isAuthenticated: async (context: IdentityContext) =>
(await useCases.session(context)) !== undefined,
},
},
register: (app: Parameters<typeof registerIdentityRoutes>[0]) =>
registerIdentityRoutes(app, useCases, origin),
};
}