plugins.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import * as express from 'express'
  2. import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants'
  3. import { join } from 'path'
  4. import { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager'
  5. import { getPluginValidator, pluginStaticDirectoryValidator, getExternalAuthValidator } from '../middlewares/validators/plugins'
  6. import { serveThemeCSSValidator } from '../middlewares/validators/themes'
  7. import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
  8. import { getCompleteLocale, is18nLocale } from '../../shared/core-utils/i18n'
  9. import { PluginType } from '../../shared/models/plugins/plugin.type'
  10. import { isTestInstance } from '../helpers/core-utils'
  11. import { logger } from '@server/helpers/logger'
  12. import { optionalAuthenticate } from '@server/middlewares/oauth'
  13. const sendFileOptions = {
  14. maxAge: '30 days',
  15. immutable: !isTestInstance()
  16. }
  17. const pluginsRouter = express.Router()
  18. pluginsRouter.get('/plugins/global.css',
  19. servePluginGlobalCSS
  20. )
  21. pluginsRouter.get('/plugins/translations/:locale.json',
  22. getPluginTranslations
  23. )
  24. pluginsRouter.get('/plugins/:pluginName/:pluginVersion/auth/:authName',
  25. getPluginValidator(PluginType.PLUGIN),
  26. getExternalAuthValidator,
  27. handleAuthInPlugin
  28. )
  29. pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
  30. getPluginValidator(PluginType.PLUGIN),
  31. pluginStaticDirectoryValidator,
  32. servePluginStaticDirectory
  33. )
  34. pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
  35. getPluginValidator(PluginType.PLUGIN),
  36. pluginStaticDirectoryValidator,
  37. servePluginClientScripts
  38. )
  39. pluginsRouter.use('/plugins/:pluginName/router',
  40. getPluginValidator(PluginType.PLUGIN, false),
  41. optionalAuthenticate,
  42. servePluginCustomRoutes
  43. )
  44. pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router',
  45. getPluginValidator(PluginType.PLUGIN),
  46. optionalAuthenticate,
  47. servePluginCustomRoutes
  48. )
  49. pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
  50. getPluginValidator(PluginType.THEME),
  51. pluginStaticDirectoryValidator,
  52. servePluginStaticDirectory
  53. )
  54. pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
  55. getPluginValidator(PluginType.THEME),
  56. pluginStaticDirectoryValidator,
  57. servePluginClientScripts
  58. )
  59. pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)',
  60. serveThemeCSSValidator,
  61. serveThemeCSSDirectory
  62. )
  63. // ---------------------------------------------------------------------------
  64. export {
  65. pluginsRouter
  66. }
  67. // ---------------------------------------------------------------------------
  68. function servePluginGlobalCSS (req: express.Request, res: express.Response) {
  69. // Only cache requests that have a ?hash=... query param
  70. const globalCSSOptions = req.query.hash
  71. ? sendFileOptions
  72. : {}
  73. return res.sendFile(PLUGIN_GLOBAL_CSS_PATH, globalCSSOptions)
  74. }
  75. function getPluginTranslations (req: express.Request, res: express.Response) {
  76. const locale = req.params.locale
  77. if (is18nLocale(locale)) {
  78. const completeLocale = getCompleteLocale(locale)
  79. const json = PluginManager.Instance.getTranslations(completeLocale)
  80. return res.json(json)
  81. }
  82. return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
  83. }
  84. function servePluginStaticDirectory (req: express.Request, res: express.Response) {
  85. const plugin: RegisteredPlugin = res.locals.registeredPlugin
  86. const staticEndpoint = req.params.staticEndpoint
  87. const [ directory, ...file ] = staticEndpoint.split('/')
  88. const staticPath = plugin.staticDirs[directory]
  89. if (!staticPath) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
  90. const filepath = file.join('/')
  91. return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions)
  92. }
  93. function servePluginCustomRoutes (req: express.Request, res: express.Response, next: express.NextFunction) {
  94. const plugin: RegisteredPlugin = res.locals.registeredPlugin
  95. const router = PluginManager.Instance.getRouter(plugin.npmName)
  96. if (!router) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
  97. return router(req, res, next)
  98. }
  99. function servePluginClientScripts (req: express.Request, res: express.Response) {
  100. const plugin: RegisteredPlugin = res.locals.registeredPlugin
  101. const staticEndpoint = req.params.staticEndpoint
  102. const file = plugin.clientScripts[staticEndpoint]
  103. if (!file) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
  104. return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
  105. }
  106. function serveThemeCSSDirectory (req: express.Request, res: express.Response) {
  107. const plugin: RegisteredPlugin = res.locals.registeredPlugin
  108. const staticEndpoint = req.params.staticEndpoint
  109. if (plugin.css.includes(staticEndpoint) === false) {
  110. return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
  111. }
  112. return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
  113. }
  114. function handleAuthInPlugin (req: express.Request, res: express.Response) {
  115. const authOptions = res.locals.externalAuth
  116. try {
  117. logger.debug('Forwarding auth plugin request in %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName)
  118. authOptions.onAuthRequest(req, res)
  119. } catch (err) {
  120. logger.error('Forward request error in auth %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName, { err })
  121. }
  122. }