ActivityPub-federated video streaming platform using P2P directly in your web browser
Add more openid tests
Chocobozzzb44dfef3f962
diff --git a/packages/tests/src/external-plugins/auth-openid-connect.ts b/packages/tests/src/external-plugins/auth-openid-connect.tsindex 150f8f8543..70bf7f880a 100644--- a/packages/tests/src/external-plugins/auth-openid-connect.ts+++ b/packages/tests/src/external-plugins/auth-openid-connect.ts@@ -1,6 +1,7 @@/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */-import { HttpStatusCode } from '@peertube/peertube-models'+import { wait } from '@peertube/peertube-core-utils'+import { HttpStatusCode, HttpStatusCodeType, UserRole } from '@peertube/peertube-models'import {cleanupTests,createSingleServer,@@ -18,52 +19,141 @@ describe('Official plugin auth-openid-connect', function () {let server: PeerTubeServerlet openIdLoginUrl: string- const pluginSettings = {- 'auth-display-name': 'OpenID Connect',- 'discover-url': `http://${oauthServerHost}:${oauthServerPort}/realms/myrealm`,- 'client-id': 'myclient',- 'client-secret': 'D9MdqzGSnlfWJq00e9mBzI31OPn9WXyg',- 'scope': 'openid email profile',- 'username-property': 'email',- 'mail-property': 'email',- 'logout-redirect-uri': '',- 'display-name-property': 'email',- 'role-property': '',- 'group-property': '',- 'allowed-group': '',- 'signature-algorithm': 'RS256'- }-before(async function () {- this.timeout(30000)+ this.timeout(60000)server = await createSingleServer(1)await setAccessTokensToServers([ server ])await server.plugins.install({ npmName: 'peertube-plugin-auth-openid-connect' })- await server.plugins.updateSettings({- npmName: 'peertube-plugin-auth-openid-connect',- settings: pluginSettings- })})it('Should load openid connect plugin', async function () {- const config = await server.config.getConfig()+ const config = await updatePluginSettings(server)+const { name, version, authName } = config.plugin.registeredExternalAuths[0]openIdLoginUrl = server.url + `/plugins/${name}/${version}/auth/${authName}`})it('Should login with the appropriate username/password', async function () {const peertubeRes = await getOpenIdUrl(openIdLoginUrl)- const kcRes = await loginOnKeycloak(extractLocation(peertubeRes))+ const kcRes = await loginOnKeycloak({ loginPageUrl: extractLocation(peertubeRes) })- const ptBypassPath = await sendBackKeycloakCode(peertubeRes, kcRes)+ const ptBypassPath = await sendBackKeycloakCode({ peertubeRes, kcRes })const externalAuthToken = new URL(ptBypassPath, server.url).searchParams.get('externalAuthToken')const { body } = await server.login.loginUsingExternalToken({ username: 'myuser_example.com', externalAuthToken })- const { username } = await server.users.getMyInfo({ token: body.access_token })+ const { username, role } = await server.users.getMyInfo({ token: body.access_token })expect(username).to.equal('myuser_example.com')+ expect(role.id).to.equal(UserRole.USER)+ })++ it('Should allow to redirect on the app by default', async function () {+ await getOpenIdUrl(openIdLoginUrl, 'peertube://joinpeertube.org/mysuperlogin', HttpStatusCode.FOUND_302)+ await getOpenIdUrl(openIdLoginUrl, 'peertube://joinpeertube.org/', HttpStatusCode.FOUND_302)+ await getOpenIdUrl(openIdLoginUrl, 'peertdube://joinpeertube.org/', HttpStatusCode.FORBIDDEN_403)+ await getOpenIdUrl(openIdLoginUrl, 'apeertube://joinpeertube.org/', HttpStatusCode.FORBIDDEN_403)+ await getOpenIdUrl(openIdLoginUrl, 'peertube://joinpeertube/', HttpStatusCode.FORBIDDEN_403)+ })++ it('Should not allow a redirect URL that is not in the allowed list', async function () {+ await updatePluginSettings(server, {+ 'allowed-external-redirect-uris': 'http://example.com,http://example2.com/.*,~https://example2.com/.*'+ })++ await getOpenIdUrl(openIdLoginUrl, 'http://example.com', HttpStatusCode.FOUND_302)+ await getOpenIdUrl(openIdLoginUrl, 'http://example.com/2', HttpStatusCode.FORBIDDEN_403)++ await getOpenIdUrl(openIdLoginUrl, 'http://example2.com/2', HttpStatusCode.FORBIDDEN_403)++ await getOpenIdUrl(openIdLoginUrl, 'http://example2.com/2', HttpStatusCode.FORBIDDEN_403)+ await getOpenIdUrl(openIdLoginUrl, 'https://example2.com/2', HttpStatusCode.FOUND_302)+ })++ it('Should correctly redirect the user', async function () {+ const peertubeRes = await getOpenIdUrl(openIdLoginUrl, 'http://example.com')+ const kcRes = await loginOnKeycloak({ loginPageUrl: extractLocation(peertubeRes) })++ const ptBypassPath = await sendBackKeycloakCode({ peertubeRes, kcRes, redirectUrl: 'http://example.com' })+ const externalAuthToken = new URL(ptBypassPath, server.url).searchParams.get('externalAuthToken')++ const { body } = await server.login.loginUsingExternalToken({ username: 'myuser_example.com', externalAuthToken })+ expect(body.access_token).to.exist+ })++ it('Should not allow a redirect URL if not provided in the settings', async function () {+ for (const value of [ null, '', undefined ]) {+ await updatePluginSettings(server, {+ 'allowed-external-redirect-uris': value+ })++ await getOpenIdUrl(openIdLoginUrl, 'https://example2.com', HttpStatusCode.FORBIDDEN_403)+ await getOpenIdUrl(openIdLoginUrl, '', HttpStatusCode.FORBIDDEN_403)+ }+ })++ it('Should only login allowed groups', async function () {+ {+ await updatePluginSettings(server, {+ 'group-property': 'typo',+ 'allowed-group': 'Group1'+ })++ const peertubeRes = await getOpenIdUrl(openIdLoginUrl)+ const kcRes = await loginOnKeycloak({ loginPageUrl: extractLocation(peertubeRes), username: 'user_group1' })+ await sendBackKeycloakCode({ peertubeRes, kcRes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })+ }++ {+ await updatePluginSettings(server, {+ 'group-property': 'groups',+ 'allowed-group': 'Group 1'+ })++ const peertubeRes = await getOpenIdUrl(openIdLoginUrl)+ const kcRes = await loginOnKeycloak({ loginPageUrl: extractLocation(peertubeRes), username: 'user_group1' })+ await sendBackKeycloakCode({ peertubeRes, kcRes, expectedStatus: HttpStatusCode.FOUND_302, username: 'user_group1_example.com' })+ }++ {+ await updatePluginSettings(server, {+ 'group-property': 'groups',+ 'allowed-group': 'Group 2'+ })++ const peertubeRes = await getOpenIdUrl(openIdLoginUrl)+ const kcRes = await loginOnKeycloak({ loginPageUrl: extractLocation(peertubeRes), username: 'user_group1' })+ await sendBackKeycloakCode({ peertubeRes, kcRes, expectedStatus: HttpStatusCode.FORBIDDEN_403 })+ }+ })++ it('Should correctly map the user role property', async function () {+ await updatePluginSettings(server, { 'role-property': 'role' })++ {+ const peertubeRes = await getOpenIdUrl(openIdLoginUrl)+ const kcRes = await loginOnKeycloak({ loginPageUrl: extractLocation(peertubeRes), username: 'moderator' })+ const ptBypassPath = await sendBackKeycloakCode({ peertubeRes, kcRes, username: 'moderator_example.com' })++ const externalAuthToken = new URL(ptBypassPath, server.url).searchParams.get('externalAuthToken')++ const { body } = await server.login.loginUsingExternalToken({ username: 'moderator_example.com', externalAuthToken })+ const me = await server.users.getMyInfo({ token: body.access_token })+ expect(me.role.id).to.equal(UserRole.MODERATOR)+ }++ {+ const peertubeRes = await getOpenIdUrl(openIdLoginUrl)+ const kcRes = await loginOnKeycloak({ loginPageUrl: extractLocation(peertubeRes), username: 'user_group2' })+ const ptBypassPath = await sendBackKeycloakCode({ peertubeRes, kcRes, username: 'user_group2_example.com' })++ const externalAuthToken = new URL(ptBypassPath, server.url).searchParams.get('externalAuthToken')++ const { body } = await server.login.loginUsingExternalToken({ username: 'user_group2_example.com', externalAuthToken })+ const me = await server.users.getMyInfo({ token: body.access_token })+ expect(me.role.id).to.equal(UserRole.USER)+ }})after(async function () {@@ -88,10 +178,28 @@ function extractInputValue (text: string, name: string) {return match[1]}-async function getOpenIdUrl (openIdLoginUrl: string) {- const peertubeRes = await makeRawRequest({ url: openIdLoginUrl, expectedStatus: HttpStatusCode.FOUND_302 })+async function getOpenIdUrl (+ openIdLoginUrl: string,+ externalRedirectUri?: string,+ expectedStatus: HttpStatusCodeType = HttpStatusCode.FOUND_302+) {+ const query = externalRedirectUri !== undefined+ ? { externalRedirectUri }+ : undefined++ const peertubeRes = await makeRawRequest({+ url: openIdLoginUrl,+ query,+ expectedStatus+ })const kcLocation = peertubeRes.headers['location']+ if (expectedStatus !== HttpStatusCode.FOUND_302) {+ expect(kcLocation).to.be.undefined++ return peertubeRes+ }+expect(kcLocation).to.include(`http://${oauthServerHost}:${oauthServerPort}/realms/myrealm/protocol/openid-connect/auth?`)const parsed = new URL(kcLocation)@@ -102,7 +210,13 @@ async function getOpenIdUrl (openIdLoginUrl: string) {return peertubeRes}-async function loginOnKeycloak (loginPageUrl: string) {+async function loginOnKeycloak (options: {+ loginPageUrl: string+ username?: string+ password?: string+}) {+ const { loginPageUrl, username = 'myuser', password = 'coucou' } = options+const resLoginPage = await makeRawRequest({ url: loginPageUrl, expectedStatus: HttpStatusCode.OK_200 })expect(resLoginPage.text).to.include('Sign in to your account')@@ -113,10 +227,7 @@ async function loginOnKeycloak (loginPageUrl: string) {url: actionUrl,method: 'POST',requestType: 'form',- fields: {- username: 'myuser',- password: 'coucou'- },+ fields: { username, password },headers: {Cookie: cookies},@@ -126,27 +237,38 @@ async function loginOnKeycloak (loginPageUrl: string) {return res}-async function sendBackKeycloakCode (peertubeRes: Response, kcRes: Response) {+async function sendBackKeycloakCode (options: {+ peertubeRes: Response+ kcRes: Response+ redirectUrl?: string+ expectedStatus?: HttpStatusCodeType+ username?: string+}) {+ const { peertubeRes, kcRes, redirectUrl = '/login', expectedStatus = HttpStatusCode.FOUND_302, username = 'myuser_example.com' } = options+const kcText = kcRes.textconst res = await makeRawRequest({url: extractActionUrl(kcText),method: 'POST',headers: {- Cookie: extractCookies(peertubeRes)+ 'Cookie': extractCookies(peertubeRes),+ 'Content-Type': 'application/x-www-form-urlencoded'},fields: {code: extractInputValue(kcText, 'code'),iss: extractInputValue(kcText, 'iss'),- state: extractInputValue(kcText, 'state'),session_state: extractInputValue(kcText, 'session_state')},- expectedStatus: HttpStatusCode.FOUND_302+ expectedStatus})+ if (expectedStatus !== HttpStatusCode.FOUND_302) return undefined+const ptBypassPath = res.headers['location']- expect(ptBypassPath).to.include('/login?externalAuthToken=')- expect(ptBypassPath).to.include('username=myuser_example.com')+ expect(ptBypassPath).to.include(redirectUrl)+ expect(ptBypassPath).to.include('?externalAuthToken=')+ expect(ptBypassPath).to.include('username=' + username)return ptBypassPath}@@ -161,3 +283,44 @@ function extractLocation (res: Response) {return location}++async function waitForAuthReady (server: PeerTubeServer) {+ let config = await server.config.getConfig()++ while (config.plugin.registeredExternalAuths.length === 0) {+ await wait(500)++ config = await server.config.getConfig()+ }++ return config+}++async function updatePluginSettings (server: PeerTubeServer, override?: Record<string, any>) {+ const pluginSettings = {+ 'auth-display-name': 'OpenID Connect',+ 'discover-url': `http://${oauthServerHost}:${oauthServerPort}/realms/myrealm`,+ 'client-id': 'myclient',+ 'client-secret': 'D9MdqzGSnlfWJq00e9mBzI31OPn9WXyg',+ 'scope': 'openid email profile',+ 'username-property': 'email',+ 'mail-property': 'email',+ 'logout-redirect-uri': '',+ 'display-name-property': 'email',+ 'role-property': '',+ 'group-property': '',+ 'allowed-group': '',+ 'signature-algorithm': 'RS256'+ }++ await server.plugins.updateSettings({+ npmName: 'peertube-plugin-auth-openid-connect',+ settings: {+ ...pluginSettings,++ ...override+ }+ })++ return waitForAuthReady(server)+}