]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/plugins.ts
Begin support for external auths
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / plugins.ts
CommitLineData
345da516 1import * as express from 'express'
5e2b2e27 2import { body, param, query, ValidationChain } from 'express-validator'
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'
4a8d113b 7import { isBooleanValid, isSafePath, toBooleanOrNull, exists } from '../../helpers/custom-validators/misc'
ad91e700 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'
6702a1b2 11import { CONFIG } from '../../initializers/config'
345da516 12
5e2b2e27
C
13const getPluginValidator = (pluginType: PluginType, withVersion = true) => {
14 const validators: (ValidationChain | express.Handler)[] = [
15 param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name')
16 ]
345da516 17
5e2b2e27
C
18 if (withVersion) {
19 validators.push(
20 param('pluginVersion').custom(isPluginVersionValid).withMessage('Should have a valid plugin version')
21 )
22 }
345da516 23
5e2b2e27
C
24 return validators.concat([
25 (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 logger.debug('Checking getPluginValidator parameters', { parameters: req.params })
27
28 if (areValidationErrors(req, res)) return
29
30 const npmName = PluginModel.buildNpmName(req.params.pluginName, pluginType)
31 const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(npmName)
32
33 if (!plugin) return res.sendStatus(404)
34 if (withVersion && plugin.version !== req.params.pluginVersion) return res.sendStatus(404)
345da516 35
5e2b2e27 36 res.locals.registeredPlugin = plugin
345da516 37
5e2b2e27 38 return next()
345da516 39 }
5e2b2e27
C
40 ])
41}
42
4a8d113b
C
43const getExternalAuthValidator = [
44 param('authName').custom(exists).withMessage('Should have a valid auth name'),
45
46 (req: express.Request, res: express.Response, next: express.NextFunction) => {
47 logger.debug('Checking getExternalAuthValidator parameters', { parameters: req.params })
48
49 if (areValidationErrors(req, res)) return
50
51 const plugin = res.locals.registeredPlugin
52 if (!plugin.registerHelpersStore) return res.sendStatus(404)
53
54 const externalAuth = plugin.registerHelpersStore.getExternalAuths().find(a => a.authName === req.params.authName)
55 if (!externalAuth) return res.sendStatus(404)
56
57 res.locals.externalAuth = externalAuth
58
59 return next()
60 }
61]
62
5e2b2e27
C
63const pluginStaticDirectoryValidator = [
64 param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'),
345da516 65
5e2b2e27
C
66 (req: express.Request, res: express.Response, next: express.NextFunction) => {
67 logger.debug('Checking pluginStaticDirectoryValidator parameters', { parameters: req.params })
68
69 if (areValidationErrors(req, res)) return
345da516
C
70
71 return next()
72 }
73]
74
ad91e700 75const listPluginsValidator = [
6702a1b2 76 query('pluginType')
ad91e700
C
77 .optional()
78 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
79 query('uninstalled')
80 .optional()
c8861d5d 81 .customSanitizer(toBooleanOrNull)
ad91e700
C
82 .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'),
83
84 (req: express.Request, res: express.Response, next: express.NextFunction) => {
85 logger.debug('Checking listPluginsValidator parameters', { parameters: req.query })
86
87 if (areValidationErrors(req, res)) return
88
89 return next()
90 }
91]
92
b5f919ac 93const installOrUpdatePluginValidator = [
8d2be0ed
C
94 body('npmName')
95 .optional()
96 .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
97 body('path')
98 .optional()
99 .custom(isSafePath).withMessage('Should have a valid safe path'),
ad91e700
C
100
101 (req: express.Request, res: express.Response, next: express.NextFunction) => {
b5f919ac 102 logger.debug('Checking installOrUpdatePluginValidator parameters', { parameters: req.body })
ad91e700
C
103
104 if (areValidationErrors(req, res)) return
105
b5f919ac 106 const body: InstallOrUpdatePlugin = req.body
8d2be0ed
C
107 if (!body.path && !body.npmName) {
108 return res.status(400)
109 .json({ error: 'Should have either a npmName or a path' })
110 .end()
111 }
112
ad91e700
C
113 return next()
114 }
115]
116
117const uninstallPluginValidator = [
118 body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
119
120 (req: express.Request, res: express.Response, next: express.NextFunction) => {
dba85a1e 121 logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body })
ad91e700
C
122
123 if (areValidationErrors(req, res)) return
124
125 return next()
126 }
127]
128
dba85a1e 129const existingPluginValidator = [
60cfd4cb 130 param('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid plugin name'),
ad91e700
C
131
132 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
dba85a1e 133 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params })
ad91e700
C
134
135 if (areValidationErrors(req, res)) return
136
dba85a1e 137 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
ad91e700
C
138 if (!plugin) {
139 return res.status(404)
140 .json({ error: 'Plugin not found' })
141 .end()
142 }
143
144 res.locals.plugin = plugin
145
146 return next()
147 }
148]
149
150const updatePluginSettingsValidator = [
151 body('settings').exists().withMessage('Should have settings'),
152
153 (req: express.Request, res: express.Response, next: express.NextFunction) => {
154 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body })
155
156 if (areValidationErrors(req, res)) return
157
158 return next()
159 }
160]
161
6702a1b2 162const listAvailablePluginsValidator = [
6702a1b2
C
163 query('search')
164 .optional()
165 .exists().withMessage('Should have a valid search'),
166 query('pluginType')
167 .optional()
168 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
09071200
C
169 query('currentPeerTubeEngine')
170 .optional()
171 .custom(isPluginVersionValid).withMessage('Should have a valid current peertube engine'),
6702a1b2
C
172
173 (req: express.Request, res: express.Response, next: express.NextFunction) => {
174 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.query })
175
176 if (areValidationErrors(req, res)) return
177
178 if (CONFIG.PLUGINS.INDEX.ENABLED === false) {
179 return res.status(400)
180 .json({ error: 'Plugin index is not enabled' })
181 .end()
182 }
183
184 return next()
185 }
186]
187
345da516
C
188// ---------------------------------------------------------------------------
189
190export {
5e2b2e27
C
191 pluginStaticDirectoryValidator,
192 getPluginValidator,
ad91e700
C
193 updatePluginSettingsValidator,
194 uninstallPluginValidator,
6702a1b2 195 listAvailablePluginsValidator,
dba85a1e 196 existingPluginValidator,
b5f919ac 197 installOrUpdatePluginValidator,
4a8d113b
C
198 listPluginsValidator,
199 getExternalAuthValidator
345da516 200}