16212: Adds login form when PAM Login is enabled. (WIP)
authorLucas Di Pentima <lucas@di-pentima.com.ar>
Sun, 29 Mar 2020 14:50:01 +0000 (11:50 -0300)
committerLucas Di Pentima <lucas@di-pentima.com.ar>
Wed, 29 Apr 2020 20:24:36 +0000 (17:24 -0300)
Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas@di-pentima.com.ar>

src/views-components/login-form/login-form.tsx [new file with mode: 0644]
src/views/login-panel/login-panel.tsx

diff --git a/src/views-components/login-form/login-form.tsx b/src/views-components/login-form/login-form.tsx
new file mode 100644 (file)
index 0000000..404c91f
--- /dev/null
@@ -0,0 +1,128 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import * as React from 'react';
+import { useState, useEffect } from 'react';
+import { withStyles, WithStyles, StyleRulesCallback } from '@material-ui/core/styles';
+import CircularProgress from '@material-ui/core/CircularProgress';
+import { Button, Card, CardContent, TextField, CardActions } from '@material-ui/core';
+import { green } from '@material-ui/core/colors';
+import { AxiosPromise } from 'axios';
+
+type CssRules = 'root' | 'loginBtn' | 'card' | 'wrapper' | 'progress';
+
+const styles: StyleRulesCallback<CssRules> = theme => ({
+    root: {
+        display: 'flex',
+        flexWrap: 'wrap',
+        width: '100%',
+        margin: `${theme.spacing.unit} auto`
+    },
+    loginBtn: {
+        marginTop: theme.spacing.unit,
+        flexGrow: 1
+    },
+    card: {
+        marginTop: theme.spacing.unit,
+        width: '100%'
+    },
+    wrapper: {
+        margin: theme.spacing.unit,
+        position: 'relative',
+    },
+    progress: {
+        color: green[500],
+        position: 'absolute',
+        top: '50%',
+        left: '50%',
+        marginTop: -12,
+        marginLeft: -12,
+    },
+});
+
+interface LoginFormProps {
+    handleSubmit: (username: string, password: string) => AxiosPromise;
+}
+
+export const LoginForm = withStyles(styles)(
+    ({ handleSubmit, classes }: LoginFormProps & WithStyles<CssRules>) => {
+        const [username, setUsername] = useState('');
+        const [password, setPassword] = useState('');
+        const [isButtonDisabled, setIsButtonDisabled] = useState(true);
+        const [isSubmitting, setSubmitting] = useState(false);
+        const [helperText, setHelperText] = useState('');
+        const [error, setError] = useState(false);
+
+        useEffect(() => {
+            setError(false);
+            setHelperText('');
+            if (username.trim() && password.trim()) {
+                setIsButtonDisabled(false);
+            } else {
+                setIsButtonDisabled(true);
+            }
+        }, [username, password]);
+
+        const handleLogin = () => {
+            setSubmitting(true);
+            handleSubmit(username, password)
+            .then((response) => {
+                setError(false);
+                console.log("LOGIN SUCESSFUL: ", response);
+                setSubmitting(false);
+            })
+            .catch((err) => {
+                setError(true);
+                console.log("ERROR: ", err.response);
+                setHelperText(`${err.response && err.response.data && err.response.data.errors[0] || 'Error logging in: '+err}`);
+                setSubmitting(false);
+            });
+        };
+
+        const handleKeyPress = (e: any) => {
+            if (e.keyCode === 13 || e.which === 13) {
+                if (!isButtonDisabled) {
+                    handleLogin();
+                }
+            }
+        };
+
+        return (
+            <React.Fragment>
+                <form className={classes.root} noValidate autoComplete="off">
+                    <Card className={classes.card}>
+                    <div className={classes.wrapper}>
+                        <CardContent>
+                            <div>
+                                <TextField
+                                    disabled={isSubmitting}
+                                    error={error} fullWidth id="username" type="email"
+                                    label="Username" margin="normal"
+                                    onChange={(e) => setUsername(e.target.value)}
+                                    onKeyPress={(e) => handleKeyPress(e)}
+                                />
+                                <TextField
+                                    disabled={isSubmitting}
+                                    error={error} fullWidth id="password" type="password"
+                                    label="Password" margin="normal"
+                                    helperText={helperText}
+                                    onChange={(e) => setPassword(e.target.value)}
+                                    onKeyPress={(e) => handleKeyPress(e)}
+                                />
+                            </div>
+                        </CardContent>
+                        <CardActions>
+                            <Button variant="contained" size="large" color="primary"
+                                className={classes.loginBtn} onClick={() => handleLogin()}
+                                disabled={isSubmitting || isButtonDisabled}>
+                                Log in
+                            </Button>
+                        </CardActions>
+                        { isSubmitting && <CircularProgress color='secondary' className={classes.progress} />}
+                    </div>
+                    </Card>
+                </form>
+            </React.Fragment>
+        );
+    });
index 45f796fd8c5847e28a7ecd41e38f94166ff42b63..e7eadbad520cc5e41e13c5391925392bbdd1a442 100644 (file)
@@ -9,6 +9,8 @@ import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/st
 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';
 
 type CssRules = 'root' | 'container' | 'title' | 'content' | 'content__bolder' | 'button';
 
