1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import { connect, DispatchProp } from 'react-redux';
7 import { Grid, Typography, Button, Select } from '@material-ui/core';
8 import { CustomStyleRulesCallback } from 'common/custom-theme';
9 import { WithStyles, withStyles } from '@material-ui/core/styles';
10 import { login, authActions } from 'store/auth/auth-action';
11 import { ArvadosTheme } from 'common/custom-theme';
12 import { RootState } from 'store/store';
13 import { LoginForm } from 'views-components/login-form/login-form';
14 import Axios, { AxiosResponse } from 'axios';
15 import { Config } from 'common/config';
16 import { sanitizeHTML } from 'common/html-sanitize';
18 type CssRules = 'root' | 'container' | 'title' | 'content' | 'content__bolder' | 'button';
20 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
23 backgroundColor: theme.palette.grey["200"],
39 marginBottom: theme.spacing(6),
40 color: theme.palette.grey["800"]
43 marginBottom: theme.spacing(3),
45 color: theme.palette.grey["800"]
55 export type PasswordLoginResponse = {
61 const doPasswordLogin = (url: string) => (username: string, password: string) => {
62 const formData: string[] = [];
63 formData.push('username='+encodeURIComponent(username));
64 formData.push('password='+encodeURIComponent(password));
65 return Axios.post<string, AxiosResponse<PasswordLoginResponse>>(`${url}/arvados/v1/users/authenticate`, formData.join('&'), {
67 'Content-Type': 'application/x-www-form-urlencoded'
72 type LoginPanelProps = DispatchProp<any> & WithStyles<CssRules> & {
73 remoteHosts: { [key: string]: string },
78 passwordLogin: boolean,
81 const loginOptions = ['LDAP', 'PAM', 'Test'];
83 export const requirePasswordLogin = (config: Config): boolean => {
84 if (config && config.clusterConfig && config.clusterConfig.Login) {
86 .filter(loginOption => !!config.clusterConfig.Login[loginOption])
87 .map(loginOption => config.clusterConfig.Login[loginOption].Enable)
88 .find(enabled => enabled === true) || false;
93 export const LoginPanel = withStyles(styles)(
94 connect((state: RootState) => ({
95 remoteHosts: state.auth.remoteHosts,
96 homeCluster: state.auth.homeCluster,
97 localCluster: state.auth.localCluster,
98 loginCluster: state.auth.loginCluster,
99 welcomePage: state.auth.config.clusterConfig.Workbench.WelcomePageHTML,
100 passwordLogin: requirePasswordLogin(state.auth.remoteHostsConfig[state.auth.loginCluster || state.auth.homeCluster]),
101 }))(({ classes, dispatch, remoteHosts, homeCluster, localCluster, loginCluster, welcomePage, passwordLogin }: LoginPanelProps) => {
102 const loginBtnLabel = `Log in${(localCluster !== homeCluster && loginCluster !== homeCluster) ? " to "+localCluster+" with user from "+homeCluster : ''}`;
104 return (<Grid container justify="center" alignItems="center"
105 className={classes.root}
106 style={{ marginTop: 56, overflowY: "auto", height: "100%" }}>
107 <Grid item className={classes.container}>
108 <Typography component="div">
109 <div dangerouslySetInnerHTML={{ __html: sanitizeHTML(welcomePage) }} style={{ margin: "1em" }} />
111 {Object.keys(remoteHosts).length > 1 && loginCluster === "" &&
113 <Typography component="div" align="right">
114 <label>Please select the cluster that hosts your user account:</label>
115 <Select native value={homeCluster} style={{ margin: "1em" }}
116 onChange={(event) => dispatch(authActions.SET_HOME_CLUSTER(event.target.value))}>
117 {Object.keys(remoteHosts).map((k) => <option key={k} value={k}>{k}</option>)}
122 ? <Typography component="div">
123 <LoginForm dispatch={dispatch}
124 loginLabel={loginBtnLabel}
125 handleSubmit={doPasswordLogin(`https://${remoteHosts[loginCluster || homeCluster]}`)}/>
127 : <Typography component="div" align="right">
128 <Button variant="contained" color="primary" style={{ margin: "1em" }}
129 className={classes.button}
130 onClick={() => dispatch(login(localCluster, homeCluster, loginCluster, remoteHosts))}>