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>
This commit is contained in:
Sofia Papagiannaki
2020-06-16 16:33:44 +03:00
committed by GitHub
parent b4136c1eca
commit fefbbc65a8
10 changed files with 133 additions and 16 deletions

View File

@@ -101,15 +101,8 @@ export class LoginCtrl extends PureComponent<Props, State> {
};
toGrafana = () => {
const params = this.props.routeParams;
// Use window.location.href to force page reload
if (params.redirect && params.redirect[0] === '/') {
if (config.appSubUrl !== '' && !params.redirect.startsWith(config.appSubUrl)) {
window.location.href = config.appSubUrl + params.redirect;
} else {
window.location.href = params.redirect;
}
} else if (this.result.redirectUrl) {
if (this.result.redirectUrl) {
if (config.appSubUrl !== '' && !this.result.redirectUrl.startsWith(config.appSubUrl)) {
window.location.href = config.appSubUrl + this.result.redirectUrl;
} else {

View File

@@ -2,6 +2,10 @@ import React from 'react';
import { shallow } from 'enzyme';
import { SignIn } from './SignIn';
jest.mock('../../config', () => ({
appUrl: 'http://localhost:3000/',
}));
describe('Render', () => {
it('should render component', () => {
const wrapper = shallow(<SignIn url="/" />);

View File

@@ -1,18 +1,26 @@
import React, { FC } from 'react';
import config from 'app/core/config';
import { connectWithStore } from 'app/core/utils/connectWithReduxStore';
import { StoreState } from 'app/types';
import { Icon } from '@grafana/ui';
const getForcedLoginUrl = (url: string) => {
const urlObj = new URL(url, config.appUrl);
let params = urlObj.searchParams;
params.set('forceLogin', 'true');
return urlObj.toString();
};
export const SignIn: FC<any> = ({ url }) => {
const loginUrl = `login?redirect=${encodeURIComponent(url)}`;
const forcedLoginUrl = getForcedLoginUrl(url);
return (
<div className="sidemenu-item">
<a href={loginUrl} className="sidemenu-link" target="_self">
<a href={forcedLoginUrl} className="sidemenu-link" target="_self">
<span className="icon-circle sidemenu-icon">
<Icon name="sign-in-alt" size="xl" />
</span>
</a>
<a href={loginUrl} target="_self">
<a href={forcedLoginUrl} target="_self">
<ul className="dropdown-menu dropdown-menu--sidemenu" role="menu">
<li className="side-menu-header">
<span className="sidemenu-item-text">Sign In</span>

View File

@@ -6,7 +6,7 @@ exports[`Render should render component 1`] = `
>
<a
className="sidemenu-link"
href="login?redirect=%2F"
href="http://localhost:3000/?forceLogin=true"
target="_self"
>
<span
@@ -19,7 +19,7 @@ exports[`Render should render component 1`] = `
</span>
</a>
<a
href="login?redirect=%2F"
href="http://localhost:3000/?forceLogin=true"
target="_self"
>
<ul