@@ -47,12 +49,22 @@ const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
     }
 });
 
+const doPAMLogin = (url: string) => (username: string, password: string) => {
+    const formData = new FormData();
+    formData.append("username", username);
+    formData.append("password", password);
+    return Axios.post(`${url}/login`, formData, {
+        headers: { 'X-Http-Method-Override': 'GET' },
+    });
+};
+
 type LoginPanelProps = DispatchProp<any> & WithStyles<CssRules> & {
     remoteHosts: { [key: string]: string },
     homeCluster: string,
     uuidPrefix: string,
     loginCluster: string,
-    welcomePage: string
+    welcomePage: string,
+    pamLogin: boolean,
 };
 
 export const LoginPanel = withStyles(styles)(
@@ -61,8 +73,9 @@ export const LoginPanel = withStyles(styles)(
         homeCluster: state.auth.homeCluster,
         uuidPrefix: state.auth.localCluster,
         loginCluster: state.auth.loginCluster,
-        welcomePage: state.auth.config.clusterConfig.Workbench.WelcomePageHTML
-    }))(({ classes, dispatch, remoteHosts, homeCluster, uuidPrefix, loginCluster, welcomePage }: LoginPanelProps) =>
+        welcomePage: state.auth.config.clusterConfig.Workbench.WelcomePageHTML,
+        pamLogin: state.auth.config.clusterConfig.Login.PAM,
+    }))(({ classes, dispatch, remoteHosts, homeCluster, uuidPrefix, loginCluster, welcomePage, pamLogin }: LoginPanelProps) =>
         <Grid container justify="center" alignItems="center"
             className={classes.root}
             style={{ marginTop: 56, overflowY: "auto", height: "100%" }}>
@@ -80,14 +93,19 @@ export const LoginPanel = withStyles(styles)(
                         </Select>
                     </Typography>}
 
-                <Typography component="div" align="right">
-                    <Button variant="contained" color="primary" style={{ margin: "1em" }} className={classes.button}
+                {pamLogin
+                ? <Typography component="div">
+                    <LoginForm handleSubmit={
+                        doPAMLogin(`https://${remoteHosts[homeCluster]}`)}/>
+                </Typography>
+                : <Typography component="div" align="right">
+                    <Button variant="contained" color="primary" style={{ margin: "1em" }}
+                        className={classes.button}
                         onClick={() => dispatch(login(uuidPrefix, homeCluster, loginCluster, remoteHosts))}>
-                        Log in
-                       {uuidPrefix !== homeCluster && loginCluster !== homeCluster &&
+                        Log in {uuidPrefix !== homeCluster && loginCluster !== homeCluster &&
                             <span>&nbsp;to {uuidPrefix} with user from {homeCluster}</span>}
                     </Button>
-                </Typography>
+                </Typography>}
             </Grid>
         </Grid >
     ));