diff options
Diffstat (limited to 'server/controllers/plugins.ts')
-rw-r--r-- | server/controllers/plugins.ts | 175 |
1 files changed, 0 insertions, 175 deletions
diff --git a/server/controllers/plugins.ts b/server/controllers/plugins.ts deleted file mode 100644 index f0491b16a..000000000 --- a/server/controllers/plugins.ts +++ /dev/null | |||
@@ -1,175 +0,0 @@ | |||
1 | import express from 'express' | ||
2 | import { join } from 'path' | ||
3 | import { logger } from '@server/helpers/logger' | ||
4 | import { CONFIG } from '@server/initializers/config' | ||
5 | import { buildRateLimiter } from '@server/middlewares' | ||
6 | import { optionalAuthenticate } from '@server/middlewares/auth' | ||
7 | import { getCompleteLocale, is18nLocale } from '../../shared/core-utils/i18n' | ||
8 | import { HttpStatusCode } from '../../shared/models/http/http-error-codes' | ||
9 | import { PluginType } from '../../shared/models/plugins/plugin.type' | ||
10 | import { isProdInstance } from '../helpers/core-utils' | ||
11 | import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants' | ||
12 | import { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager' | ||
13 | import { getExternalAuthValidator, getPluginValidator, pluginStaticDirectoryValidator } from '../middlewares/validators/plugins' | ||
14 | import { serveThemeCSSValidator } from '../middlewares/validators/themes' | ||
15 | |||
16 | const sendFileOptions = { | ||
17 | maxAge: '30 days', | ||
18 | immutable: isProdInstance() | ||
19 | } | ||
20 | |||
21 | const pluginsRouter = express.Router() | ||
22 | |||
23 | const pluginsRateLimiter = buildRateLimiter({ | ||
24 | windowMs: CONFIG.RATES_LIMIT.PLUGINS.WINDOW_MS, | ||
25 | max: CONFIG.RATES_LIMIT.PLUGINS.MAX | ||
26 | }) | ||
27 | |||
28 | pluginsRouter.get('/plugins/global.css', | ||
29 | pluginsRateLimiter, | ||
30 | servePluginGlobalCSS | ||
31 | ) | ||
32 | |||
33 | pluginsRouter.get('/plugins/translations/:locale.json', | ||
34 | pluginsRateLimiter, | ||
35 | getPluginTranslations | ||
36 | ) | ||
37 | |||
38 | pluginsRouter.get('/plugins/:pluginName/:pluginVersion/auth/:authName', | ||
39 | pluginsRateLimiter, | ||
40 | getPluginValidator(PluginType.PLUGIN), | ||
41 | getExternalAuthValidator, | ||
42 | handleAuthInPlugin | ||
43 | ) | ||
44 | |||
45 | pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)', | ||
46 | pluginsRateLimiter, | ||
47 | getPluginValidator(PluginType.PLUGIN), | ||
48 | pluginStaticDirectoryValidator, | ||
49 | servePluginStaticDirectory | ||
50 | ) | ||
51 | |||
52 | pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', | ||
53 | pluginsRateLimiter, | ||
54 | getPluginValidator(PluginType.PLUGIN), | ||
55 | pluginStaticDirectoryValidator, | ||
56 | servePluginClientScripts | ||
57 | ) | ||
58 | |||
59 | pluginsRouter.use('/plugins/:pluginName/router', | ||
60 | pluginsRateLimiter, | ||
61 | getPluginValidator(PluginType.PLUGIN, false), | ||
62 | optionalAuthenticate, | ||
63 | servePluginCustomRoutes | ||
64 | ) | ||
65 | |||
66 | pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router', | ||
67 | pluginsRateLimiter, | ||
68 | getPluginValidator(PluginType.PLUGIN), | ||
69 | optionalAuthenticate, | ||
70 | servePluginCustomRoutes | ||
71 | ) | ||
72 | |||
73 | pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)', | ||
74 | pluginsRateLimiter, | ||
75 | getPluginValidator(PluginType.THEME), | ||
76 | pluginStaticDirectoryValidator, | ||
77 | servePluginStaticDirectory | ||
78 | ) | ||
79 | |||
80 | pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', | ||
81 | pluginsRateLimiter, | ||
82 | getPluginValidator(PluginType.THEME), | ||
83 | pluginStaticDirectoryValidator, | ||
84 | servePluginClientScripts | ||
85 | ) | ||
86 | |||
87 | pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)', | ||
88 | pluginsRateLimiter, | ||
89 | serveThemeCSSValidator, | ||
90 | serveThemeCSSDirectory | ||
91 | ) | ||
92 | |||
93 | // --------------------------------------------------------------------------- | ||
94 | |||
95 | export { | ||
96 | pluginsRouter | ||
97 | } | ||
98 | |||
99 | // --------------------------------------------------------------------------- | ||
100 | |||
101 | function servePluginGlobalCSS (req: express.Request, res: express.Response) { | ||
102 | // Only cache requests that have a ?hash=... query param | ||
103 | const globalCSSOptions = req.query.hash | ||
104 | ? sendFileOptions | ||
105 | : {} | ||
106 | |||
107 | return res.sendFile(PLUGIN_GLOBAL_CSS_PATH, globalCSSOptions) | ||
108 | } | ||
109 | |||
110 | function getPluginTranslations (req: express.Request, res: express.Response) { | ||
111 | const locale = req.params.locale | ||
112 | |||
113 | if (is18nLocale(locale)) { | ||
114 | const completeLocale = getCompleteLocale(locale) | ||
115 | const json = PluginManager.Instance.getTranslations(completeLocale) | ||
116 | |||
117 | return res.json(json) | ||
118 | } | ||
119 | |||
120 | return res.status(HttpStatusCode.NOT_FOUND_404).end() | ||
121 | } | ||
122 | |||
123 | function servePluginStaticDirectory (req: express.Request, res: express.Response) { | ||
124 | const plugin: RegisteredPlugin = res.locals.registeredPlugin | ||
125 | const staticEndpoint = req.params.staticEndpoint | ||
126 | |||
127 | const [ directory, ...file ] = staticEndpoint.split('/') | ||
128 | |||
129 | const staticPath = plugin.staticDirs[directory] | ||
130 | if (!staticPath) return res.status(HttpStatusCode.NOT_FOUND_404).end() | ||
131 | |||
132 | const filepath = file.join('/') | ||
133 | return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions) | ||
134 | } | ||
135 | |||
136 | function servePluginCustomRoutes (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
137 | const plugin: RegisteredPlugin = res.locals.registeredPlugin | ||
138 | const router = PluginManager.Instance.getRouter(plugin.npmName) | ||
139 | |||
140 | if (!router) return res.status(HttpStatusCode.NOT_FOUND_404).end() | ||
141 | |||
142 | return router(req, res, next) | ||
143 | } | ||
144 | |||
145 | function servePluginClientScripts (req: express.Request, res: express.Response) { | ||
146 | const plugin: RegisteredPlugin = res.locals.registeredPlugin | ||
147 | const staticEndpoint = req.params.staticEndpoint | ||
148 | |||
149 | const file = plugin.clientScripts[staticEndpoint] | ||
150 | if (!file) return res.status(HttpStatusCode.NOT_FOUND_404).end() | ||
151 | |||
152 | return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions) | ||
153 | } | ||
154 | |||
155 | function serveThemeCSSDirectory (req: express.Request, res: express.Response) { | ||
156 | const plugin: RegisteredPlugin = res.locals.registeredPlugin | ||
157 | const staticEndpoint = req.params.staticEndpoint | ||
158 | |||
159 | if (plugin.css.includes(staticEndpoint) === false) { | ||
160 | return res.status(HttpStatusCode.NOT_FOUND_404).end() | ||
161 | } | ||
162 | |||
163 | return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions) | ||
164 | } | ||
165 | |||
166 | function handleAuthInPlugin (req: express.Request, res: express.Response) { | ||
167 | const authOptions = res.locals.externalAuth | ||
168 | |||
169 | try { | ||
170 | logger.debug('Forwarding auth plugin request in %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName) | ||
171 | authOptions.onAuthRequest(req, res) | ||
172 | } catch (err) { | ||
173 | logger.error('Forward request error in auth %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName, { err }) | ||
174 | } | ||
175 | } | ||