From 48fefa0d3ef740ce70a59af29d8477350d03b02b Mon Sep 17 00:00:00 2001 From: Jason Robinson Date: Wed, 28 Oct 2020 16:38:47 +0200 Subject: [PATCH] Implement fetching OpenID token for hosting provider iframe And then pass it via postMessage when requested. Send whole OpenID credentials object to Hosting Provider iframe --- .../structures/HostingProviderDialog.tsx | 72 ++++++++++++++++++- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/src/components/structures/HostingProviderDialog.tsx b/src/components/structures/HostingProviderDialog.tsx index 0ee3d32b3f..fd5799a1b7 100644 --- a/src/components/structures/HostingProviderDialog.tsx +++ b/src/components/structures/HostingProviderDialog.tsx @@ -16,17 +16,83 @@ limitations under the License. import * as React from "react"; import SdkConfig from "../../SdkConfig"; +import {MatrixClientPeg} from "../../MatrixClientPeg"; interface IProps {} -interface IState {} +interface IState { + error: string, +} export default class HostingProviderDialog extends React.PureComponent { + iframeRef; + hostingSignupUrl: string; + + constructor(props: IProps) { + super(props); + + this.state = { + error: null, + }; + + this.iframeRef = React.createRef(); + this.hostingSignupUrl = SdkConfig.get().hosting_signup_iframe; + } + + private messageHandler = (message) => { + if (!this.hostingSignupUrl.startsWith(message.origin)) { + return; + } + + switch (message.data.action) { + case 'openid_credentials_request': + // noinspection JSIgnoredPromiseFromCall + this.fetchOpenIDToken(); + break; + } + } + + private sendMessage = (message) => { + this.iframeRef.contentWindow.postMessage( + message, + this.hostingSignupUrl, + ) + } + + private async fetchOpenIDToken() { + const token = await MatrixClientPeg.get().getOpenIdToken(); + if (token && token.access_token) { + this.sendMessage({ + action: 'openid_credentials', + tokenData: token, + }); + } else { + this.setState({ + error: "Failed to connect to your homeserver. Please close this dialog and try again.", + }); + } + } + + public componentDidMount() { + window.addEventListener("message", this.messageHandler); + } + + public componentWillUnmount() { + window.removeEventListener("message", this.messageHandler); + } + public render(): React.ReactNode { - const hostingSignupUrl = SdkConfig.get().hosting_signup_iframe; return (
-