be543a64a5bee708cdecfd0bb65f4853008c848a
[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 { User } from "~/models/user";
9 import { getSaltedToken } from '~/store/auth/auth-action-session';
10 import { Config } from '~/common/config';
11
12 export interface FedLoginProps {
13     user?: User;
14     apiToken?: string;
15     localCluster: string;
16     remoteHostsConfig: { [key: string]: Config };
17 }
18
19 const mapStateToProps = ({ auth }: RootState) => ({
20     user: auth.user,
21     apiToken: auth.apiToken,
22     remoteHostsConfig: auth.remoteHostsConfig,
23     localCluster: auth.localCluster,
24 });
25
26 export const FedLogin = connect(mapStateToProps)(
27     class extends React.Component<FedLoginProps> {
28         render() {
29             const { apiToken, user, localCluster, remoteHostsConfig } = this.props;
30             if (!apiToken || !user || !user.uuid.startsWith(localCluster)) {
31                 return <></>;
32             }
33             const [, tokenUuid, token] = apiToken.split("/");
34             return <div id={"fedtoken-iframe-div"}>
35                 {Object.keys(remoteHostsConfig)
36                     .map((k) => {
37                         if (k === localCluster) {
38                             return;
39                         }
40                         if (!remoteHostsConfig[k].workbench2Url) {
41                             console.log(`Cluster ${k} does not define workbench2Url.  Federated login / cross-site linking to ${k} is unavailable.  Tell the admin of ${k} to set Services->Workbench2->ExternalURL in config.yml.`);
42                             return;
43                         }
44                         const fedtoken = (remoteHostsConfig[k].loginCluster === localCluster)
45                             ? apiToken : getSaltedToken(k, tokenUuid, token);
46                         return <iframe key={k} src={`${remoteHostsConfig[k].workbench2Url}/fedtoken?api_token=${fedtoken}`} style={{
47                             height: 0,
48                             width: 0,
49                             visibility: "hidden"
50                         }}
51                         />;
52                     })}
53             </div>;
54         }
55     });