2022-06-08 11:53:12 -07:00
|
|
|
/* eslint-disable @typescript-eslint/naming-convention */
|
|
|
|
/* eslint-disable import/no-cycle */
|
|
|
|
import express, { Router } from 'express';
|
2022-06-14 09:32:19 -07:00
|
|
|
import fs from 'fs/promises';
|
|
|
|
import path from 'path';
|
|
|
|
|
2022-06-08 11:53:12 -07:00
|
|
|
import * as OpenApiValidator from 'express-openapi-validator';
|
|
|
|
import { HttpError } from 'express-openapi-validator/dist/framework/types';
|
|
|
|
import { OpenAPIV3 } from 'openapi-types';
|
2022-06-14 09:32:19 -07:00
|
|
|
import swaggerUi from 'swagger-ui-express';
|
2022-06-08 11:53:12 -07:00
|
|
|
import validator from 'validator';
|
2022-06-14 09:32:19 -07:00
|
|
|
import YAML from 'yamljs';
|
|
|
|
|
2022-06-08 11:53:12 -07:00
|
|
|
import config from '../../config';
|
2022-06-14 09:32:19 -07:00
|
|
|
import { Db, InternalHooksManager } from '..';
|
2022-06-08 11:53:12 -07:00
|
|
|
import { getInstanceBaseUrl } from '../UserManagement/UserManagementHelper';
|
|
|
|
|
|
|
|
function createApiRouter(
|
|
|
|
version: string,
|
|
|
|
openApiSpecPath: string,
|
2022-06-14 09:32:19 -07:00
|
|
|
handlersDirectory: string,
|
2022-06-08 11:53:12 -07:00
|
|
|
swaggerThemeCss: string,
|
|
|
|
publicApiEndpoint: string,
|
|
|
|
): Router {
|
|
|
|
const n8nPath = config.getEnv('path');
|
|
|
|
const swaggerDocument = YAML.load(openApiSpecPath) as swaggerUi.JsonObject;
|
|
|
|
// add the server depeding 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
|
|
|
|
2022-06-08 11:53:12 -07:00
|
|
|
apiController.use(
|
|
|
|
`/${publicApiEndpoint}/${version}/docs`,
|
|
|
|
swaggerUi.serveFiles(swaggerDocument),
|
|
|
|
swaggerUi.setup(swaggerDocument, {
|
|
|
|
customCss: swaggerThemeCss,
|
|
|
|
customSiteTitle: 'n8n Public API UI',
|
|
|
|
customfavIcon: `${n8nPath}favicon.ico`,
|
|
|
|
}),
|
|
|
|
);
|
2022-06-14 09:32:19 -07:00
|
|
|
|
2022-06-08 11:53:12 -07:00
|
|
|
apiController.use(`/${publicApiEndpoint}/${version}`, express.json());
|
2022-06-14 09:32:19 -07:00
|
|
|
|
2022-06-08 11:53:12 -07:00
|
|
|
apiController.use(
|
|
|
|
`/${publicApiEndpoint}/${version}`,
|
|
|
|
OpenApiValidator.middleware({
|
|
|
|
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> => {
|
|
|
|
const apiKey = req.headers[schema.name.toLowerCase()];
|
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 swaggerThemePath = path.join(__dirname, 'swaggerTheme.css');
|
|
|
|
const folders = await fs.readdir(__dirname);
|
|
|
|
const css = (await fs.readFile(swaggerThemePath)).toString();
|
|
|
|
const versions = folders.filter((folderName) => folderName.startsWith('v'));
|
2022-06-14 09:32:19 -07:00
|
|
|
|
|
|
|
const apiRouters = versions.map((version) => {
|
2022-06-08 11:53:12 -07:00
|
|
|
const openApiPath = path.join(__dirname, version, 'openapi.yml');
|
2022-06-14 09:32:19 -07:00
|
|
|
return createApiRouter(version, openApiPath, __dirname, css, publicApiEndpoint);
|
|
|
|
});
|
|
|
|
|
2022-06-08 11:53:12 -07:00
|
|
|
return {
|
|
|
|
apiRouters,
|
|
|
|
apiLatestVersion: Number(versions.pop()?.charAt(1)) ?? 1,
|
|
|
|
};
|
|
|
|
};
|