]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/plugins.ts
Add peertube plugin index website models
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / plugins.ts
CommitLineData
345da516 1import * as express from 'express'
b5f919ac 2import { body, param, query } from 'express-validator/check'
345da516
C
3import { logger } from '../../helpers/logger'
4import { areValidationErrors } from './utils'
b5f919ac 5import { isNpmPluginNameValid, isPluginNameValid, isPluginTypeValid, isPluginVersionValid } 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'
b5f919ac
C
9import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/install-plugin.model'
10import { PluginType } from '../../../shared/models/plugins/plugin.type'
345da516 11
b5f919ac 12const servePluginStaticDirectoryValidator = (pluginType: PluginType) => [
345da516
C
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
b5f919ac
C
22 const npmName = PluginModel.buildNpmName(req.params.pluginName, pluginType)
23 const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(npmName)
345da516
C
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
ad91e700
C
35const 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
b5f919ac 53const installOrUpdatePluginValidator = [
8d2be0ed
C
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'),
ad91e700
C
60
61 (req: express.Request, res: express.Response, next: express.NextFunction) => {
b5f919ac 62 logger.debug('Checking installOrUpdatePluginValidator parameters', { parameters: req.body })
ad91e700
C
63
64 if (areValidationErrors(req, res)) return
65
b5f919ac 66 const body: InstallOrUpdatePlugin = req.body
8d2be0ed
C
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
ad91e700
C
73 return next()
74 }
75]
76
77const uninstallPluginValidator = [
78 body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
79
80 (req: express.Request, res: express.Response, next: express.NextFunction) => {
dba85a1e 81 logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body })
ad91e700
C
82
83 if (areValidationErrors(req, res)) return
84
85 return next()
86 }
87]
88
dba85a1e
C
89const existingPluginValidator = [
90 param('npmName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'),
ad91e700
C
91
92 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
dba85a1e 93 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params })
ad91e700
C
94
95 if (areValidationErrors(req, res)) return
96
dba85a1e 97 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
ad91e700
C
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
110const 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
345da516
C
122// ---------------------------------------------------------------------------
123
124export {
ad91e700
C
125 servePluginStaticDirectoryValidator,
126 updatePluginSettingsValidator,
127 uninstallPluginValidator,
dba85a1e 128 existingPluginValidator,
b5f919ac 129 installOrUpdatePluginValidator,
ad91e700 130 listPluginsValidator
345da516 131}