diff options
Diffstat (limited to 'server/controllers/plugins.ts')
-rw-r--r-- | server/controllers/plugins.ts | 59 |
1 files changed, 48 insertions, 11 deletions
diff --git a/server/controllers/plugins.ts b/server/controllers/plugins.ts index 1caee9a29..f88a1632d 100644 --- a/server/controllers/plugins.ts +++ b/server/controllers/plugins.ts | |||
@@ -2,11 +2,12 @@ import * as express from 'express' | |||
2 | import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants' | 2 | import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants' |
3 | import { join } from 'path' | 3 | import { join } from 'path' |
4 | import { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager' | 4 | import { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager' |
5 | import { servePluginStaticDirectoryValidator } from '../middlewares/validators/plugins' | 5 | import { getPluginValidator, pluginStaticDirectoryValidator, getExternalAuthValidator } from '../middlewares/validators/plugins' |
6 | import { serveThemeCSSValidator } from '../middlewares/validators/themes' | 6 | import { serveThemeCSSValidator } from '../middlewares/validators/themes' |
7 | import { PluginType } from '../../shared/models/plugins/plugin.type' | 7 | import { PluginType } from '../../shared/models/plugins/plugin.type' |
8 | import { isTestInstance } from '../helpers/core-utils' | 8 | import { isTestInstance } from '../helpers/core-utils' |
9 | import { getCompleteLocale, is18nLocale } from '../../shared/models/i18n' | 9 | import { getCompleteLocale, is18nLocale } from '../../shared/models/i18n' |
10 | import { logger } from '@server/helpers/logger' | ||
10 | 11 | ||
11 | const sendFileOptions = { | 12 | const sendFileOptions = { |
12 | maxAge: '30 days', | 13 | maxAge: '30 days', |
@@ -23,23 +24,43 @@ pluginsRouter.get('/plugins/translations/:locale.json', | |||
23 | getPluginTranslations | 24 | getPluginTranslations |
24 | ) | 25 | ) |
25 | 26 | ||
27 | pluginsRouter.get('/plugins/:pluginName/:pluginVersion/auth/:authName', | ||
28 | getPluginValidator(PluginType.PLUGIN), | ||
29 | getExternalAuthValidator, | ||
30 | handleAuthInPlugin | ||
31 | ) | ||
32 | |||
26 | pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)', | 33 | pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)', |
27 | servePluginStaticDirectoryValidator(PluginType.PLUGIN), | 34 | getPluginValidator(PluginType.PLUGIN), |
35 | pluginStaticDirectoryValidator, | ||
28 | servePluginStaticDirectory | 36 | servePluginStaticDirectory |
29 | ) | 37 | ) |
30 | 38 | ||
31 | pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', | 39 | pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', |
32 | servePluginStaticDirectoryValidator(PluginType.PLUGIN), | 40 | getPluginValidator(PluginType.PLUGIN), |
41 | pluginStaticDirectoryValidator, | ||
33 | servePluginClientScripts | 42 | servePluginClientScripts |
34 | ) | 43 | ) |
35 | 44 | ||
45 | pluginsRouter.use('/plugins/:pluginName/router', | ||
46 | getPluginValidator(PluginType.PLUGIN, false), | ||
47 | servePluginCustomRoutes | ||
48 | ) | ||
49 | |||
50 | pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router', | ||
51 | getPluginValidator(PluginType.PLUGIN), | ||
52 | servePluginCustomRoutes | ||
53 | ) | ||
54 | |||
36 | pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)', | 55 | pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)', |
37 | servePluginStaticDirectoryValidator(PluginType.THEME), | 56 | getPluginValidator(PluginType.THEME), |
57 | pluginStaticDirectoryValidator, | ||
38 | servePluginStaticDirectory | 58 | servePluginStaticDirectory |
39 | ) | 59 | ) |
40 | 60 | ||
41 | pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', | 61 | pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', |
42 | servePluginStaticDirectoryValidator(PluginType.THEME), | 62 | getPluginValidator(PluginType.THEME), |
63 | pluginStaticDirectoryValidator, | ||
43 | servePluginClientScripts | 64 | servePluginClientScripts |
44 | ) | 65 | ) |
45 | 66 | ||
@@ -85,22 +106,27 @@ function servePluginStaticDirectory (req: express.Request, res: express.Response | |||
85 | const [ directory, ...file ] = staticEndpoint.split('/') | 106 | const [ directory, ...file ] = staticEndpoint.split('/') |
86 | 107 | ||
87 | const staticPath = plugin.staticDirs[directory] | 108 | const staticPath = plugin.staticDirs[directory] |
88 | if (!staticPath) { | 109 | if (!staticPath) return res.sendStatus(404) |
89 | return res.sendStatus(404) | ||
90 | } | ||
91 | 110 | ||
92 | const filepath = file.join('/') | 111 | const filepath = file.join('/') |
93 | return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions) | 112 | return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions) |
94 | } | 113 | } |
95 | 114 | ||
115 | function servePluginCustomRoutes (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
116 | const plugin: RegisteredPlugin = res.locals.registeredPlugin | ||
117 | const router = PluginManager.Instance.getRouter(plugin.npmName) | ||
118 | |||
119 | if (!router) return res.sendStatus(404) | ||
120 | |||
121 | return router(req, res, next) | ||
122 | } | ||
123 | |||
96 | function servePluginClientScripts (req: express.Request, res: express.Response) { | 124 | function servePluginClientScripts (req: express.Request, res: express.Response) { |
97 | const plugin: RegisteredPlugin = res.locals.registeredPlugin | 125 | const plugin: RegisteredPlugin = res.locals.registeredPlugin |
98 | const staticEndpoint = req.params.staticEndpoint | 126 | const staticEndpoint = req.params.staticEndpoint |
99 | 127 | ||
100 | const file = plugin.clientScripts[staticEndpoint] | 128 | const file = plugin.clientScripts[staticEndpoint] |
101 | if (!file) { | 129 | if (!file) return res.sendStatus(404) |
102 | return res.sendStatus(404) | ||
103 | } | ||
104 | 130 | ||
105 | return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions) | 131 | return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions) |
106 | } | 132 | } |
@@ -115,3 +141,14 @@ function serveThemeCSSDirectory (req: express.Request, res: express.Response) { | |||
115 | 141 | ||
116 | return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions) | 142 | return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions) |
117 | } | 143 | } |
144 | |||
145 | function handleAuthInPlugin (req: express.Request, res: express.Response) { | ||
146 | const authOptions = res.locals.externalAuth | ||
147 | |||
148 | try { | ||
149 | logger.debug('Forwarding auth plugin request in %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName) | ||
150 | authOptions.onAuthRequest(req, res) | ||
151 | } catch (err) { | ||
152 | logger.error('Forward request error in auth %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName, { err }) | ||
153 | } | ||
154 | } | ||