]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/plugins.ts
Refactor auth flow
[github/Chocobozzz/PeerTube.git] / server / controllers / plugins.ts
index 05f03324d80f69f8e2b4095925a4b85678784c42..105f515184130196a093d5f2d842448e5e7542ca 100644 (file)
@@ -1,25 +1,78 @@
 import * as express from 'express'
+import { join } from 'path'
+import { logger } from '@server/helpers/logger'
+import { optionalAuthenticate } from '@server/middlewares/auth'
+import { getCompleteLocale, is18nLocale } from '../../shared/core-utils/i18n'
+import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
+import { PluginType } from '../../shared/models/plugins/plugin.type'
+import { isTestInstance } from '../helpers/core-utils'
 import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants'
-import { basename, 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 { getExternalAuthValidator, getPluginValidator, pluginStaticDirectoryValidator } from '../middlewares/validators/plugins'
+import { serveThemeCSSValidator } from '../middlewares/validators/themes'
+
+const sendFileOptions = {
+  maxAge: '30 days',
+  immutable: !isTestInstance()
+}
 
 const pluginsRouter = express.Router()
 
-pluginsRouter.get('/global.css',
+pluginsRouter.get('/plugins/global.css',
   servePluginGlobalCSS
 )
 
-pluginsRouter.get('/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
-  servePluginStaticDirectoryValidator,
+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(*)',
+  getPluginValidator(PluginType.PLUGIN),
+  pluginStaticDirectoryValidator,
   servePluginStaticDirectory
 )
 
-pluginsRouter.get('/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
-  servePluginStaticDirectoryValidator,
+pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
+  getPluginValidator(PluginType.PLUGIN),
+  pluginStaticDirectoryValidator,
   servePluginClientScripts
 )
 
+pluginsRouter.use('/plugins/:pluginName/router',
+  getPluginValidator(PluginType.PLUGIN, false),
+  optionalAuthenticate,
+  servePluginCustomRoutes
+)
+
+pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router',
+  getPluginValidator(PluginType.PLUGIN),
+  optionalAuthenticate,
+  servePluginCustomRoutes
+)
+
+pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
+  getPluginValidator(PluginType.THEME),
+  pluginStaticDirectoryValidator,
+  servePluginStaticDirectory
+)
+
+pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
+  getPluginValidator(PluginType.THEME),
+  pluginStaticDirectoryValidator,
+  servePluginClientScripts
+)
+
+pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)',
+  serveThemeCSSValidator,
+  serveThemeCSSDirectory
+)
+
 // ---------------------------------------------------------------------------
 
 export {
@@ -29,7 +82,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(HttpStatusCode.NOT_FOUND_404)
 }
 
 function servePluginStaticDirectory (req: express.Request, res: express.Response) {
@@ -39,12 +110,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(HttpStatusCode.NOT_FOUND_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(HttpStatusCode.NOT_FOUND_404)
+
+  return router(req, res, next)
 }
 
 function servePluginClientScripts (req: express.Request, res: express.Response) {
@@ -52,9 +130,29 @@ 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(HttpStatusCode.NOT_FOUND_404)
+
+  return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
+}
+
+function serveThemeCSSDirectory (req: express.Request, res: express.Response) {
+  const plugin: RegisteredPlugin = res.locals.registeredPlugin
+  const staticEndpoint = req.params.staticEndpoint
+
+  if (plugin.css.includes(staticEndpoint) === false) {
+    return res.sendStatus(HttpStatusCode.NOT_FOUND_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 })
+  }
 }