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