1717import { Room } from "matrix-js-sdk/src/models/room" ;
1818import {
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" ;
3236import { StopGapWidgetDriver } from "./StopGapWidgetDriver" ;
@@ -43,6 +47,8 @@ import ActiveWidgetStore from "../ActiveWidgetStore";
4347import { objectShallowClone } from "../../utils/objects" ;
4448import defaultDispatcher from "../../dispatcher/dispatcher" ;
4549import { 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