Files
grafana/public/app/core/components/Login/LoginCtrl.tsx
Sofia Papagiannaki fefbbc65a8 Auth: Add support for forcing authentication in anonymous mode and modify SignIn to use it instead of redirect (#25567)
* Forbid additional redirect urls

* Optionally force login in anonymous mode

* Update LoginCtrl page to ignore redirect parameter

* Modify SignIn to set forceLogin query instead of redirect

* Pass appUrl to frontend and use URL API for updating url query

* Apply suggestions from code review

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix SignIn test

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
2020-06-16 16:33:44 +03:00

150 lines
3.7 KiB
TypeScript

import React from 'react';
import config from 'app/core/config';
import { updateLocation } from 'app/core/actions';
import { connect } from 'react-redux';
import { StoreState } from 'app/types';
import { PureComponent } from 'react';
import { getBackendSrv } from '@grafana/runtime';
import { hot } from 'react-hot-loader';
import appEvents from 'app/core/app_events';
import { AppEvents } from '@grafana/data';
const isOauthEnabled = () => {
return !!config.oauth && Object.keys(config.oauth).length > 0;
};
export interface FormModel {
user: string;
password: string;
email: string;
}
interface Props {
routeParams?: any;
updateLocation?: typeof updateLocation;
children: (props: {
isLoggingIn: boolean;
changePassword: (pw: string) => void;
isChangingPassword: boolean;
skipPasswordChange: Function;
login: (data: FormModel) => void;
disableLoginForm: boolean;
ldapEnabled: boolean;
authProxyEnabled: boolean;
disableUserSignUp: boolean;
isOauthEnabled: boolean;
loginHint: string;
passwordHint: string;
}) => JSX.Element;
}
interface State {
isLoggingIn: boolean;
isChangingPassword: boolean;
}
export class LoginCtrl extends PureComponent<Props, State> {
result: any = {};
constructor(props: Props) {
super(props);
this.state = {
isLoggingIn: false,
isChangingPassword: false,
};
if (config.loginError) {
appEvents.emit(AppEvents.alertWarning, ['Login Failed', config.loginError]);
}
}
changePassword = (password: string) => {
const pw = {
newPassword: password,
confirmNew: password,
oldPassword: 'admin',
};
getBackendSrv()
.put('/api/user/password', pw)
.then(() => {
this.toGrafana();
})
.catch((err: any) => console.log(err));
};
login = (formModel: FormModel) => {
this.setState({
isLoggingIn: true,
});
getBackendSrv()
.post('/login', formModel)
.then((result: any) => {
this.result = result;
if (formModel.password !== 'admin' || config.ldapEnabled || config.authProxyEnabled) {
this.toGrafana();
return;
} else {
this.changeView();
}
})
.catch(() => {
this.setState({
isLoggingIn: false,
});
});
};
changeView = () => {
this.setState({
isChangingPassword: true,
});
};
toGrafana = () => {
// Use window.location.href to force page reload
if (this.result.redirectUrl) {
if (config.appSubUrl !== '' && !this.result.redirectUrl.startsWith(config.appSubUrl)) {
window.location.href = config.appSubUrl + this.result.redirectUrl;
} else {
window.location.href = this.result.redirectUrl;
}
} else {
window.location.href = config.appSubUrl + '/';
}
};
render() {
const { children } = this.props;
const { isLoggingIn, isChangingPassword } = this.state;
const { login, toGrafana, changePassword } = this;
const { loginHint, passwordHint, disableLoginForm, ldapEnabled, authProxyEnabled, disableUserSignUp } = config;
return (
<>
{children({
isOauthEnabled: isOauthEnabled(),
loginHint,
passwordHint,
disableLoginForm,
ldapEnabled,
authProxyEnabled,
disableUserSignUp,
login,
isLoggingIn,
changePassword,
skipPasswordChange: toGrafana,
isChangingPassword,
})}
</>
);
}
}
export const mapStateToProps = (state: StoreState) => ({
routeParams: state.location.routeParams,
});
const mapDispatchToProps = { updateLocation };
export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(LoginCtrl));