]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/plugins.ts
Begin support for external auths
[github/Chocobozzz/PeerTube.git] / server / controllers / plugins.ts
CommitLineData
345da516
C
1import * as express from 'express'
2import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants'
b5f919ac 3import { join } from 'path'
d75db01f 4import { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager'
4a8d113b 5import { getPluginValidator, pluginStaticDirectoryValidator, getExternalAuthValidator } from '../middlewares/validators/plugins'
b5f919ac
C
6import { serveThemeCSSValidator } from '../middlewares/validators/themes'
7import { PluginType } from '../../shared/models/plugins/plugin.type'
a8b666e9 8import { isTestInstance } from '../helpers/core-utils'
d75db01f 9import { getCompleteLocale, is18nLocale } from '../../shared/models/i18n'
4a8d113b 10import { logger } from '@server/helpers/logger'
a8b666e9
C
11
12const sendFileOptions = {
13 maxAge: '30 days',
14 immutable: !isTestInstance()
15}
345da516
C
16
17const pluginsRouter = express.Router()
18
b5f919ac 19pluginsRouter.get('/plugins/global.css',
2c053942 20 servePluginGlobalCSS
345da516
C
21)
22
d75db01f
C
23pluginsRouter.get('/plugins/translations/:locale.json',
24 getPluginTranslations
25)
26
4a8d113b
C
27pluginsRouter.get('/plugins/:pluginName/:pluginVersion/auth/:authName',
28 getPluginValidator(PluginType.PLUGIN),
29 getExternalAuthValidator,
30 handleAuthInPlugin
31)
32
b5f919ac 33pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
5e2b2e27
C
34 getPluginValidator(PluginType.PLUGIN),
35 pluginStaticDirectoryValidator,
345da516
C
36 servePluginStaticDirectory
37)
38
b5f919ac 39pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
5e2b2e27
C
40 getPluginValidator(PluginType.PLUGIN),
41 pluginStaticDirectoryValidator,
345da516
C
42 servePluginClientScripts
43)
44
5e2b2e27
C
45pluginsRouter.use('/plugins/:pluginName/router',
46 getPluginValidator(PluginType.PLUGIN, false),
47 servePluginCustomRoutes
48)
49
50pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router',
51 getPluginValidator(PluginType.PLUGIN),
52 servePluginCustomRoutes
53)
54
b5f919ac 55pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
5e2b2e27
C
56 getPluginValidator(PluginType.THEME),
57 pluginStaticDirectoryValidator,
b5f919ac
C
58 servePluginStaticDirectory
59)
60
61pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
5e2b2e27
C
62 getPluginValidator(PluginType.THEME),
63 pluginStaticDirectoryValidator,
b5f919ac
C
64 servePluginClientScripts
65)
66
67pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)',
68 serveThemeCSSValidator,
69 serveThemeCSSDirectory
70)
71
345da516
C
72// ---------------------------------------------------------------------------
73
74export {
75 pluginsRouter
76}
77
78// ---------------------------------------------------------------------------
79
2c053942 80function servePluginGlobalCSS (req: express.Request, res: express.Response) {
a8b666e9
C
81 // Only cache requests that have a ?hash=... query param
82 const globalCSSOptions = req.query.hash
83 ? sendFileOptions
84 : {}
85
86 return res.sendFile(PLUGIN_GLOBAL_CSS_PATH, globalCSSOptions)
2c053942
C
87}
88
d75db01f
C
89function getPluginTranslations (req: express.Request, res: express.Response) {
90 const locale = req.params.locale
91
92 if (is18nLocale(locale)) {
93 const completeLocale = getCompleteLocale(locale)
94 const json = PluginManager.Instance.getTranslations(completeLocale)
95
96 return res.json(json)
97 }
98
99 return res.sendStatus(404)
100}
101
345da516
C
102function servePluginStaticDirectory (req: express.Request, res: express.Response) {
103 const plugin: RegisteredPlugin = res.locals.registeredPlugin
104 const staticEndpoint = req.params.staticEndpoint
105
2c053942
C
106 const [ directory, ...file ] = staticEndpoint.split('/')
107
108 const staticPath = plugin.staticDirs[directory]
5e2b2e27 109 if (!staticPath) return res.sendStatus(404)
345da516 110
2c053942 111 const filepath = file.join('/')
a8b666e9 112 return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions)
345da516
C
113}
114
5e2b2e27
C
115function 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
345da516
C
124function servePluginClientScripts (req: express.Request, res: express.Response) {
125 const plugin: RegisteredPlugin = res.locals.registeredPlugin
126 const staticEndpoint = req.params.staticEndpoint
127
2c053942 128 const file = plugin.clientScripts[staticEndpoint]
5e2b2e27 129 if (!file) return res.sendStatus(404)
2c053942 130
a8b666e9 131 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
345da516 132}
b5f919ac
C
133
134function serveThemeCSSDirectory (req: express.Request, res: express.Response) {
135 const plugin: RegisteredPlugin = res.locals.registeredPlugin
136 const staticEndpoint = req.params.staticEndpoint
137
138 if (plugin.css.includes(staticEndpoint) === false) {
139 return res.sendStatus(404)
140 }
141
a8b666e9 142 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
b5f919ac 143}
4a8d113b
C
144
145function 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)
153 }
154}