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'
10 isPluginStableOrUnstableVersionValid,
12 } from '../../helpers/custom-validators/plugins'
13 import { CONFIG } from '../../initializers/config'
14 import { PluginManager } from '../../lib/plugins/plugin-manager'
15 import { PluginModel } from '../../models/server/plugin'
16 import { areValidationErrors } from './shared'
18 const getPluginValidator = (pluginType: PluginType, withVersion = true) => {
19 const validators: (ValidationChain | express.Handler)[] = [
21 .custom(isPluginNameValid)
26 param('pluginVersion')
27 .custom(isPluginStableOrUnstableVersionValid)
31 return validators.concat([
32 (req: express.Request, res: express.Response, next: express.NextFunction) => {
33 if (areValidationErrors(req, res)) return
35 const npmName = PluginModel.buildNpmName(req.params.pluginName, pluginType)
36 const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(npmName)
40 status: HttpStatusCode.NOT_FOUND_404,
41 message: 'No plugin found named ' + npmName
44 if (withVersion && plugin.version !== req.params.pluginVersion) {
46 status: HttpStatusCode.NOT_FOUND_404,
47 message: 'No plugin found named ' + npmName + ' with version ' + req.params.pluginVersion
51 res.locals.registeredPlugin = plugin
58 const getExternalAuthValidator = [
62 (req: express.Request, res: express.Response, next: express.NextFunction) => {
63 if (areValidationErrors(req, res)) return
65 const plugin = res.locals.registeredPlugin
66 if (!plugin.registerHelpers) {
68 status: HttpStatusCode.NOT_FOUND_404,
69 message: 'No registered helpers were found for this plugin'
73 const externalAuth = plugin.registerHelpers.getExternalAuths().find(a => a.authName === req.params.authName)
76 status: HttpStatusCode.NOT_FOUND_404,
77 message: 'No external auths were found for this plugin'
81 res.locals.externalAuth = externalAuth
87 const pluginStaticDirectoryValidator = [
88 param('staticEndpoint')
91 (req: express.Request, res: express.Response, next: express.NextFunction) => {
92 if (areValidationErrors(req, res)) return
98 const listPluginsValidator = [
101 .customSanitizer(toIntOrNull)
102 .custom(isPluginTypeValid),
105 .customSanitizer(toBooleanOrNull)
106 .custom(isBooleanValid),
108 (req: express.Request, res: express.Response, next: express.NextFunction) => {
109 if (areValidationErrors(req, res)) return
115 const installOrUpdatePluginValidator = [
118 .custom(isNpmPluginNameValid),
119 body('pluginVersion')
121 .custom(isPluginStableOrUnstableVersionValid),
126 (req: express.Request, res: express.Response, next: express.NextFunction) => {
127 if (areValidationErrors(req, res)) return
129 const body: InstallOrUpdatePlugin = req.body
130 if (!body.path && !body.npmName) {
131 return res.fail({ message: 'Should have either a npmName or a path' })
133 if (body.pluginVersion && !body.npmName) {
134 return res.fail({ message: 'Should have a npmName when specifying a pluginVersion' })
141 const uninstallPluginValidator = [
143 .custom(isNpmPluginNameValid),
145 (req: express.Request, res: express.Response, next: express.NextFunction) => {
146 if (areValidationErrors(req, res)) return
152 const existingPluginValidator = [
154 .custom(isNpmPluginNameValid),
156 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
157 if (areValidationErrors(req, res)) return
159 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
162 status: HttpStatusCode.NOT_FOUND_404,
163 message: 'Plugin not found'
167 res.locals.plugin = plugin
172 const updatePluginSettingsValidator = [
176 (req: express.Request, res: express.Response, next: express.NextFunction) => {
177 if (areValidationErrors(req, res)) return
183 const listAvailablePluginsValidator = [
189 .customSanitizer(toIntOrNull)
190 .custom(isPluginTypeValid),
191 query('currentPeerTubeEngine')
193 .custom(isPluginStableOrUnstableVersionValid),
195 (req: express.Request, res: express.Response, next: express.NextFunction) => {
196 if (areValidationErrors(req, res)) return
198 if (CONFIG.PLUGINS.INDEX.ENABLED === false) {
199 return res.fail({ message: 'Plugin index is not enabled' })
206 // ---------------------------------------------------------------------------
209 pluginStaticDirectoryValidator,
211 updatePluginSettingsValidator,
212 uninstallPluginValidator,
213 listAvailablePluginsValidator,
214 existingPluginValidator,
215 installOrUpdatePluginValidator,
216 listPluginsValidator,
217 getExternalAuthValidator