Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support arbitrary login redirect routes #522

Merged
merged 3 commits into from May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 3 additions & 7 deletions webui/react/src/pages/Authentication.tsx
Expand Up @@ -8,7 +8,7 @@ import DeterminedAuth from 'components/DeterminedAuth';
import Logo, { LogoTypes } from 'components/Logo';
import Spinner from 'components/Spinner';
import Auth, { updateAuth } from 'contexts/Auth';
import { crossoverRoute, isCrossoverRoute } from 'routes';
import { routeAll } from 'routes';
import history from 'routes/history';
import { logout } from 'services/api';

Expand Down Expand Up @@ -45,15 +45,11 @@ const Authentication: React.FC = () => {
}, [ setAuth ]);

if (auth.isAuthenticated) {
const redirectUrl = queries.redirect || DEFAULT_REDIRECT;
const redirect = queries.redirect || DEFAULT_REDIRECT;
if (queries.cli) {
return <AuthToken />;
}
if (isCrossoverRoute(redirectUrl)) {
crossoverRoute(redirectUrl);
} else {
history.push(redirectUrl);
}
routeAll(redirect);
return <Spinner fullPage />;
}

Expand Down
50 changes: 34 additions & 16 deletions webui/react/src/routes/index.ts
Expand Up @@ -3,6 +3,8 @@ import { RouteProps } from 'react-router';
import Authentication from 'pages/Authentication';
import Dashboard from 'pages/Dashboard';
import Determined from 'pages/Determined';
import history from 'routes/history';
import { isFullPath, parseUrl } from 'utils/routes';

/*
* Router Configuration
Expand All @@ -20,22 +22,6 @@ export interface RouteConfigItem extends RouteProps {
needAuth?: boolean;
}

export const isFullPath = (path: string): boolean => {
return path.startsWith('http');
};

export const isCrossoverRoute = (path: string): boolean => {
return path.startsWith('/ui') || path.includes(':8080/ui');
};

export const crossoverRoute = (path: string): void => {
if (!isFullPath(path)) {
const pathPrefix = process.env.IS_DEV ? 'http://localhost:8080' : '';
path = `${pathPrefix}${path}`;
}
window.location.assign(path);
};

export const appRoutes: RouteConfigItem[] = [
{
component: Determined,
Expand Down Expand Up @@ -115,3 +101,35 @@ export const detRoutes: RouteConfigItem[] = [
},
];
export const defaultDetRouteId = detRoutes[0].id;

// Is the path going to be served from the same host?
const isDetRoute = (url: string): boolean => {
if (!isFullPath(url)) return true;
if (process.env.IS_DEV) {
// dev live is served on a different port
return parseUrl(url).hostname === window.location.hostname;
}
return parseUrl(url).host === window.location.host;
};

const isReactRoute = (url: string): boolean => {
if (!isDetRoute(url)) return false;
const pathname = parseUrl(url).pathname;
return !!appRoutes.find(route => pathname.startsWith(route.path));
};

export const routeToExternalUrl = (path: string): void => {
if (!isFullPath(path)) {
const pathPrefix = process.env.IS_DEV ? 'http://localhost:8080' : '';
path = `${pathPrefix}${path}`;
}
window.location.assign(path);
};

export const routeAll = (path: string): void => {
if (!isReactRoute(path)) {
routeToExternalUrl(path);
} else {
history.push(path);
}
};
12 changes: 12 additions & 0 deletions webui/react/src/utils/routes.ts
@@ -0,0 +1,12 @@
export const isFullPath = (url: string): boolean => url.startsWith('http');

export const isAbsolutePath = (url: string): boolean => url.startsWith('/');

export const parseUrl = (url: string): URL => {
let cleanUrl = url;
if (!isFullPath(url)) {
if (!isAbsolutePath(url)) cleanUrl = '/' + url;
cleanUrl = window.location.origin + url;
}
return new window.URL(cleanUrl);
};