]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/plugins.ts
WIP plugins: move plugin CLI in peertube script
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / plugins.ts
CommitLineData
345da516 1import * as express from 'express'
ad91e700 2import { param, query, body } from 'express-validator/check'
345da516
C
3import { logger } from '../../helpers/logger'
4import { areValidationErrors } from './utils'
ad91e700 5import { isPluginNameValid, isPluginTypeValid, isPluginVersionValid, isNpmPluginNameValid } from '../../helpers/custom-validators/plugins'
345da516 6import { PluginManager } from '../../lib/plugins/plugin-manager'
ad91e700
C
7import { isBooleanValid, isSafePath } from '../../helpers/custom-validators/misc'
8import { PluginModel } from '../../models/server/plugin'
8d2be0ed 9import { InstallPlugin } from '../../../shared/models/plugins/install-plugin.model'
345da516
C
10
11const servePluginStaticDirectoryValidator = [
12 param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'),
13 param('pluginVersion').custom(isPluginVersionValid).withMessage('Should have a valid plugin version'),
14 param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'),
15
16 (req: express.Request, res: express.Response, next: express.NextFunction) => {
17 logger.debug('Checking servePluginStaticDirectory parameters', { parameters: req.params })
18
19 if (areValidationErrors(req, res)) return
20
7cd4d2ba 21 const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(req.params.pluginName)
345da516
C
22
23 if (!plugin || plugin.version !== req.params.pluginVersion) {
24 return res.sendStatus(404)
25 }
26
27 res.locals.registeredPlugin = plugin
28
29 return next()
30 }
31]
32
ad91e700
C
33const listPluginsValidator = [
34 query('type')
35 .optional()
36 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
37 query('uninstalled')
38 .optional()
39 .toBoolean()
40 .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'),
41
42 (req: express.Request, res: express.Response, next: express.NextFunction) => {
43 logger.debug('Checking listPluginsValidator parameters', { parameters: req.query })
44
45 if (areValidationErrors(req, res)) return
46
47 return next()
48 }
49]
50
51const installPluginValidator = [
8d2be0ed
C
52 body('npmName')
53 .optional()
54 .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
55 body('path')
56 .optional()
57 .custom(isSafePath).withMessage('Should have a valid safe path'),
ad91e700
C
58
59 (req: express.Request, res: express.Response, next: express.NextFunction) => {
60 logger.debug('Checking installPluginValidator parameters', { parameters: req.body })
61
62 if (areValidationErrors(req, res)) return
63
8d2be0ed
C
64 const body: InstallPlugin = req.body
65 if (!body.path && !body.npmName) {
66 return res.status(400)
67 .json({ error: 'Should have either a npmName or a path' })
68 .end()
69 }
70
ad91e700
C
71 return next()
72 }
73]
74
75const uninstallPluginValidator = [
76 body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
77
78 (req: express.Request, res: express.Response, next: express.NextFunction) => {
dba85a1e 79 logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body })
ad91e700
C
80
81 if (areValidationErrors(req, res)) return
82
83 return next()
84 }
85]
86
dba85a1e
C
87const existingPluginValidator = [
88 param('npmName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'),
ad91e700
C
89
90 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
dba85a1e 91 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params })
ad91e700
C
92
93 if (areValidationErrors(req, res)) return
94
dba85a1e 95 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
ad91e700
C
96 if (!plugin) {
97 return res.status(404)
98 .json({ error: 'Plugin not found' })
99 .end()
100 }
101
102 res.locals.plugin = plugin
103
104 return next()
105 }
106]
107
108const updatePluginSettingsValidator = [
109 body('settings').exists().withMessage('Should have settings'),
110
111 (req: express.Request, res: express.Response, next: express.NextFunction) => {
112 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body })
113
114 if (areValidationErrors(req, res)) return
115
116 return next()
117 }
118]
119
345da516
C
120// ---------------------------------------------------------------------------
121
122export {
ad91e700
C
123 servePluginStaticDirectoryValidator,
124 updatePluginSettingsValidator,
125 uninstallPluginValidator,
dba85a1e 126 existingPluginValidator,
ad91e700
C
127 installPluginValidator,
128 listPluginsValidator
345da516 129}