2022-06-07 16:05:24 +08:00
|
|
|
import { UserRole } from '@logto/schemas';
|
2021-06-27 20:44:05 +08:00
|
|
|
import Koa from 'koa';
|
2021-08-30 11:30:54 +08:00
|
|
|
import mount from 'koa-mount';
|
2021-06-27 20:44:05 +08:00
|
|
|
import Router from 'koa-router';
|
2021-07-03 21:19:20 +08:00
|
|
|
import { Provider } from 'oidc-provider';
|
2021-08-30 11:30:54 +08:00
|
|
|
|
2021-09-01 17:35:23 +08:00
|
|
|
import koaAuth from '@/middleware/koa-auth';
|
2022-05-13 20:37:36 +08:00
|
|
|
import koaLogSession from '@/middleware/koa-log-session';
|
2021-08-15 23:39:03 +08:00
|
|
|
import applicationRoutes from '@/routes/application';
|
2022-01-24 14:40:15 +08:00
|
|
|
import connectorRoutes from '@/routes/connector';
|
2022-05-25 16:54:21 +08:00
|
|
|
import dashboardRoutes from '@/routes/dashboard';
|
2022-01-18 09:20:49 +08:00
|
|
|
import resourceRoutes from '@/routes/resource';
|
2022-06-14 21:48:05 +08:00
|
|
|
import sessionPasswordlessRoutes from '@/routes/session/passwordless';
|
|
|
|
import sessionRoutes from '@/routes/session/session';
|
|
|
|
import sessionSocialRoutes from '@/routes/session/social';
|
2022-01-13 20:05:14 +08:00
|
|
|
import settingRoutes from '@/routes/setting';
|
2022-01-26 11:07:45 +08:00
|
|
|
import signInExperiencesRoutes from '@/routes/sign-in-experience';
|
2022-05-19 11:24:42 +08:00
|
|
|
import signInSettingsRoutes from '@/routes/sign-in-settings';
|
2021-09-06 10:40:06 +08:00
|
|
|
import statusRoutes from '@/routes/status';
|
2021-08-30 11:30:54 +08:00
|
|
|
import swaggerRoutes from '@/routes/swagger';
|
2021-06-27 20:44:05 +08:00
|
|
|
|
2022-01-28 13:33:57 +08:00
|
|
|
import adminUserRoutes from './admin-user';
|
2022-05-16 14:43:33 +08:00
|
|
|
import logRoutes from './log';
|
2022-06-07 16:05:24 +08:00
|
|
|
import meRoutes from './me';
|
2022-01-28 13:33:57 +08:00
|
|
|
import roleRoutes from './role';
|
2021-09-01 17:35:23 +08:00
|
|
|
import { AnonymousRouter, AuthedRouter } from './types';
|
|
|
|
|
2021-08-15 23:39:03 +08:00
|
|
|
const createRouters = (provider: Provider) => {
|
2022-05-13 20:37:36 +08:00
|
|
|
const sessionRouter: AnonymousRouter = new Router();
|
|
|
|
sessionRouter.use(koaLogSession(provider));
|
|
|
|
sessionRoutes(sessionRouter, provider);
|
2022-06-14 21:48:05 +08:00
|
|
|
sessionPasswordlessRoutes(sessionRouter, provider);
|
|
|
|
sessionSocialRoutes(sessionRouter, provider);
|
2021-06-27 20:44:05 +08:00
|
|
|
|
2022-06-07 16:05:24 +08: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);
|
|
|
|
|
|
|
|
const meRouter: AuthedRouter = new Router();
|
|
|
|
meRouter.use(koaAuth());
|
|
|
|
meRoutes(meRouter);
|
2022-05-13 20:37:36 +08:00
|
|
|
|
2022-06-05 14:34:50 +08:00
|
|
|
const anonymousRouter: AnonymousRouter = new Router();
|
2022-06-17 10:36:07 +08:00
|
|
|
signInSettingsRoutes(anonymousRouter, provider);
|
2022-06-05 14:34:50 +08:00
|
|
|
statusRoutes(anonymousRouter);
|
|
|
|
// The swagger.json should contain all API routers.
|
2022-06-07 16:05:24 +08:00
|
|
|
swaggerRoutes(anonymousRouter, [sessionRouter, managementRouter, meRouter, anonymousRouter]);
|
2022-06-05 14:34:50 +08:00
|
|
|
|
2022-06-07 16:05:24 +08:00
|
|
|
return [sessionRouter, managementRouter, meRouter, anonymousRouter];
|
2021-07-03 21:19:20 +08:00
|
|
|
};
|
|
|
|
|
2021-07-30 02:21:47 +08:00
|
|
|
export default function initRouter(app: Koa, provider: Provider) {
|
2021-08-15 23:39:03 +08:00
|
|
|
const apisApp = new Koa();
|
|
|
|
|
|
|
|
for (const router of createRouters(provider)) {
|
|
|
|
apisApp.use(router.routes()).use(router.allowedMethods());
|
|
|
|
}
|
2021-07-30 02:21:47 +08:00
|
|
|
|
|
|
|
app.use(mount('/api', apisApp));
|
2021-06-27 20:44:05 +08:00
|
|
|
}
|