]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/plugins.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / controllers / plugins.ts
index 1caee9a29fcbb10d2fdd5fbb0f09121f154b72fe..28fffc9e1dfcee68e0d886e3b655fdf69583b236 100644 (file)
@@ -1,12 +1,15 @@
-import * as express from 'express'
-import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants'
+import express from 'express'
 import { join } from 'path'
-import { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager'
-import { servePluginStaticDirectoryValidator } from '../middlewares/validators/plugins'
-import { serveThemeCSSValidator } from '../middlewares/validators/themes'
+import { logger } from '@server/helpers/logger'
+import { optionalAuthenticate } from '@server/middlewares/auth'
+import { getCompleteLocale, is18nLocale } from '../../shared/core-utils/i18n'
+import { HttpStatusCode } from '../../shared/models/http/http-error-codes'
 import { PluginType } from '../../shared/models/plugins/plugin.type'
 import { isTestInstance } from '../helpers/core-utils'
-import { getCompleteLocale, is18nLocale } from '../../shared/models/i18n'
+import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants'
+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',
@@ -23,23 +26,45 @@ 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),
+  optionalAuthenticate,
+  servePluginCustomRoutes
+)
+
+pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router',
+  getPluginValidator(PluginType.PLUGIN),
+  optionalAuthenticate,
+  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
 )
 
@@ -75,7 +100,7 @@ function getPluginTranslations (req: express.Request, res: express.Response) {
     return res.json(json)
   }
 
-  return res.sendStatus(404)
+  return res.status(HttpStatusCode.NOT_FOUND_404).end()
 }
 
 function servePluginStaticDirectory (req: express.Request, res: express.Response) {
@@ -85,22 +110,27 @@ 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.status(HttpStatusCode.NOT_FOUND_404).end()
 
   const filepath = file.join('/')
   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.status(HttpStatusCode.NOT_FOUND_404).end()
+
+  return router(req, res, next)
+}
+
 function servePluginClientScripts (req: express.Request, res: express.Response) {
   const plugin: RegisteredPlugin = res.locals.registeredPlugin
   const staticEndpoint = req.params.staticEndpoint
 
   const file = plugin.clientScripts[staticEndpoint]
-  if (!file) {
-    return res.sendStatus(404)
-  }
+  if (!file) return res.status(HttpStatusCode.NOT_FOUND_404).end()
 
   return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
 }
@@ -110,8 +140,19 @@ function serveThemeCSSDirectory (req: express.Request, res: express.Response) {
   const staticEndpoint = req.params.staticEndpoint
 
   if (plugin.css.includes(staticEndpoint) === false) {
-    return res.sendStatus(404)
+    return res.status(HttpStatusCode.NOT_FOUND_404).end()
   }
 
   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 })
+  }
+}