From cb7521926d1912df3c2da0d604b0248ce6836b6e Mon Sep 17 00:00:00 2001 From: Justin Carlson Date: Wed, 26 Oct 2022 15:03:12 -0400 Subject: [PATCH 1/7] Fix missing await --- src/ScalarMessaging.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ScalarMessaging.ts b/src/ScalarMessaging.ts index 72ff94d4d3f..6420127eda7 100644 --- a/src/ScalarMessaging.ts +++ b/src/ScalarMessaging.ts @@ -651,7 +651,7 @@ function returnStateEvent(event: MessageEvent, roomId: string, eventType: s async function getOpenIdToken(event: MessageEvent) { try { - const tokenObject = MatrixClientPeg.get().getOpenIdToken(); + const tokenObject = await MatrixClientPeg.get().getOpenIdToken(); sendResponse(event, tokenObject); } catch (ex) { logger.warn("Unable to fetch openId token.", ex); From 2563095766b12e413f946cd7b452ce7cfd4c5dff Mon Sep 17 00:00:00 2001 From: Justin Carlson Date: Wed, 26 Oct 2022 15:03:43 -0400 Subject: [PATCH 2/7] Fix get openID token action requiring room ID and user ID --- src/ScalarMessaging.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ScalarMessaging.ts b/src/ScalarMessaging.ts index 6420127eda7..e1e7bc42c15 100644 --- a/src/ScalarMessaging.ts +++ b/src/ScalarMessaging.ts @@ -715,6 +715,9 @@ const onMessage = function(event: MessageEvent): void { } else if (event.data.action === Action.SetWidget) { setWidget(event, null); return; + } else if (event.data.action === Action.GetOpenIdToken) { + getOpenIdToken(event); + return; } else { sendError(event, _t('Missing room_id in request')); return; @@ -776,9 +779,6 @@ const onMessage = function(event: MessageEvent): void { case Action.SetBotPower: setBotPower(event, roomId, userId, event.data.level, event.data.ignoreIfGreater); break; - case Action.GetOpenIdToken: - getOpenIdToken(event); - break; default: logger.warn("Unhandled postMessage event with action '" + event.data.action +"'"); break; From 325b4fe240a7a2bb8d8b06338b5150af04356568 Mon Sep 17 00:00:00 2001 From: Justin Carlson Date: Fri, 28 Oct 2022 12:30:20 -0400 Subject: [PATCH 3/7] Add e2e test for integration manager get openID token --- .../get-openid-token.spec.ts | 143 ++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 cypress/e2e/integration-manager/get-openid-token.spec.ts diff --git a/cypress/e2e/integration-manager/get-openid-token.spec.ts b/cypress/e2e/integration-manager/get-openid-token.spec.ts new file mode 100644 index 00000000000..956c808a409 --- /dev/null +++ b/cypress/e2e/integration-manager/get-openid-token.spec.ts @@ -0,0 +1,143 @@ +/* +Copyright 2022 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/// + +import { SynapseInstance } from "../../plugins/synapsedocker"; +import { UserCredentials } from "../../support/login"; + +const ROOM_NAME = "Integration Manager Test"; +const USER_DISPLAY_NAME = "Alice"; + +const INTEGRATION_MANAGER_TOKEN = "DefinitelySecret_DoNotUseThisForReal"; +const INTEGRATION_MANAGER_HTML = ` + + + Fake Integration Manager + + + + +

No response

+ + + +`; + +function openIntegrationManager() { + cy.get(".mx_RightPanel_roomSummaryButton").click(); + cy.get(".mx_RoomSummaryCard_appsGroup").within(() => { + cy.contains("Add widgets, bridges & bots").click(); + }); +} + +function sendActionFromIntegrationManager(integrationManagerUrl: string) { + cy.accessIframe(`iframe[src*="${integrationManagerUrl}"]`).within(() => { + cy.get("#send-action").should("exist").click(); + }); +} + +describe("Integration Manager: Get OpenID Token", () => { + let testUser: UserCredentials; + let synapse: SynapseInstance; + let integrationManagerUrl: string; + + beforeEach(() => { + cy.serveHtmlFile(INTEGRATION_MANAGER_HTML).then(url => { + integrationManagerUrl = url; + }); + cy.startSynapse("default").then(data => { + synapse = data; + + cy.initTestUser(synapse, USER_DISPLAY_NAME, () => { + cy.window().then(win => { + win.localStorage.setItem("mx_scalar_token", INTEGRATION_MANAGER_TOKEN); + win.localStorage.setItem(`mx_scalar_token_at_${integrationManagerUrl}`, INTEGRATION_MANAGER_TOKEN); + }); + }).then(user => { + testUser = user; + }); + + cy.setAccountData("m.widgets", { + "m.integration_manager": { + content: { + type: "m.integration_manager", + name: "Integration Manager", + url: integrationManagerUrl, + data: { + api_url: integrationManagerUrl, + }, + }, + id: "integration-manager", + }, + }).as("integrationManager"); + + // Succeed when checking the token is valid + cy.intercept(`${integrationManagerUrl}/account?scalar_token=${INTEGRATION_MANAGER_TOKEN}*`, req => { + req.continue(res => { + return res.send(200, { + user_id: testUser.userId, + }); + }); + }); + + cy.createRoom({ + name: ROOM_NAME, + }).as("roomId"); + }); + }); + + afterEach(() => { + cy.stopSynapse(synapse); + cy.stopWebServers(); + }); + + it("should obtain an openID token", () => { + cy.all([ + cy.get<{}>("@integrationManager"), + ]).then(() => { + cy.viewRoomByName(ROOM_NAME); + + openIntegrationManager(); + sendActionFromIntegrationManager(integrationManagerUrl); + + cy.accessIframe(`iframe[src*="${integrationManagerUrl}"]`).within(() => { + cy.get("#message-response").should('include.text', 'access_token'); + }); + }); + }); +}); From 5607f58eac1a6ee34f4795ec5be369f37b754ec3 Mon Sep 17 00:00:00 2001 From: Justin Carlson Date: Thu, 3 Nov 2022 11:51:45 -0400 Subject: [PATCH 4/7] Remove outdated comment --- src/ScalarMessaging.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/ScalarMessaging.ts b/src/ScalarMessaging.ts index e1e7bc42c15..f4a4dcd34e5 100644 --- a/src/ScalarMessaging.ts +++ b/src/ScalarMessaging.ts @@ -706,9 +706,6 @@ const onMessage = function(event: MessageEvent): void { if (!roomId) { // These APIs don't require roomId - // Get and set user widgets (not associated with a specific room) - // If roomId is specified, it must be validated, so room-based widgets agreed - // handled further down. if (event.data.action === Action.GetWidgets) { getWidgets(event, null); return; From 0f11ab00f7ce5f2275b986ecdf4f15a7d4564a20 Mon Sep 17 00:00:00 2001 From: Justin Carlson Date: Thu, 3 Nov 2022 11:52:12 -0400 Subject: [PATCH 5/7] Update test description Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> --- cypress/e2e/integration-manager/get-openid-token.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cypress/e2e/integration-manager/get-openid-token.spec.ts b/cypress/e2e/integration-manager/get-openid-token.spec.ts index 956c808a409..6f4f977c36f 100644 --- a/cypress/e2e/integration-manager/get-openid-token.spec.ts +++ b/cypress/e2e/integration-manager/get-openid-token.spec.ts @@ -126,7 +126,7 @@ describe("Integration Manager: Get OpenID Token", () => { cy.stopWebServers(); }); - it("should obtain an openID token", () => { + it("should successfully obtain an openID token", () => { cy.all([ cy.get<{}>("@integrationManager"), ]).then(() => { From 0c90afa5676facca9f6ad78684914c24205b02bd Mon Sep 17 00:00:00 2001 From: Justin Carlson Date: Wed, 9 Nov 2022 09:24:50 -0500 Subject: [PATCH 6/7] Fix type --- src/ScalarMessaging.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ScalarMessaging.ts b/src/ScalarMessaging.ts index f4a4dcd34e5..15b459b5ca5 100644 --- a/src/ScalarMessaging.ts +++ b/src/ScalarMessaging.ts @@ -376,7 +376,7 @@ function kickUser(event: MessageEvent, roomId: string, userId: string): voi }); } -function setWidget(event: MessageEvent, roomId: string): void { +function setWidget(event: MessageEvent, roomId?: string): void { const widgetId = event.data.widget_id; let widgetType = event.data.type; const widgetUrl = event.data.url; From 90bf40430c8fa354b2232fb2720242a41b261d83 Mon Sep 17 00:00:00 2001 From: Justin Carlson Date: Thu, 10 Nov 2022 10:29:34 -0500 Subject: [PATCH 7/7] Fix types again --- src/ScalarMessaging.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ScalarMessaging.ts b/src/ScalarMessaging.ts index 15b459b5ca5..2b0c8744903 100644 --- a/src/ScalarMessaging.ts +++ b/src/ScalarMessaging.ts @@ -376,7 +376,7 @@ function kickUser(event: MessageEvent, roomId: string, userId: string): voi }); } -function setWidget(event: MessageEvent, roomId?: string): void { +function setWidget(event: MessageEvent, roomId: string | null): void { const widgetId = event.data.widget_id; let widgetType = event.data.type; const widgetUrl = event.data.url; @@ -435,6 +435,7 @@ function setWidget(event: MessageEvent, roomId?: string): void { } else { // Room widget if (!roomId) { sendError(event, _t('Missing roomId.'), null); + return; } WidgetUtils.setRoomWidget(roomId, widgetId, widgetType, widgetUrl, widgetName, widgetData, widgetAvatarUrl) .then(() => {