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