mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
committed by
GitHub
parent
b4136c1eca
commit
fefbbc65a8
@@ -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 {
|
||||
|
||||
@@ -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="/" />);
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user