Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 92f89b8

Browse files
authored
Merge pull request #5334 from matrix-org/t3chguy/fix/jitsi-openidc
Fix Jitsi OpenIDC auth
2 parents b10f7a4 + 81990d3 commit 92f89b8

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

src/stores/widgets/StopGapWidget.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import { Room } from "matrix-js-sdk/src/models/room";
1818
import {
1919
ClientWidgetApi,
20+
IGetOpenIDActionRequest,
21+
IGetOpenIDActionResponseData,
2022
IStickerActionRequest,
2123
IStickyActionRequest,
2224
ITemplateParams,
@@ -25,8 +27,10 @@ import {
2527
IWidgetApiRequestEmptyData,
2628
IWidgetData,
2729
MatrixCapabilities,
30+
OpenIDRequestState,
2831
runTemplate,
2932
Widget,
33+
WidgetApiToWidgetAction,
3034
WidgetApiFromWidgetAction,
3135
} from "matrix-widget-api";
3236
import { StopGapWidgetDriver } from "./StopGapWidgetDriver";
@@ -43,6 +47,8 @@ import ActiveWidgetStore from "../ActiveWidgetStore";
4347
import { objectShallowClone } from "../../utils/objects";
4448
import defaultDispatcher from "../../dispatcher/dispatcher";
4549
import { ElementWidgetActions } from "./ElementWidgetActions";
50+
import Modal from "../../Modal";
51+
import WidgetOpenIDPermissionsDialog from "../../components/views/dialogs/WidgetOpenIDPermissionsDialog";
4652

4753
// TODO: Destroy all of this code
4854

@@ -190,12 +196,66 @@ export class StopGapWidget extends EventEmitter {
190196
return !!this.messaging;
191197
}
192198

199+
private get widgetId() {
200+
return this.messaging.widget.id;
201+
}
202+
203+
private onOpenIdReq = async (ev: CustomEvent<IGetOpenIDActionRequest>) => {
204+
if (ev?.detail?.widgetId !== this.widgetId) return;
205+
206+
const rawUrl = this.appTileProps.app.url;
207+
const widgetSecurityKey = WidgetUtils.getWidgetSecurityKey(this.widgetId, rawUrl, this.appTileProps.userWidget);
208+
209+
const settings = SettingsStore.getValue("widgetOpenIDPermissions");
210+
if (settings.deny && settings.deny.includes(widgetSecurityKey)) {
211+
this.messaging.transport.reply(ev.detail, <IGetOpenIDActionResponseData>{
212+
state: OpenIDRequestState.Blocked,
213+
});
214+
return;
215+
}
216+
if (settings.allow && settings.allow.includes(widgetSecurityKey)) {
217+
const credentials = await MatrixClientPeg.get().getOpenIdToken();
218+
this.messaging.transport.reply(ev.detail, <IGetOpenIDActionResponseData>{
219+
state: OpenIDRequestState.Allowed,
220+
...credentials,
221+
});
222+
return;
223+
}
224+
225+
// Confirm that we received the request
226+
this.messaging.transport.reply(ev.detail, <IGetOpenIDActionResponseData>{
227+
state: OpenIDRequestState.PendingUserConfirmation,
228+
});
229+
230+
// Actually ask for permission to send the user's data
231+
Modal.createTrackedDialog("OpenID widget permissions", '', WidgetOpenIDPermissionsDialog, {
232+
widgetUrl: rawUrl.substr(0, rawUrl.lastIndexOf("?")),
233+
widgetId: this.widgetId,
234+
isUserWidget: this.appTileProps.userWidget,
235+
236+
onFinished: async (confirm) => {
237+
const responseBody: IGetOpenIDActionResponseData = {
238+
state: confirm ? OpenIDRequestState.Allowed : OpenIDRequestState.Blocked,
239+
original_request_id: ev.detail.requestId, // eslint-disable-line camelcase
240+
};
241+
if (confirm) {
242+
const credentials = await MatrixClientPeg.get().getOpenIdToken();
243+
Object.assign(responseBody, credentials);
244+
}
245+
this.messaging.transport.send(WidgetApiToWidgetAction.OpenIDCredentials, responseBody).catch(error => {
246+
console.error("Failed to send OpenID credentials: ", error);
247+
});
248+
},
249+
});
250+
};
251+
193252
public start(iframe: HTMLIFrameElement) {
194253
if (this.started) return;
195254
const driver = new StopGapWidgetDriver( this.appTileProps.whitelistCapabilities || []);
196255
this.messaging = new ClientWidgetApi(this.mockWidget, iframe, driver);
197256
this.messaging.addEventListener("preparing", () => this.emit("preparing"));
198257
this.messaging.addEventListener("ready", () => this.emit("ready"));
258+
this.messaging.addEventListener(`action:${WidgetApiFromWidgetAction.GetOpenIDCredentials}`, this.onOpenIdReq);
199259
WidgetMessagingStore.instance.storeMessaging(this.mockWidget, this.messaging);
200260

201261
if (!this.appTileProps.userWidget && this.appTileProps.room) {

0 commit comments

Comments
 (0)