ActivityPub-federated video streaming platform using P2P directly in your web browser
Add cross-auth option (#7655)
Asyncnomi543930a547bf
diff --git a/config/config-schema.json b/config/config-schema.jsonindex dd05e422bb..2941e3f66a 100644--- a/config/config-schema.json+++ b/config/config-schema.json@@ -1667,6 +1667,11 @@"type": "boolean","default": false},+ "allow_cross_provider_auth": {+ "description": "Enable cross authentication when using multiple auth plugins Never enable this unless you know what you're doing. If not used correctly, it can lead to account takeover",+ "type": "boolean",+ "default": false+ },"video_quota": {"description": "Default value of maximum video bytes the user can upload Does not take into account transcoded files or account export archives (that can include user uploaded files) Byte format is supported (\"1GB\" etc) -1 == unlimited","type": "integer",diff --git a/config/default.yaml b/config/default.yamlindex 222a6b5a64..f63a1af603 100644--- a/config/default.yaml+++ b/config/default.yaml@@ -608,6 +608,10 @@ user:# If enabled, root cannot login and existing root OAuth access/refresh tokens are rejected.disable_root_auth: false+ # Enable cross authentication when using multiple auth plugins+ # Never enable this unless you know what you're doing. If not used correctly, it can lead to account takeover+ allow_cross_provider_auth: false+# Default value of maximum video bytes the user can upload# Does not take into account transcoded files or account export archives (that can include user uploaded files)# Byte format is supported ("1GB" etc)diff --git a/config/production.yaml.example b/config/production.yaml.exampleindex a212401980..8e52b735b4 100644--- a/config/production.yaml.example+++ b/config/production.yaml.example@@ -618,6 +618,10 @@ user:# If enabled, root cannot login and existing root OAuth access/refresh tokens are rejected.disable_root_auth: false+ # Enable cross authentication when using multiple auth plugins+ # Never enable this unless you know what you're doing. If not used correctly, it can lead to account takeover+ allow_cross_provider_auth: false+# Default value of maximum video bytes the user can upload# Does not take into account transcoded files or account export archives (that can include user uploaded files)# Byte format is supported ("1GB" etc)diff --git a/packages/tests/src/plugins/external-auth.ts b/packages/tests/src/plugins/external-auth.tsindex 03871bf8ff..3466c8f714 100644--- a/packages/tests/src/plugins/external-auth.ts+++ b/packages/tests/src/plugins/external-auth.ts@@ -169,18 +169,20 @@ describe('Test external auth plugins', function () {})it('Should reject an invalid language returned by an external auth plugin', async function () {- const res = await server.plugins.getExternalAuth({+ const res = await loginExternal({+ server,npmName: 'test-external-auth-one',- npmVersion: '0.0.1',authName: 'external-auth-1',query: {username: 'shadow',language: 'not-a-valid-locale'},- expectedStatus: HttpStatusCode.FOUND_302+ username: 'shadow'})- expect(res.header.location).to.equal('/login?externalAuthError=true')+ const body = await server.users.getMyInfo({ token: res.access_token })+ expect(body.username).to.equal('shadow')+ expect(body.language).to.be.null})it('Should auto login Kefka, create the user and use the token', async function () {@@ -430,10 +432,6 @@ describe('Test external auth plugins', function () {expect(auth2).to.not.exist})- after(async function () {- await cleanupTests([ server ])- })-it('Should forward the redirectUrl if the plugin returns one', async function () {const resLogin = await loginExternal({server,@@ -481,4 +479,93 @@ describe('Test external auth plugins', function () {await server.login.loginUsingExternalToken({ username: 'cid', externalAuthToken, expectedStatus: HttpStatusCode.OK_200 })})++ after(async function () {+ await cleanupTests([ server ])+ })+})++describe('Test external auth plugins with some non-default config options', function () {+ let server: PeerTubeServer++ before(async function () {+ this.timeout(30000)++ server = await createSingleServer(1, {+ user: {+ allow_cross_provider_auth: true+ }+ })++ await setAccessTokensToServers([ server ])++ for (const suffix of [ 'two', 'three' ]) {+ await server.plugins.install({ path: PluginsCommand.getPluginTestPath('-external-auth-' + suffix) })+ }+ })++ it('Should allow cross-provider auth when the option is enabled', async function () {+ {+ const res = await loginExternal({+ server,+ npmName: 'test-external-auth-three',+ authName: 'external-auth-7',+ username: 'cid'+ })++ const body = await server.users.getMyInfo({ token: res.access_token })+ expect(body.pluginAuth).to.equal('peertube-plugin-test-external-auth-three')+ }++ {+ const res = await loginExternal({+ server,+ npmName: 'test-external-auth-two',+ authName: 'external-auth-3',+ username: 'cid'+ })++ const body = await server.users.getMyInfo({ token: res.access_token })+ expect(body.pluginAuth).to.equal('peertube-plugin-test-external-auth-two')+ }++ {+ const res = await loginExternal({+ server,+ npmName: 'test-external-auth-three',+ authName: 'external-auth-7',+ username: 'cid'+ })++ const body = await server.users.getMyInfo({ token: res.access_token })+ expect(body.pluginAuth).to.equal('peertube-plugin-test-external-auth-three')+ }++ await server.kill()+ await server.run({+ user: {+ allow_cross_provider_auth: false+ }+ })++ await loginExternal({+ server,+ npmName: 'test-external-auth-three',+ authName: 'external-auth-7',+ username: 'cid',+ expectedStatusStep2: HttpStatusCode.OK_200+ })++ await loginExternal({+ server,+ npmName: 'test-external-auth-two',+ authName: 'external-auth-3',+ username: 'cid',+ expectedStatusStep2: HttpStatusCode.BAD_REQUEST_400+ })+ })++ after(async function () {+ await cleanupTests([ server ])+ })})diff --git a/server/core/initializers/config.ts b/server/core/initializers/config.tsindex 9780467d25..ece47506af 100644--- a/server/core/initializers/config.ts+++ b/server/core/initializers/config.ts@@ -618,6 +618,9 @@ const CONFIG = {get DISABLE_ROOT_AUTH () {return config.get<boolean>('user.disable_root_auth')},+ get ALLOW_CROSS_PROVIDER_AUTH () {+ return config.get<boolean>('user.allow_cross_provider_auth')+ },get VIDEO_QUOTA () {return parseBytes(config.get<number>('user.video_quota'))},diff --git a/server/core/lib/auth/oauth-model.ts b/server/core/lib/auth/oauth-model.tsindex 5c4ee4b3a3..3c878f7f23 100644--- a/server/core/lib/auth/oauth-model.ts+++ b/server/core/lib/auth/oauth-model.ts@@ -109,16 +109,32 @@ async function getUser (usernameOrEmail?: string, password?: string, options?: {// If the user does not belongs to a plugin, it was created before its installation// Then we just go through a regular login processif (user.pluginAuth !== null) {- // This user does not belong to this plugin, skip it+ // This user does not belong to this pluginif (user.pluginAuth !== pluginName) {- logger.info(- 'Cannot bypass oauth login by plugin %s because %s has another plugin auth method (%s).',- pluginName,- externalUser.email,- user.pluginAuth- )-- return null+ if (CONFIG.USER.ALLOW_CROSS_PROVIDER_AUTH !== true) {+ logger.info(+ 'Cannot bypass oauth login by plugin %s because %s has another plugin auth method (%s).',+ pluginName,+ externalUser.email,+ user.pluginAuth+ )++ return null+ } else {+ logger.info(+ 'Allowing cross authentication login for %s using plugin %s despite being known from plugin %s',+ bypassLogin.user.email,+ bypassLogin.pluginName,+ user.pluginAuth+ )++ user.pluginAuth = pluginName+ await updateUserFromExternal({ user, userOptions: externalUser, userUpdater, syncEmail: true })++ // Tokens issued under the previous auth plugin can no longer have their validity checked by that+ // plugin's hookTokenValidity (the user is not registered under it anymore), so force a fresh login+ await OAuthTokenModel.deleteUserToken({ userId: user.id })+ }}checkUserValidityOrThrow(user, req)diff --git a/support/docker/production/config/custom-environment-variables.yaml b/support/docker/production/config/custom-environment-variables.yamlindex 15203bf07d..c16e8315dd 100644--- a/support/docker/production/config/custom-environment-variables.yaml+++ b/support/docker/production/config/custom-environment-variables.yaml@@ -540,6 +540,9 @@ signup:__format: jsonuser:+ allow_cross_provider_auth:+ __name: PEERTUBE_USER_ALLOW_CROSS_PROVIDER_AUTH+ __format: jsonvideo_quota:__name: PEERTUBE_USER_VIDEO_QUOTA__format: json