48 lines
2.1 KiB
TypeScript
48 lines
2.1 KiB
TypeScript
import { randomBytes } from "node:crypto";
|
|
|
|
import type { Sequelize } from "sequelize";
|
|
|
|
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 { requireGlobalResourceManager } from "./infrastructure/http/require-global-resource-manager";
|
|
import { migrateIdentity } from "./infrastructure/persistence/migrate-identity";
|
|
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";
|
|
|
|
export { type IdentityContext, SequelizeSessionStore, migrateIdentity };
|
|
|
|
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"))
|
|
);
|
|
|
|
return {
|
|
authenticate: requireAuthenticated(useCases),
|
|
requireGlobalResourceManager: requireGlobalResourceManager(useCases),
|
|
services: {
|
|
authorization: {
|
|
canManageGlobalResources: (context: IdentityContext | undefined) =>
|
|
useCases.canManageGlobalResources(context),
|
|
},
|
|
sessions: {
|
|
isAuthenticated: async (context: IdentityContext) =>
|
|
(await useCases.session(context)) !== undefined,
|
|
},
|
|
},
|
|
register: (app: Parameters<typeof registerIdentityRoutes>[0]) =>
|
|
registerIdentityRoutes(app, useCases, origin),
|
|
};
|
|
}
|