From 15dbe300848890406579f7532f51d2fb698bf1c9 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Wed, 8 Jun 2022 09:06:21 +0100 Subject: [PATCH 1/4] Add getOpenIdToken function --- src/ScalarMessaging.ts | 49 ++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/src/ScalarMessaging.ts b/src/ScalarMessaging.ts index 337b9c8167b..fa9271c318a 100644 --- a/src/ScalarMessaging.ts +++ b/src/ScalarMessaging.ts @@ -148,7 +148,6 @@ Request: can configure/lay out the widget in different ways. All widgets must have a type. - `name` (String) is an optional human-readable string about the widget. - `data` (Object) is some optional data about the widget, and can contain arbitrary key/value pairs. - - `avatar_url` (String) is some optional mxc: URI pointing to the avatar of the widget. Response: { success: true @@ -234,6 +233,13 @@ Example: avatar_url: null } } + +get_open_id_token +----------------- +Get an openID token for the current user session. +Request: No parameters +Response: + - The openId token object as described in https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3useruseridopenidrequest_token */ import { MatrixEvent } from 'matrix-js-sdk/src/models/event'; @@ -242,7 +248,7 @@ import { logger } from "matrix-js-sdk/src/logger"; import { MatrixClientPeg } from './MatrixClientPeg'; import dis from './dispatcher/dispatcher'; import WidgetUtils from './utils/WidgetUtils'; -import { RoomViewStore } from './stores/RoomViewStore'; +import RoomViewStore from './stores/RoomViewStore'; import { _t } from './languageHandler'; import { IntegrationManagers } from "./integrations/IntegrationManagers"; import { WidgetType } from "./widgets/WidgetType"; @@ -262,6 +268,7 @@ enum Action { BotOptions = "bot_options", SetBotOptions = "set_bot_options", SetBotPower = "set_bot_power", + GetOpenIdToken = "get_open_id_token" } function sendResponse(event: MessageEvent, res: any): void { @@ -320,7 +327,6 @@ function setWidget(event: MessageEvent, roomId: string): void { const widgetUrl = event.data.url; const widgetName = event.data.name; // optional const widgetData = event.data.data; // optional - const widgetAvatarUrl = event.data.avatar_url; // optional const userWidget = event.data.userWidget; // both adding/removing widgets need these checks @@ -339,14 +345,6 @@ function setWidget(event: MessageEvent, roomId: string): void { sendError(event, _t("Unable to create widget."), new Error("Optional field 'data' must be an Object.")); return; } - if (widgetAvatarUrl !== undefined && typeof widgetAvatarUrl !== 'string') { - sendError( - event, - _t("Unable to create widget."), - new Error("Optional field 'avatar_url' must be a string."), - ); - return; - } if (typeof widgetType !== 'string') { sendError(event, _t("Unable to create widget."), new Error("Field 'type' must be a string.")); return; @@ -374,14 +372,13 @@ function setWidget(event: MessageEvent, roomId: string): void { if (!roomId) { sendError(event, _t('Missing roomId.'), null); } - WidgetUtils.setRoomWidget(roomId, widgetId, widgetType, widgetUrl, widgetName, widgetData, widgetAvatarUrl) - .then(() => { - sendResponse(event, { - success: true, - }); - }, (err) => { - sendError(event, _t('Failed to send request.'), err); + WidgetUtils.setRoomWidget(roomId, widgetId, widgetType, widgetUrl, widgetName, widgetData).then(() => { + sendResponse(event, { + success: true, }); + }, (err) => { + sendError(event, _t('Failed to send request.'), err); + }); } } @@ -587,6 +584,15 @@ function returnStateEvent(event: MessageEvent, roomId: string, eventType: s sendResponse(event, stateEvent.getContent()); } +async function getOpenIdToken(event: MessageEvent) { + try { + const tokenObject = MatrixClientPeg.get().getOpenIdToken(); + sendResponse(event, tokenObject); + } catch (ex) { + sendError(event, 'Unable to fetch openId token.'); + } +} + const onMessage = function(event: MessageEvent): void { if (!event.origin) { // stupid chrome // @ts-ignore @@ -649,7 +655,7 @@ const onMessage = function(event: MessageEvent): void { } } - if (roomId !== RoomViewStore.instance.getRoomId()) { + if (roomId !== RoomViewStore.getRoomId()) { sendError(event, _t('Room %(roomId)s not visible', { roomId: roomId })); return; } @@ -701,12 +707,17 @@ 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; } }; + + let listenerCount = 0; let openManagerUrl: string = null; From e1f516ac81724cb6d8423bd8cfcc19f439bd8b99 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Wed, 8 Jun 2022 09:15:26 +0100 Subject: [PATCH 2/4] tidy up --- src/ScalarMessaging.ts | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/ScalarMessaging.ts b/src/ScalarMessaging.ts index fa9271c318a..aa779250314 100644 --- a/src/ScalarMessaging.ts +++ b/src/ScalarMessaging.ts @@ -248,7 +248,7 @@ import { logger } from "matrix-js-sdk/src/logger"; import { MatrixClientPeg } from './MatrixClientPeg'; import dis from './dispatcher/dispatcher'; import WidgetUtils from './utils/WidgetUtils'; -import RoomViewStore from './stores/RoomViewStore'; +import { RoomViewStore } from './stores/RoomViewStore'; import { _t } from './languageHandler'; import { IntegrationManagers } from "./integrations/IntegrationManagers"; import { WidgetType } from "./widgets/WidgetType"; @@ -327,6 +327,7 @@ function setWidget(event: MessageEvent, roomId: string): void { const widgetUrl = event.data.url; const widgetName = event.data.name; // optional const widgetData = event.data.data; // optional + const widgetAvatarUrl = event.data.avatar_url; // optional const userWidget = event.data.userWidget; // both adding/removing widgets need these checks @@ -345,6 +346,14 @@ function setWidget(event: MessageEvent, roomId: string): void { sendError(event, _t("Unable to create widget."), new Error("Optional field 'data' must be an Object.")); return; } + if (widgetAvatarUrl !== undefined && typeof widgetAvatarUrl !== 'string') { + sendError( + event, + _t("Unable to create widget."), + new Error("Optional field 'avatar_url' must be a string."), + ); + return; + } if (typeof widgetType !== 'string') { sendError(event, _t("Unable to create widget."), new Error("Field 'type' must be a string.")); return; @@ -372,13 +381,14 @@ function setWidget(event: MessageEvent, roomId: string): void { if (!roomId) { sendError(event, _t('Missing roomId.'), null); } - WidgetUtils.setRoomWidget(roomId, widgetId, widgetType, widgetUrl, widgetName, widgetData).then(() => { - sendResponse(event, { - success: true, + WidgetUtils.setRoomWidget(roomId, widgetId, widgetType, widgetUrl, widgetName, widgetData, widgetAvatarUrl) + .then(() => { + sendResponse(event, { + success: true, + }); + }, (err) => { + sendError(event, _t('Failed to send request.'), err); }); - }, (err) => { - sendError(event, _t('Failed to send request.'), err); - }); } } @@ -655,7 +665,7 @@ const onMessage = function(event: MessageEvent): void { } } - if (roomId !== RoomViewStore.getRoomId()) { + if (roomId !== RoomViewStore.instance.getRoomId()) { sendError(event, _t('Room %(roomId)s not visible', { roomId: roomId })); return; } @@ -716,8 +726,6 @@ const onMessage = function(event: MessageEvent): void { } }; - - let listenerCount = 0; let openManagerUrl: string = null; From 3b450e548342bfa8847d7c562621ee92f5f6aa4b Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Wed, 8 Jun 2022 09:15:53 +0100 Subject: [PATCH 3/4] tidy up --- src/ScalarMessaging.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ScalarMessaging.ts b/src/ScalarMessaging.ts index aa779250314..a0360eca353 100644 --- a/src/ScalarMessaging.ts +++ b/src/ScalarMessaging.ts @@ -148,6 +148,7 @@ Request: can configure/lay out the widget in different ways. All widgets must have a type. - `name` (String) is an optional human-readable string about the widget. - `data` (Object) is some optional data about the widget, and can contain arbitrary key/value pairs. + - `avatar_url` (String) is some optional mxc: URI pointing to the avatar of the widget. Response: { success: true From adf8a4dc27cf57678c43b7aff7e1c6cb7781349e Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Thu, 16 Jun 2022 16:54:19 +0100 Subject: [PATCH 4/4] log an error --- src/ScalarMessaging.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ScalarMessaging.ts b/src/ScalarMessaging.ts index a0360eca353..bf629bb711b 100644 --- a/src/ScalarMessaging.ts +++ b/src/ScalarMessaging.ts @@ -600,6 +600,7 @@ async function getOpenIdToken(event: MessageEvent) { const tokenObject = MatrixClientPeg.get().getOpenIdToken(); sendResponse(event, tokenObject); } catch (ex) { + logger.warn("Unable to fetch openId token.", ex); sendError(event, 'Unable to fetch openId token.'); } }