2022-06-08 11:53:12 -07:00
|
|
|
/* eslint-disable @typescript-eslint/naming-convention */
|
2023-01-27 05:56:56 -08:00
|
|
|
import type { Router } from 'express';
|
|
|
|
import express from 'express';
|
2022-06-14 09:32:19 -07:00
|
|
|
import fs from 'fs/promises';
|
|
|
|
import path from 'path';
|
|
|
|
|
2023-01-26 02:44:50 -08:00
|
|
|
import validator from 'validator';
|
|
|
|
import { middleware as openapiValidatorMiddleware } from 'express-openapi-validator';
|
|
|
|
import YAML from 'yamljs';
|
2023-01-02 03:14:58 -08:00
|
|
|
import type { HttpError } from 'express-openapi-validator/dist/framework/types';
|
|
|
|
import type { OpenAPIV3 } from 'openapi-types';
|
|
|
|
import type { JsonObject } from 'swagger-ui-express';
|
2022-06-14 09:32:19 -07:00
|
|
|
|
2022-11-09 06:25:00 -08:00
|
|
|
import config from '@/config';
|
|
|
|
import * as Db from '@/Db';
|
|
|
|
import { InternalHooksManager } from '@/InternalHooksManager';
|
|
|
|
import { getInstanceBaseUrl } from '@/UserManagement/UserManagementHelper';
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-01-02 03:14:58 -08:00
|
|
|
async function createApiRouter(
|
2022-06-08 11:53:12 -07:00
|
|
|
version: string,
|
|
|
|
openApiSpecPath: string,
|
2022-06-14 09:32:19 -07:00
|
|
|
handlersDirectory: string,
|
2022-06-08 11:53:12 -07:00
|
|
|
publicApiEndpoint: string,
|
2023-01-02 03:14:58 -08:00
|
|
|
): Promise<Router> {
|
2022-06-08 11:53:12 -07:00
|
|
|
const n8nPath = config.getEnv('path');
|
2023-01-02 03:14:58 -08:00
|
|
|
const swaggerDocument = YAML.load(openApiSpecPath) as JsonObject;
|
2022-09-02 07:13:17 -07:00
|
|
|
// add the server depending on the config so the user can interact with the API
|
2022-06-14 09:32:19 -07:00
|
|
|
// from the Swagger UI
|
2022-06-08 11:53:12 -07:00
|
|
|
swaggerDocument.server = [
|
|
|
|
{
|
|
|
|
url: `${getInstanceBaseUrl()}/${publicApiEndpoint}/${version}}`,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const apiController = express.Router();
|
2022-06-14 09:32:19 -07:00
|
|
|
|
2023-01-02 03:14:58 -08:00
|
|
|
if (!config.getEnv('publicApi.swaggerUi.disabled')) {
|
|
|
|
const { serveFiles, setup } = await import('swagger-ui-express');
|
2023-01-26 02:44:50 -08:00
|
|
|
const swaggerThemePath = path.join(__dirname, 'swaggerTheme.css');
|
|
|
|
const swaggerThemeCss = await fs.readFile(swaggerThemePath, { encoding: 'utf-8' });
|
2022-06-14 09:32:19 -07:00
|
|
|
|
2023-01-02 03:14:58 -08:00
|
|
|
apiController.use(
|
|
|
|
`/${publicApiEndpoint}/${version}/docs`,
|
|
|
|
serveFiles(swaggerDocument),
|
|
|
|
setup(swaggerDocument, {
|
|
|
|
customCss: swaggerThemeCss,
|
|
|
|
customSiteTitle: 'n8n Public API UI',
|
|
|
|
customfavIcon: `${n8nPath}favicon.ico`,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2022-06-14 09:32:19 -07:00
|
|
|
|
2023-01-30 08:38:31 -08:00
|
|
|
apiController.get(`/${publicApiEndpoint}/${version}/openapi.yml`, (req, res) => {
|
|
|
|
res.sendFile(openApiSpecPath);
|
|
|
|
});
|
|
|
|
|
2022-06-08 11:53:12 -07:00
|
|
|
apiController.use(
|
|
|
|
`/${publicApiEndpoint}/${version}`,
|
2023-01-02 03:14:58 -08:00
|
|
|
express.json(),
|
2023-01-26 02:44:50 -08:00
|
|
|
openapiValidatorMiddleware({
|
2022-06-08 11:53:12 -07:00
|
|
|
apiSpec: openApiSpecPath,
|
2022-06-14 09:32:19 -07:00
|
|
|
operationHandlers: handlersDirectory,
|
2022-06-08 11:53:12 -07:00
|
|
|
validateRequests: true,
|
|
|
|
validateApiSpec: true,
|
|
|
|
formats: [
|
|
|
|
{
|
|
|
|
name: 'email',
|
|
|
|
type: 'string',
|
|
|
|
validate: (email: string) => validator.isEmail(email),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'identifier',
|
|
|
|
type: 'string',
|
|
|
|
validate: (identifier: string) =>
|
|
|
|
validator.isUUID(identifier) || validator.isEmail(identifier),
|
|
|
|
},
|
2022-07-19 23:57:29 -07:00
|
|
|
{
|
|
|
|
name: 'jsonString',
|
|
|
|
validate: (data: string) => {
|
|
|
|
try {
|
|
|
|
JSON.parse(data);
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2022-06-08 11:53:12 -07:00
|
|
|
],
|
|
|
|
validateSecurity: {
|
|
|
|
handlers: {
|
|
|
|
ApiKeyAuth: async (
|
|
|
|
req: express.Request,
|
|
|
|
_scopes: unknown,
|
|
|
|
schema: OpenAPIV3.ApiKeySecurityScheme,
|
|
|
|
): Promise<boolean> => {
|
2023-01-13 09:12:22 -08:00
|
|
|
const apiKey = req.headers[schema.name.toLowerCase()] as string;
|
2022-06-14 09:32:19 -07:00
|
|
|
const user = await Db.collections.User.findOne({
|
|
|
|
where: { apiKey },
|
2022-06-08 11:53:12 -07:00
|
|
|
relations: ['globalRole'],
|
|
|
|
});
|
|
|
|
|
2022-06-14 09:32:19 -07:00
|
|
|
if (!user) return false;
|
2022-06-08 11:53:12 -07:00
|
|
|
|
|
|
|
void InternalHooksManager.getInstance().onUserInvokedApi({
|
|
|
|
user_id: user.id,
|
|
|
|
path: req.path,
|
|
|
|
method: req.method,
|
|
|
|
api_version: version,
|
|
|
|
});
|
|
|
|
|
|
|
|
req.user = user;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2022-06-14 09:32:19 -07:00
|
|
|
|
2022-06-08 11:53:12 -07:00
|
|
|
apiController.use(
|
2022-06-14 09:32:19 -07:00
|
|
|
(
|
|
|
|
error: HttpError,
|
|
|
|
_req: express.Request,
|
|
|
|
res: express.Response,
|
|
|
|
_next: express.NextFunction,
|
|
|
|
) => {
|
2022-06-08 11:53:12 -07:00
|
|
|
return res.status(error.status || 400).json({
|
|
|
|
message: error.message,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2022-06-14 09:32:19 -07:00
|
|
|
|
2022-06-08 11:53:12 -07:00
|
|
|
return apiController;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const loadPublicApiVersions = async (
|
|
|
|
publicApiEndpoint: string,
|
|
|
|
): Promise<{ apiRouters: express.Router[]; apiLatestVersion: number }> => {
|
|
|
|
const folders = await fs.readdir(__dirname);
|
|
|
|
const versions = folders.filter((folderName) => folderName.startsWith('v'));
|
2022-06-14 09:32:19 -07:00
|
|
|
|
2023-01-02 03:14:58 -08:00
|
|
|
const apiRouters = await Promise.all(
|
|
|
|
versions.map(async (version) => {
|
|
|
|
const openApiPath = path.join(__dirname, version, 'openapi.yml');
|
2023-01-26 02:44:50 -08:00
|
|
|
return createApiRouter(version, openApiPath, __dirname, publicApiEndpoint);
|
2023-01-02 03:14:58 -08:00
|
|
|
}),
|
|
|
|
);
|
2022-06-14 09:32:19 -07:00
|
|
|
|
2022-06-08 11:53:12 -07:00
|
|
|
return {
|
|
|
|
apiRouters,
|
|
|
|
apiLatestVersion: Number(versions.pop()?.charAt(1)) ?? 1,
|
|
|
|
};
|
|
|
|
};
|