16314: Fix syntax.
[arvados-workbench2.git] / src / views / workbench / fed-login.tsx
index 0e9b5308c5d06c5405ce084fd1a2263c4aebcc9c..7c8b87c7f022f42db31404309bd3ad7657583272 100644 (file)
@@ -5,27 +5,50 @@
 import * as React from 'react';
 import { connect } from 'react-redux';
 import { RootState } from '~/store/store';
-import { AuthState } from '~/store/auth/auth-reducer';
+import { User } from "~/models/user";
 import { getSaltedToken } from '~/store/auth/auth-action-session';
+import { Config } from '~/common/config';
 
 export interface FedLoginProps {
-    auth: AuthState;
+    user?: User;
+    apiToken?: string;
+    localCluster: string;
+    remoteHostsConfig: { [key: string]: Config };
 }
 
-const mapStateToProps = ({ auth }: RootState) => ({ auth });
+const mapStateToProps = ({ auth }: RootState) => ({
+    user: auth.user,
+    apiToken: auth.apiToken,
+    remoteHostsConfig: auth.remoteHostsConfig,
+    localCluster: auth.localCluster,
+});
 
 export const FedLogin = connect(mapStateToProps)(
     class extends React.Component<FedLoginProps> {
         render() {
-            const auth = this.props.auth;
-            const remoteHostsConfig = auth.remoteHostsConfig;
-            if (!auth.user || !auth.user.uuid.startsWith(auth.homeCluster)) {
+            const { apiToken, user, localCluster, remoteHostsConfig } = this.props;
+            if (!apiToken || !user || !user.uuid.startsWith(localCluster)) {
                 return <></>;
             }
-            return <div>
+            return <div id={"fedtoken-iframe-div"}>
                 {Object.keys(remoteHostsConfig)
-                    .filter((k) => k !== auth.homeCluster)
-                    .map((k) => <iframe key={k} src={"https://" + remoteHostsConfig[k].workbench2Url} style={{ visibility: "hidden" }} />)}
+                    .map((k) => {
+                        if (k === localCluster) {
+                            return;
+                        }
+                        if (!remoteHostsConfig[k].workbench2Url) {
+                            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.`);
+                            return;
+                        }
+                        const fedtoken = (remoteHostsConfig[k].loginCluster === localCluster)
+                            ? apiToken : getSaltedToken(k, apiToken);
+                        return <iframe key={k} src={`${remoteHostsConfig[k].workbench2Url}/fedtoken?api_token=${fedtoken}`} style={{
+                            height: 0,
+                            width: 0,
+                            visibility: "hidden"
+                        }}
+                        />;
+                    })}
             </div>;
         }
     });