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
This commit is contained in:
Jason Robinson 2020-10-28 16:38:47 +02:00
parent c7706ac3d5
commit 48fefa0d3e

View file

@ -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<IProps, IState> {
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 (
<div className="mx_HostingProviderDialog_container">
<iframe src={hostingSignupUrl} />
<iframe
src={this.hostingSignupUrl}
ref={ref => this.iframeRef = ref}
/>
{this.state.error &&
<div>
{this.state.error}
</div>
}
</div>
);
}