X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fplugins.ts;h=f88a1632d7b3d5903be581a7d7f9ffc93da21c21;hb=b84d4c809f0b1c4b3f2029f56b81dbca37364f36;hp=f255d13e840ec3b279ca7b06583534cf853b3186;hpb=b5f919ac8eb2a1c20e26582fdfd377d687710d8f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/plugins.ts b/server/controllers/plugins.ts index f255d13e8..f88a1632d 100644 --- a/server/controllers/plugins.ts +++ b/server/controllers/plugins.ts @@ -1,10 +1,18 @@ import * as express from 'express' import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants' import { join } from 'path' -import { RegisteredPlugin } from '../lib/plugins/plugin-manager' -import { servePluginStaticDirectoryValidator } from '../middlewares/validators/plugins' +import { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager' +import { getPluginValidator, pluginStaticDirectoryValidator, getExternalAuthValidator } from '../middlewares/validators/plugins' import { serveThemeCSSValidator } from '../middlewares/validators/themes' import { PluginType } from '../../shared/models/plugins/plugin.type' +import { isTestInstance } from '../helpers/core-utils' +import { getCompleteLocale, is18nLocale } from '../../shared/models/i18n' +import { logger } from '@server/helpers/logger' + +const sendFileOptions = { + maxAge: '30 days', + immutable: !isTestInstance() +} const pluginsRouter = express.Router() @@ -12,23 +20,47 @@ pluginsRouter.get('/plugins/global.css', servePluginGlobalCSS ) +pluginsRouter.get('/plugins/translations/:locale.json', + getPluginTranslations +) + +pluginsRouter.get('/plugins/:pluginName/:pluginVersion/auth/:authName', + getPluginValidator(PluginType.PLUGIN), + getExternalAuthValidator, + handleAuthInPlugin +) + pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)', - servePluginStaticDirectoryValidator(PluginType.PLUGIN), + getPluginValidator(PluginType.PLUGIN), + pluginStaticDirectoryValidator, servePluginStaticDirectory ) pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', - servePluginStaticDirectoryValidator(PluginType.PLUGIN), + getPluginValidator(PluginType.PLUGIN), + pluginStaticDirectoryValidator, servePluginClientScripts ) +pluginsRouter.use('/plugins/:pluginName/router', + getPluginValidator(PluginType.PLUGIN, false), + servePluginCustomRoutes +) + +pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router', + getPluginValidator(PluginType.PLUGIN), + servePluginCustomRoutes +) + pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)', - servePluginStaticDirectoryValidator(PluginType.THEME), + getPluginValidator(PluginType.THEME), + pluginStaticDirectoryValidator, servePluginStaticDirectory ) pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', - servePluginStaticDirectoryValidator(PluginType.THEME), + getPluginValidator(PluginType.THEME), + pluginStaticDirectoryValidator, servePluginClientScripts ) @@ -46,7 +78,25 @@ export { // --------------------------------------------------------------------------- function servePluginGlobalCSS (req: express.Request, res: express.Response) { - return res.sendFile(PLUGIN_GLOBAL_CSS_PATH) + // Only cache requests that have a ?hash=... query param + const globalCSSOptions = req.query.hash + ? sendFileOptions + : {} + + return res.sendFile(PLUGIN_GLOBAL_CSS_PATH, globalCSSOptions) +} + +function getPluginTranslations (req: express.Request, res: express.Response) { + const locale = req.params.locale + + if (is18nLocale(locale)) { + const completeLocale = getCompleteLocale(locale) + const json = PluginManager.Instance.getTranslations(completeLocale) + + return res.json(json) + } + + return res.sendStatus(404) } function servePluginStaticDirectory (req: express.Request, res: express.Response) { @@ -56,12 +106,19 @@ function servePluginStaticDirectory (req: express.Request, res: express.Response const [ directory, ...file ] = staticEndpoint.split('/') const staticPath = plugin.staticDirs[directory] - if (!staticPath) { - return res.sendStatus(404) - } + if (!staticPath) return res.sendStatus(404) const filepath = file.join('/') - return res.sendFile(join(plugin.path, staticPath, filepath)) + return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions) +} + +function servePluginCustomRoutes (req: express.Request, res: express.Response, next: express.NextFunction) { + const plugin: RegisteredPlugin = res.locals.registeredPlugin + const router = PluginManager.Instance.getRouter(plugin.npmName) + + if (!router) return res.sendStatus(404) + + return router(req, res, next) } function servePluginClientScripts (req: express.Request, res: express.Response) { @@ -69,11 +126,9 @@ function servePluginClientScripts (req: express.Request, res: express.Response) const staticEndpoint = req.params.staticEndpoint const file = plugin.clientScripts[staticEndpoint] - if (!file) { - return res.sendStatus(404) - } + if (!file) return res.sendStatus(404) - return res.sendFile(join(plugin.path, staticEndpoint)) + return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions) } function serveThemeCSSDirectory (req: express.Request, res: express.Response) { @@ -84,5 +139,16 @@ function serveThemeCSSDirectory (req: express.Request, res: express.Response) { return res.sendStatus(404) } - return res.sendFile(join(plugin.path, staticEndpoint)) + return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions) +} + +function handleAuthInPlugin (req: express.Request, res: express.Response) { + const authOptions = res.locals.externalAuth + + try { + logger.debug('Forwarding auth plugin request in %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName) + authOptions.onAuthRequest(req, res) + } catch (err) { + logger.error('Forward request error in auth %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName, { err }) + } }