aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorkontrollanten <6680299+kontrollanten@users.noreply.github.com>2020-12-04 20:56:48 +0100
committerChocobozzz <chocobozzz@cpy.re>2020-12-08 10:40:08 +0100
commitf17faefb30e4872688a1c0dafcc6c793242750f4 (patch)
treefcae9afbdbb1f3fffff89737c2e3d7c3919510cd
parentc824e8a0c7f28a8770553e21e225589dd8f015c0 (diff)
downloadPeerTube-f17faefb30e4872688a1c0dafcc6c793242750f4.tar.gz
PeerTube-f17faefb30e4872688a1c0dafcc6c793242750f4.tar.zst
PeerTube-f17faefb30e4872688a1c0dafcc6c793242750f4.zip
plugins: add optional authentication for routes
-rw-r--r--server/controllers/plugins.ts3
-rw-r--r--server/tests/fixtures/peertube-plugin-test-five/main.js2
-rw-r--r--server/tests/plugins/plugin-router.ts21
3 files changed, 26 insertions, 0 deletions
diff --git a/server/controllers/plugins.ts b/server/controllers/plugins.ts
index 18c6613e2..6a1ccc0bf 100644
--- a/server/controllers/plugins.ts
+++ b/server/controllers/plugins.ts
@@ -9,6 +9,7 @@ import { getCompleteLocale, is18nLocale } from '../../shared/core-utils/i18n'
9import { PluginType } from '../../shared/models/plugins/plugin.type' 9import { PluginType } from '../../shared/models/plugins/plugin.type'
10import { isTestInstance } from '../helpers/core-utils' 10import { isTestInstance } from '../helpers/core-utils'
11import { logger } from '@server/helpers/logger' 11import { logger } from '@server/helpers/logger'
12import { optionalAuthenticate } from '@server/middlewares/oauth'
12 13
13const sendFileOptions = { 14const sendFileOptions = {
14 maxAge: '30 days', 15 maxAge: '30 days',
@@ -45,11 +46,13 @@ pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEnd
45 46
46pluginsRouter.use('/plugins/:pluginName/router', 47pluginsRouter.use('/plugins/:pluginName/router',
47 getPluginValidator(PluginType.PLUGIN, false), 48 getPluginValidator(PluginType.PLUGIN, false),
49 optionalAuthenticate,
48 servePluginCustomRoutes 50 servePluginCustomRoutes
49) 51)
50 52
51pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router', 53pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router',
52 getPluginValidator(PluginType.PLUGIN), 54 getPluginValidator(PluginType.PLUGIN),
55 optionalAuthenticate,
53 servePluginCustomRoutes 56 servePluginCustomRoutes
54) 57)
55 58
diff --git a/server/tests/fixtures/peertube-plugin-test-five/main.js b/server/tests/fixtures/peertube-plugin-test-five/main.js
index c1435b928..07dd18654 100644
--- a/server/tests/fixtures/peertube-plugin-test-five/main.js
+++ b/server/tests/fixtures/peertube-plugin-test-five/main.js
@@ -4,6 +4,8 @@ async function register ({
4 const router = getRouter() 4 const router = getRouter()
5 router.get('/ping', (req, res) => res.json({ message: 'pong' })) 5 router.get('/ping', (req, res) => res.json({ message: 'pong' }))
6 6
7 router.get('/is-authenticated', (req, res) => res.json({ isAuthenticated: res.locals.authenticated }))
8
7 router.post('/form/post/mirror', (req, res) => { 9 router.post('/form/post/mirror', (req, res) => {
8 res.json(req.body) 10 res.json(req.body)
9 }) 11 })
diff --git a/server/tests/plugins/plugin-router.ts b/server/tests/plugins/plugin-router.ts
index 9e78568cd..5392acc51 100644
--- a/server/tests/plugins/plugin-router.ts
+++ b/server/tests/plugins/plugin-router.ts
@@ -44,6 +44,27 @@ describe('Test plugin helpers', function () {
44 } 44 }
45 }) 45 })
46 46
47 it('Should check if authenticated', async function () {
48 for (const path of basePaths) {
49 const res = await makeGetRequest({
50 url: server.url,
51 path: path + 'is-authenticated',
52 token: server.accessToken,
53 statusCodeExpected: 200
54 })
55
56 expect(res.body.isAuthenticated).to.equal(undefined)
57
58 const secRes = await makeGetRequest({
59 url: server.url,
60 path: path + 'is-authenticated',
61 statusCodeExpected: 200
62 })
63
64 expect(secRes.body.isAuthenticated).to.equal(false)
65 }
66 })
67
47 it('Should mirror post body', async function () { 68 it('Should mirror post body', async function () {
48 const body = { 69 const body = {
49 hello: 'world', 70 hello: 'world',