X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/ae044d06880a54d847c73fbcf88884d21f0d6621..2c2339c775f9112f5da638b2beb81d8b6d5abc2b:/src/views/login-panel/login-panel.tsx

diff --git a/src/views/login-panel/login-panel.tsx b/src/views/login-panel/login-panel.tsx
index c0834a1f..110097be 100644
--- a/src/views/login-panel/login-panel.tsx
+++ b/src/views/login-panel/login-panel.tsx
@@ -2,13 +2,16 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import * as React from 'react';
+import React from 'react';
 import { connect, DispatchProp } from 'react-redux';
 import { Grid, Typography, Button, Select } from '@material-ui/core';
 import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles';
-import { login, authActions } from '~/store/auth/auth-action';
-import { ArvadosTheme } from '~/common/custom-theme';
-import { RootState } from '~/store/store';
+import { login, authActions } from 'store/auth/auth-action';
+import { ArvadosTheme } from 'common/custom-theme';
+import { RootState } from 'store/store';
+import { LoginForm } from 'views-components/login-form/login-form';
+import Axios from 'axios';
+import { Config } from 'common/config';
 
 type CssRules = 'root' | 'container' | 'title' | 'content' | 'content__bolder' | 'button';
 
@@ -47,28 +50,58 @@ const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
     }
 });
 
+const doPasswordLogin = (url: string) => (username: string, password: string) => {
+    const formData: string[] = [];
+    formData.push('username='+encodeURIComponent(username));
+    formData.push('password='+encodeURIComponent(password));
+    return Axios.post(`${url}/arvados/v1/users/authenticate`, formData.join('&'), {
+        headers: {
+            'Content-Type': 'application/x-www-form-urlencoded'
+        },
+    });
+};
+
 type LoginPanelProps = DispatchProp<any> & WithStyles<CssRules> & {
     remoteHosts: { [key: string]: string },
     homeCluster: string,
-    uuidPrefix: string,
-    welcomePage: string
+    localCluster: string,
+    loginCluster: string,
+    welcomePage: string,
+    passwordLogin: boolean,
+};
+
+const loginOptions = ['LDAP', 'PAM', 'Test'];
+
+export const requirePasswordLogin = (config: Config): boolean => {
+    if (config && config.clusterConfig && config.clusterConfig.Login) {
+        return loginOptions
+            .filter(loginOption => !!config.clusterConfig.Login[loginOption])
+            .map(loginOption => config.clusterConfig.Login[loginOption].Enable)
+            .find(enabled => enabled === true) || false;
+    }
+    return false;
 };
 
 export const LoginPanel = withStyles(styles)(
     connect((state: RootState) => ({
         remoteHosts: state.auth.remoteHosts,
         homeCluster: state.auth.homeCluster,
-        uuidPrefix: state.auth.localCluster,
-        welcomePage: state.config.clusterConfig.Workbench.WelcomePageHTML
-    }))(({ classes, dispatch, remoteHosts, homeCluster, uuidPrefix, welcomePage }: LoginPanelProps) =>
-        <Grid container justify="center" alignItems="center"
+        localCluster: state.auth.localCluster,
+        loginCluster: state.auth.loginCluster,
+        welcomePage: state.auth.config.clusterConfig.Workbench.WelcomePageHTML,
+        passwordLogin: requirePasswordLogin(state.auth.remoteHostsConfig[state.auth.loginCluster || state.auth.homeCluster]),
+        }))(({ classes, dispatch, remoteHosts, homeCluster, localCluster, loginCluster, welcomePage, passwordLogin }: LoginPanelProps) => {
+        const loginBtnLabel = `Log in${(localCluster !== homeCluster && loginCluster !== homeCluster) ? " to "+localCluster+" with user from "+homeCluster : ''}`;
+
+        return (<Grid container justify="center" alignItems="center"
             className={classes.root}
             style={{ marginTop: 56, overflowY: "auto", height: "100%" }}>
             <Grid item className={classes.container}>
-                <Typography>
-                    <div dangerouslySetInnerHTML={{ __html: welcomePage }} />
+                <Typography component="div">
+                    <div dangerouslySetInnerHTML={{ __html: welcomePage }} style={{ margin: "1em" }} />
                 </Typography>
-                {Object.keys(remoteHosts).length > 1 &&
+                {Object.keys(remoteHosts).length > 1 && loginCluster === "" &&
+
                     <Typography component="div" align="right">
                         <label>Please select the cluster that hosts your user account:</label>
                         <Select native value={homeCluster} style={{ margin: "1em" }}
@@ -77,14 +110,19 @@ export const LoginPanel = withStyles(styles)(
                         </Select>
                     </Typography>}
 
-                <Typography component="div" align="right">
-                    <Button variant="contained" color="primary" style={{ margin: "1em" }} className={classes.button}
-                        onClick={() => dispatch(login(uuidPrefix, homeCluster, remoteHosts))}>
-                        Log in to {uuidPrefix}
-                        {uuidPrefix !== homeCluster &&
-                            <span>&nbsp;with user from {homeCluster}</span>}
-                    </Button>
+                {passwordLogin
+                ? <Typography component="div">
+                    <LoginForm dispatch={dispatch}
+                        loginLabel={loginBtnLabel}
+                        handleSubmit={doPasswordLogin(`https://${remoteHosts[loginCluster || homeCluster]}`)}/>
                 </Typography>
+                : <Typography component="div" align="right">
+                    <Button variant="contained" color="primary" style={{ margin: "1em" }}
+                        className={classes.button}
+                        onClick={() => dispatch(login(localCluster, homeCluster, loginCluster, remoteHosts))}>
+                        {loginBtnLabel}
+                    </Button>
+                </Typography>}
             </Grid>
-        </Grid >
+        </Grid >);}
     ));
