1 import express from 'express'
2 import { body, param, query, ValidationChain } from 'express-validator'
3 import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
4 import { PluginType } from '../../../shared/models/plugins/plugin.type'
5 import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/server/api/install-plugin.model'
6 import { exists, isBooleanValid, isSafePath, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc'
7 import { isNpmPluginNameValid, isPluginNameValid, isPluginTypeValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins'
8 import { CONFIG } from '../../initializers/config'
9 import { PluginManager } from '../../lib/plugins/plugin-manager'
10 import { PluginModel } from '../../models/server/plugin'
11 import { areValidationErrors } from './shared'
13 const getPluginValidator = (pluginType: PluginType, withVersion = true) => {
14 const validators: (ValidationChain | express.Handler)[] = [
16 .custom(isPluginNameValid)
21 param('pluginVersion')
22 .custom(isPluginVersionValid)
26 return validators.concat([
27 (req: express.Request, res: express.Response, next: express.NextFunction) => {
28 if (areValidationErrors(req, res)) return
30 const npmName = PluginModel.buildNpmName(req.params.pluginName, pluginType)
31 const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(npmName)
35 status: HttpStatusCode.NOT_FOUND_404,
36 message: 'No plugin found named ' + npmName
39 if (withVersion && plugin.version !== req.params.pluginVersion) {
41 status: HttpStatusCode.NOT_FOUND_404,
42 message: 'No plugin found named ' + npmName + ' with version ' + req.params.pluginVersion
46 res.locals.registeredPlugin = plugin
53 const getExternalAuthValidator = [
57 (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 if (areValidationErrors(req, res)) return
60 const plugin = res.locals.registeredPlugin
61 if (!plugin.registerHelpers) {
63 status: HttpStatusCode.NOT_FOUND_404,
64 message: 'No registered helpers were found for this plugin'
68 const externalAuth = plugin.registerHelpers.getExternalAuths().find(a => a.authName === req.params.authName)
71 status: HttpStatusCode.NOT_FOUND_404,
72 message: 'No external auths were found for this plugin'
76 res.locals.externalAuth = externalAuth
82 const pluginStaticDirectoryValidator = [
83 param('staticEndpoint')
86 (req: express.Request, res: express.Response, next: express.NextFunction) => {
87 if (areValidationErrors(req, res)) return
93 const listPluginsValidator = [
96 .customSanitizer(toIntOrNull)
97 .custom(isPluginTypeValid),
100 .customSanitizer(toBooleanOrNull)
101 .custom(isBooleanValid),
103 (req: express.Request, res: express.Response, next: express.NextFunction) => {
104 if (areValidationErrors(req, res)) return
110 const installOrUpdatePluginValidator = [
113 .custom(isNpmPluginNameValid),
114 body('pluginVersion')
116 .custom(isPluginVersionValid),
121 (req: express.Request, res: express.Response, next: express.NextFunction) => {
122 if (areValidationErrors(req, res)) return
124 const body: InstallOrUpdatePlugin = req.body
125 if (!body.path && !body.npmName) {
126 return res.fail({ message: 'Should have either a npmName or a path' })
128 if (body.pluginVersion && !body.npmName) {
129 return res.fail({ message: 'Should have a npmName when specifying a pluginVersion' })
136 const uninstallPluginValidator = [
138 .custom(isNpmPluginNameValid),
140 (req: express.Request, res: express.Response, next: express.NextFunction) => {
141 if (areValidationErrors(req, res)) return
147 const existingPluginValidator = [
149 .custom(isNpmPluginNameValid),
151 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
152 if (areValidationErrors(req, res)) return
154 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
157 status: HttpStatusCode.NOT_FOUND_404,
158 message: 'Plugin not found'
162 res.locals.plugin = plugin
167 const updatePluginSettingsValidator = [
171 (req: express.Request, res: express.Response, next: express.NextFunction) => {
172 if (areValidationErrors(req, res)) return
178 const listAvailablePluginsValidator = [
184 .customSanitizer(toIntOrNull)
185 .custom(isPluginTypeValid),
186 query('currentPeerTubeEngine')
188 .custom(isPluginVersionValid),
190 (req: express.Request, res: express.Response, next: express.NextFunction) => {
191 if (areValidationErrors(req, res)) return
193 if (CONFIG.PLUGINS.INDEX.ENABLED === false) {
194 return res.fail({ message: 'Plugin index is not enabled' })
201 // ---------------------------------------------------------------------------
204 pluginStaticDirectoryValidator,
206 updatePluginSettingsValidator,
207 uninstallPluginValidator,
208 listAvailablePluginsValidator,
209 existingPluginValidator,
210 installOrUpdatePluginValidator,
211 listPluginsValidator,
212 getExternalAuthValidator