]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
plugins: add optional authentication for routes
authorkontrollanten <6680299+kontrollanten@users.noreply.github.com>
Fri, 4 Dec 2020 19:56:48 +0000 (20:56 +0100)
committerChocobozzz <chocobozzz@cpy.re>
Tue, 8 Dec 2020 09:40:08 +0000 (10:40 +0100)
server/controllers/plugins.ts
server/tests/fixtures/peertube-plugin-test-five/main.js
server/tests/plugins/plugin-router.ts

index 18c6613e25ac798a362785d9d9318111e0c821ae..6a1ccc0bf91c5439176eee6fab392e5371340a83 100644 (file)
@@ -9,6 +9,7 @@ import { getCompleteLocale, is18nLocale } from '../../shared/core-utils/i18n'
 import { PluginType } from '../../shared/models/plugins/plugin.type'
 import { isTestInstance } from '../helpers/core-utils'
 import { logger } from '@server/helpers/logger'
+import { optionalAuthenticate } from '@server/middlewares/oauth'
 
 const sendFileOptions = {
   maxAge: '30 days',
@@ -45,11 +46,13 @@ pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEnd
 
 pluginsRouter.use('/plugins/:pluginName/router',
   getPluginValidator(PluginType.PLUGIN, false),
+  optionalAuthenticate,
   servePluginCustomRoutes
 )
 
 pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router',
   getPluginValidator(PluginType.PLUGIN),
+  optionalAuthenticate,
   servePluginCustomRoutes
 )
 
index c1435b928b1e02fd1d03b481686e82d44ef17e58..07dd18654440f5933a47fcaab00f52cffea4d22a 100644 (file)
@@ -4,6 +4,8 @@ async function register ({
   const router = getRouter()
   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) => {
     res.json(req.body)
   })
index 9e78568cdfe73ff438814b55c349c83d486113c6..5392acc51a7b8b5f0a172e98ebd1595be6d14c4f 100644 (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 () {
     const body = {
       hello: 'world',