2022-06-07 03:05:24 -05:00
|
|
|
import { UserRole } from '@logto/schemas';
|
2021-06-27 07:44:05 -05:00
|
|
|
import Koa from 'koa';
|
2021-08-29 22:30:54 -05:00
|
|
|
import mount from 'koa-mount';
|
2021-06-27 07:44:05 -05:00
|
|
|
import Router from 'koa-router';
|
2022-10-21 00:14:17 -05:00
|
|
|
import type { Provider } from 'oidc-provider';
|
2021-08-29 22:30:54 -05:00
|
|
|
|
2022-12-16 10:41:45 -05:00
|
|
|
import koaAuditLogLegacy from '#src/middleware/koa-audit-log-legacy.js';
|
|
|
|
|
2022-12-05 01:14:21 -05:00
|
|
|
import koaAuth from '../middleware/koa-auth.js';
|
2022-12-16 00:54:37 -05:00
|
|
|
import koaLogSessionLegacy from '../middleware/koa-log-session-legacy.js';
|
2022-12-05 01:14:21 -05:00
|
|
|
import adminUserRoutes from './admin-user.js';
|
|
|
|
import applicationRoutes from './application.js';
|
|
|
|
import authnRoutes from './authn.js';
|
|
|
|
import connectorRoutes from './connector.js';
|
|
|
|
import customPhraseRoutes from './custom-phrase.js';
|
|
|
|
import dashboardRoutes from './dashboard.js';
|
2022-12-19 23:14:15 -05:00
|
|
|
import hookRoutes from './hook.js';
|
2022-12-08 23:12:32 -05:00
|
|
|
import interactionRoutes from './interaction/index.js';
|
2022-12-05 01:14:21 -05:00
|
|
|
import logRoutes from './log.js';
|
|
|
|
import phraseRoutes from './phrase.js';
|
|
|
|
import profileRoutes from './profile.js';
|
|
|
|
import resourceRoutes from './resource.js';
|
|
|
|
import roleRoutes from './role.js';
|
|
|
|
import sessionRoutes from './session/index.js';
|
|
|
|
import settingRoutes from './setting.js';
|
|
|
|
import signInExperiencesRoutes from './sign-in-experience.js';
|
|
|
|
import statusRoutes from './status.js';
|
|
|
|
import swaggerRoutes from './swagger.js';
|
2022-12-16 00:54:37 -05:00
|
|
|
import type { AnonymousRouter, AnonymousRouterLegacy, AuthedRouter } from './types.js';
|
2022-12-05 01:14:21 -05:00
|
|
|
import wellKnownRoutes from './well-known.js';
|
2021-09-01 04:35:23 -05:00
|
|
|
|
2021-08-15 10:39:03 -05:00
|
|
|
const createRouters = (provider: Provider) => {
|
2022-12-16 00:54:37 -05:00
|
|
|
const sessionRouter: AnonymousRouterLegacy = new Router();
|
2022-12-16 10:41:45 -05:00
|
|
|
sessionRouter.use(koaAuditLogLegacy(), koaLogSessionLegacy(provider));
|
2022-05-13 07:37:36 -05:00
|
|
|
sessionRoutes(sessionRouter, provider);
|
2021-06-27 07:44:05 -05:00
|
|
|
|
2022-12-08 23:12:32 -05:00
|
|
|
const interactionRouter: AnonymousRouter = new Router();
|
|
|
|
interactionRoutes(interactionRouter, provider);
|
|
|
|
|
2022-06-07 03:05:24 -05:00
|
|
|
const managementRouter: AuthedRouter = new Router();
|
|
|
|
managementRouter.use(koaAuth(UserRole.Admin));
|
|
|
|
applicationRoutes(managementRouter);
|
|
|
|
settingRoutes(managementRouter);
|
|
|
|
connectorRoutes(managementRouter);
|
|
|
|
resourceRoutes(managementRouter);
|
|
|
|
signInExperiencesRoutes(managementRouter);
|
|
|
|
adminUserRoutes(managementRouter);
|
|
|
|
logRoutes(managementRouter);
|
|
|
|
roleRoutes(managementRouter);
|
|
|
|
dashboardRoutes(managementRouter);
|
2022-09-13 04:36:37 -05:00
|
|
|
customPhraseRoutes(managementRouter);
|
2022-12-19 23:14:15 -05:00
|
|
|
hookRoutes(managementRouter);
|
2022-06-07 03:05:24 -05:00
|
|
|
|
2022-12-05 01:14:21 -05:00
|
|
|
const profileRouter: AnonymousRouter = new Router();
|
|
|
|
profileRoutes(profileRouter, provider);
|
|
|
|
|
2022-06-05 01:34:50 -05:00
|
|
|
const anonymousRouter: AnonymousRouter = new Router();
|
2022-09-27 02:36:03 -05:00
|
|
|
phraseRoutes(anonymousRouter, provider);
|
2022-07-01 04:14:38 -05:00
|
|
|
wellKnownRoutes(anonymousRouter, provider);
|
2022-06-05 01:34:50 -05:00
|
|
|
statusRoutes(anonymousRouter);
|
2022-08-18 09:10:17 -05:00
|
|
|
authnRoutes(anonymousRouter);
|
2022-06-05 01:34:50 -05:00
|
|
|
// The swagger.json should contain all API routers.
|
2022-12-08 23:12:32 -05:00
|
|
|
swaggerRoutes(anonymousRouter, [
|
|
|
|
sessionRouter,
|
|
|
|
interactionRouter,
|
|
|
|
profileRouter,
|
|
|
|
managementRouter,
|
|
|
|
anonymousRouter,
|
|
|
|
]);
|
2022-06-05 01:34:50 -05:00
|
|
|
|
2022-12-08 23:12:32 -05:00
|
|
|
return [sessionRouter, interactionRouter, profileRouter, managementRouter, anonymousRouter];
|
2021-07-03 08:19:20 -05:00
|
|
|
};
|
|
|
|
|
2021-07-29 13:21:47 -05:00
|
|
|
export default function initRouter(app: Koa, provider: Provider) {
|
2021-08-15 10:39:03 -05:00
|
|
|
const apisApp = new Koa();
|
|
|
|
|
|
|
|
for (const router of createRouters(provider)) {
|
2022-12-16 00:54:37 -05:00
|
|
|
// @ts-expect-error will remove once interaction refactor finished
|
2021-08-15 10:39:03 -05:00
|
|
|
apisApp.use(router.routes()).use(router.allowedMethods());
|
|
|
|
}
|
2021-07-29 13:21:47 -05:00
|
|
|
|
|
|
|
app.use(mount('/api', apisApp));
|
2021-06-27 07:44:05 -05:00
|
|
|
}
|