15255: Don't render fedtoken iframe if workbench2 config is missing.
[arvados-workbench2.git] / src / views / workbench / fed-login.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import { connect } from 'react-redux';
7 import { RootState } from '~/store/store';
8 import { AuthState } from '~/store/auth/auth-reducer';
9 import { User } from "~/models/user";
10 import { getSaltedToken } from '~/store/auth/auth-action-session';
11 import { Config } from '~/common/config';
12
13 export interface FedLoginProps {
14     user?: User;
15     apiToken?: string;
16     localCluster: string;
17     remoteHostsConfig: { [key: string]: Config };
18 }
19
20 const mapStateToProps = ({ auth }: RootState) => ({
21     user: auth.user,
22     apiToken: auth.apiToken,
23     remoteHostsConfig: auth.remoteHostsConfig,
24     localCluster: auth.localCluster,
25 });
26
27 export const FedLogin = connect(mapStateToProps)(
28     class extends React.Component<FedLoginProps> {
29         render() {
30             const { apiToken, user, localCluster, remoteHostsConfig } = this.props;
31             if (!apiToken || !user || !user.uuid.startsWith(localCluster)) {
32                 return <></>;
33             }
34             const [, tokenUuid, token] = apiToken.split("/");
35             return <div id={"fedtoken-iframe-div"}>
36                 {Object.keys(remoteHostsConfig)
37                     .map((k) => k !== localCluster && remoteHostsConfig[k].workbench2Url &&
38                         <iframe key={k} src={`${remoteHostsConfig[k].workbench2Url}/fedtoken?api_token=${getSaltedToken(k, tokenUuid, token)}`} style={{
39                             height: 0,
40                             width: 0,
41                             visibility: "hidden"
42                         }}
43                         />)}
44             </div>;
45         }
46     });