aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-04-30 10:03:09 +0200
committerChocobozzz <chocobozzz@cpy.re>2020-05-04 16:21:39 +0200
commita4995eb7ac5745f62604d70f7b2225ff33916d49 (patch)
tree2dbed723d3f5a539e8f0847d914723cd7543b1b2 /server
parente9b0fa5c16ca196a3ba3267af4008782a3e86875 (diff)
downloadPeerTube-a4995eb7ac5745f62604d70f7b2225ff33916d49.tar.gz
PeerTube-a4995eb7ac5745f62604d70f7b2225ff33916d49.tar.zst
PeerTube-a4995eb7ac5745f62604d70f7b2225ff33916d49.zip
Add ability to unregister plugin auths
Diffstat (limited to 'server')
-rw-r--r--server/lib/plugins/register-helpers-store.ts24
-rw-r--r--server/tests/fixtures/peertube-plugin-test-external-auth-one/main.js10
-rw-r--r--server/tests/fixtures/peertube-plugin-test-id-pass-auth-one/main.js10
-rw-r--r--server/tests/plugins/external-auth.ts38
-rw-r--r--server/tests/plugins/id-and-pass-auth.ts26
-rw-r--r--server/typings/plugins/register-server-option.model.ts2
6 files changed, 102 insertions, 8 deletions
diff --git a/server/lib/plugins/register-helpers-store.ts b/server/lib/plugins/register-helpers-store.ts
index 6317ac2cf..a3ec7ef6a 100644
--- a/server/lib/plugins/register-helpers-store.ts
+++ b/server/lib/plugins/register-helpers-store.ts
@@ -49,8 +49,8 @@ export class RegisterHelpersStore {
49 49
50 private readonly settings: RegisterServerSettingOptions[] = [] 50 private readonly settings: RegisterServerSettingOptions[] = []
51 51
52 private readonly idAndPassAuths: RegisterServerAuthPassOptions[] = [] 52 private idAndPassAuths: RegisterServerAuthPassOptions[] = []
53 private readonly externalAuths: RegisterServerAuthExternalOptions[] = [] 53 private externalAuths: RegisterServerAuthExternalOptions[] = []
54 54
55 private readonly onSettingsChangeCallbacks: ((settings: any) => void)[] = [] 55 private readonly onSettingsChangeCallbacks: ((settings: any) => void)[] = []
56 56
@@ -83,6 +83,8 @@ export class RegisterHelpersStore {
83 83
84 const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth() 84 const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth()
85 const registerExternalAuth = this.buildRegisterExternalAuth() 85 const registerExternalAuth = this.buildRegisterExternalAuth()
86 const unregisterIdAndPassAuth = this.buildUnregisterIdAndPassAuth()
87 const unregisterExternalAuth = this.buildUnregisterExternalAuth()
86 88
87 const peertubeHelpers = buildPluginHelpers(this.npmName) 89 const peertubeHelpers = buildPluginHelpers(this.npmName)
88 90
@@ -104,6 +106,8 @@ export class RegisterHelpersStore {
104 106
105 registerIdAndPassAuth, 107 registerIdAndPassAuth,
106 registerExternalAuth, 108 registerExternalAuth,
109 unregisterIdAndPassAuth,
110 unregisterExternalAuth,
107 111
108 peertubeHelpers 112 peertubeHelpers
109 } 113 }
@@ -179,7 +183,7 @@ export class RegisterHelpersStore {
179 private buildRegisterIdAndPassAuth () { 183 private buildRegisterIdAndPassAuth () {
180 return (options: RegisterServerAuthPassOptions) => { 184 return (options: RegisterServerAuthPassOptions) => {
181 if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') { 185 if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') {
182 logger.error('Cannot register auth plugin %s: authName of getWeight or login are not valid.', this.npmName) 186 logger.error('Cannot register auth plugin %s: authName, getWeight or login are not valid.', this.npmName, { options })
183 return 187 return
184 } 188 }
185 189
@@ -192,7 +196,7 @@ export class RegisterHelpersStore {
192 196
193 return (options: RegisterServerAuthExternalOptions) => { 197 return (options: RegisterServerAuthExternalOptions) => {
194 if (!options.authName || typeof options.authDisplayName !== 'function' || typeof options.onAuthRequest !== 'function') { 198 if (!options.authName || typeof options.authDisplayName !== 'function' || typeof options.onAuthRequest !== 'function') {
195 logger.error('Cannot register auth plugin %s: authName of getWeight or login are not valid.', this.npmName) 199 logger.error('Cannot register auth plugin %s: authName, authDisplayName or onAuthRequest are not valid.', this.npmName, { options })
196 return 200 return
197 } 201 }
198 202
@@ -212,6 +216,18 @@ export class RegisterHelpersStore {
212 } 216 }
213 } 217 }
214 218
219 private buildUnregisterExternalAuth () {
220 return (authName: string) => {
221 this.externalAuths = this.externalAuths.filter(a => a.authName !== authName)
222 }
223 }
224
225 private buildUnregisterIdAndPassAuth () {
226 return (authName: string) => {
227 this.idAndPassAuths = this.idAndPassAuths.filter(a => a.authName !== authName)
228 }
229 }
230
215 private buildSettingsManager (): PluginSettingsManager { 231 private buildSettingsManager (): PluginSettingsManager {
216 return { 232 return {
217 getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name), 233 getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name),
diff --git a/server/tests/fixtures/peertube-plugin-test-external-auth-one/main.js b/server/tests/fixtures/peertube-plugin-test-external-auth-one/main.js
index 91c67e550..c65b8d3a8 100644
--- a/server/tests/fixtures/peertube-plugin-test-external-auth-one/main.js
+++ b/server/tests/fixtures/peertube-plugin-test-external-auth-one/main.js
@@ -1,6 +1,8 @@
1async function register ({ 1async function register ({
2 registerExternalAuth, 2 registerExternalAuth,
3 peertubeHelpers 3 peertubeHelpers,
4 settingsManager,
5 unregisterExternalAuth
4}) { 6}) {
5 { 7 {
6 const result = registerExternalAuth({ 8 const result = registerExternalAuth({
@@ -53,6 +55,12 @@ async function register ({
53 } 55 }
54 }) 56 })
55 } 57 }
58
59 settingsManager.onSettingsChange(settings => {
60 if (settings.disableKefka) {
61 unregisterExternalAuth('external-auth-2')
62 }
63 })
56} 64}
57 65
58async function unregister () { 66async function unregister () {
diff --git a/server/tests/fixtures/peertube-plugin-test-id-pass-auth-one/main.js b/server/tests/fixtures/peertube-plugin-test-id-pass-auth-one/main.js
index 9fc12a3e3..f58faa847 100644
--- a/server/tests/fixtures/peertube-plugin-test-id-pass-auth-one/main.js
+++ b/server/tests/fixtures/peertube-plugin-test-id-pass-auth-one/main.js
@@ -1,6 +1,8 @@
1async function register ({ 1async function register ({
2 registerIdAndPassAuth, 2 registerIdAndPassAuth,
3 peertubeHelpers 3 peertubeHelpers,
4 settingsManager,
5 unregisterIdAndPassAuth
4}) { 6}) {
5 registerIdAndPassAuth({ 7 registerIdAndPassAuth({
6 authName: 'spyro-auth', 8 authName: 'spyro-auth',
@@ -47,6 +49,12 @@ async function register ({
47 return null 49 return null
48 } 50 }
49 }) 51 })
52
53 settingsManager.onSettingsChange(settings => {
54 if (settings.disableSpyro) {
55 unregisterIdAndPassAuth('spyro-auth')
56 }
57 })
50} 58}
51 59
52async function unregister () { 60async function unregister () {
diff --git a/server/tests/plugins/external-auth.ts b/server/tests/plugins/external-auth.ts
index a72b2829b..312561538 100644
--- a/server/tests/plugins/external-auth.ts
+++ b/server/tests/plugins/external-auth.ts
@@ -16,7 +16,9 @@ import {
16 setAccessTokensToServers, 16 setAccessTokensToServers,
17 uninstallPlugin, 17 uninstallPlugin,
18 updateMyUser, 18 updateMyUser,
19 wait 19 wait,
20 userLogin,
21 updatePluginSettings
20} from '../../../shared/extra-utils' 22} from '../../../shared/extra-utils'
21import { cleanupTests, flushAndRunServer, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers' 23import { cleanupTests, flushAndRunServer, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers'
22 24
@@ -258,6 +260,40 @@ describe('Test external auth plugins', function () {
258 await getMyUserInformation(server.url, kefkaAccessToken, 401) 260 await getMyUserInformation(server.url, kefkaAccessToken, 401)
259 }) 261 })
260 262
263 it('Should unregister external-auth-2 and do not login existing Kefka', async function () {
264 await updatePluginSettings({
265 url: server.url,
266 accessToken: server.accessToken,
267 npmName: 'peertube-plugin-test-external-auth-one',
268 settings: { disableKefka: true }
269 })
270
271 await userLogin(server, { username: 'kefka', password: 'fake' }, 400)
272
273 await loginExternal({
274 server,
275 npmName: 'test-external-auth-one',
276 authName: 'external-auth-2',
277 query: {
278 username: 'kefka'
279 },
280 username: 'kefka',
281 statusCodeExpected: 404
282 })
283 })
284
285 it('Should have disabled this auth', async function () {
286 const res = await getConfig(server.url)
287
288 const config: ServerConfig = res.body
289
290 const auths = config.plugin.registeredExternalAuths
291 expect(auths).to.have.lengthOf(2)
292
293 const auth1 = auths.find(a => a.authName === 'external-auth-2')
294 expect(auth1).to.not.exist
295 })
296
261 it('Should uninstall the plugin one and do not login Cyan', async function () { 297 it('Should uninstall the plugin one and do not login Cyan', async function () {
262 await uninstallPlugin({ 298 await uninstallPlugin({
263 url: server.url, 299 url: server.url,
diff --git a/server/tests/plugins/id-and-pass-auth.ts b/server/tests/plugins/id-and-pass-auth.ts
index 6c10730aa..97df4c1fd 100644
--- a/server/tests/plugins/id-and-pass-auth.ts
+++ b/server/tests/plugins/id-and-pass-auth.ts
@@ -12,7 +12,7 @@ import {
12 updateMyUser, 12 updateMyUser,
13 userLogin, 13 userLogin,
14 wait, 14 wait,
15 login, refreshToken, getConfig 15 login, refreshToken, getConfig, updatePluginSettings
16} from '../../../shared/extra-utils' 16} from '../../../shared/extra-utils'
17import { User, UserRole, ServerConfig } from '@shared/models' 17import { User, UserRole, ServerConfig } from '@shared/models'
18import { expect } from 'chai' 18import { expect } from 'chai'
@@ -179,6 +179,30 @@ describe('Test id and pass auth plugins', function () {
179 await waitUntilLog(server, 'valid email') 179 await waitUntilLog(server, 'valid email')
180 }) 180 })
181 181
182 it('Should unregister spyro-auth and do not login existing Spyro', async function () {
183 await updatePluginSettings({
184 url: server.url,
185 accessToken: server.accessToken,
186 npmName: 'peertube-plugin-test-id-pass-auth-one',
187 settings: { disableSpyro: true }
188 })
189
190 await userLogin(server, { username: 'spyro', password: 'spyro password' }, 400)
191 await userLogin(server, { username: 'spyro', password: 'fake' }, 400)
192 })
193
194 it('Should have disabled this auth', async function () {
195 const res = await getConfig(server.url)
196
197 const config: ServerConfig = res.body
198
199 const auths = config.plugin.registeredIdAndPassAuths
200 expect(auths).to.have.lengthOf(7)
201
202 const spyroAuth = auths.find(a => a.authName === 'spyro-auth')
203 expect(spyroAuth).to.not.exist
204 })
205
182 it('Should uninstall the plugin one and do not login existing Crash', async function () { 206 it('Should uninstall the plugin one and do not login existing Crash', async function () {
183 await uninstallPlugin({ 207 await uninstallPlugin({
184 url: server.url, 208 url: server.url,
diff --git a/server/typings/plugins/register-server-option.model.ts b/server/typings/plugins/register-server-option.model.ts
index c1e63316d..7f933b43a 100644
--- a/server/typings/plugins/register-server-option.model.ts
+++ b/server/typings/plugins/register-server-option.model.ts
@@ -49,6 +49,8 @@ export type RegisterServerOptions = {
49 49
50 registerIdAndPassAuth: (options: RegisterServerAuthPassOptions) => void 50 registerIdAndPassAuth: (options: RegisterServerAuthPassOptions) => void
51 registerExternalAuth: (options: RegisterServerAuthExternalOptions) => RegisterServerAuthExternalResult 51 registerExternalAuth: (options: RegisterServerAuthExternalOptions) => RegisterServerAuthExternalResult
52 unregisterIdAndPassAuth: (authName: string) => void
53 unregisterExternalAuth: (authName: string) => void
52 54
53 // Get plugin router to create custom routes 55 // Get plugin router to create custom routes
54 // Base routes of this router are 56 // Base routes of this router are