plugins: add optional authentication for routes

This commit is contained in:
kontrollanten 2020-12-04 20:56:48 +01:00 committed by Chocobozzz
parent c824e8a0c7
commit f17faefb30
3 changed files with 26 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import { getCompleteLocale, is18nLocale } from '../../shared/core-utils/i18n'
import { PluginType } from '../../shared/models/plugins/plugin.type' import { PluginType } from '../../shared/models/plugins/plugin.type'
import { isTestInstance } from '../helpers/core-utils' import { isTestInstance } from '../helpers/core-utils'
import { logger } from '@server/helpers/logger' import { logger } from '@server/helpers/logger'
import { optionalAuthenticate } from '@server/middlewares/oauth'
const sendFileOptions = { const sendFileOptions = {
maxAge: '30 days', maxAge: '30 days',
@ -45,11 +46,13 @@ pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEnd
pluginsRouter.use('/plugins/:pluginName/router', pluginsRouter.use('/plugins/:pluginName/router',
getPluginValidator(PluginType.PLUGIN, false), getPluginValidator(PluginType.PLUGIN, false),
optionalAuthenticate,
servePluginCustomRoutes servePluginCustomRoutes
) )
pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router', pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router',
getPluginValidator(PluginType.PLUGIN), getPluginValidator(PluginType.PLUGIN),
optionalAuthenticate,
servePluginCustomRoutes servePluginCustomRoutes
) )

View File

@ -4,6 +4,8 @@ async function register ({
const router = getRouter() const router = getRouter()
router.get('/ping', (req, res) => res.json({ message: 'pong' })) router.get('/ping', (req, res) => res.json({ message: 'pong' }))
router.get('/is-authenticated', (req, res) => res.json({ isAuthenticated: res.locals.authenticated }))
router.post('/form/post/mirror', (req, res) => { router.post('/form/post/mirror', (req, res) => {
res.json(req.body) res.json(req.body)
}) })

View File

@ -44,6 +44,27 @@ describe('Test plugin helpers', function () {
} }
}) })
it('Should check if authenticated', async function () {
for (const path of basePaths) {
const res = await makeGetRequest({
url: server.url,
path: path + 'is-authenticated',
token: server.accessToken,
statusCodeExpected: 200
})
expect(res.body.isAuthenticated).to.equal(undefined)
const secRes = await makeGetRequest({
url: server.url,
path: path + 'is-authenticated',
statusCodeExpected: 200
})
expect(secRes.body.isAuthenticated).to.equal(false)
}
})
it('Should mirror post body', async function () { it('Should mirror post body', async function () {
const body = { const body = {
hello: 'world', hello: 'world',