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

fix(serverAdd): block server addition and selection when disallowed by config #2416

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 34 additions & 20 deletions frontend/src/plugins/router/middlewares/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,52 @@ import { getJSONConfig } from '@/utils/external-config';
const serverAddUrl = '/server/add';
const serverSelectUrl = '/server/select';
const serverLoginUrl = '/server/login';
const routes = new Set([serverAddUrl, serverSelectUrl, serverLoginUrl]);
const serverRoutes = new Set([serverAddUrl, serverSelectUrl]);
const routes = new Set([...serverRoutes, serverLoginUrl]);

/**
* Performs the login guard redirection ensuring no redirection loops happen
*/
function doRedir(dest: RouteLocationPathRaw, to: RouteLocationNormalized) {
return to.path === dest.path
? true
: dest;
}

/**
* Redirects to login page if there's no user logged in.
*/
export async function loginGuard(
to: RouteLocationNormalized
): Promise<boolean | RouteLocationRaw> {
let destinationRoute: RouteLocationPathRaw | undefined;
const jsonConfig = await getJSONConfig();

if (!isNil(remote.auth.currentServer) && !isNil(remote.auth.currentUser) && !isNil(remote.auth.currentUserToken) && routes.has(to.path)) {
destinationRoute = { path: '/', replace: true };
if (jsonConfig.defaultServerURLs.length && isNil(remote.auth.currentServer)) {
await until(() => remote.auth.currentServer).toBeTruthy({ flush: 'pre' });
}

if (remote.auth.servers.length <= 0 && jsonConfig.defaultServerURLs.length <= 0) {
destinationRoute = { path: serverAddUrl, replace: true };
} else if (!routes.has(to.path)) {
if (isNil(remote.auth.currentServer)) {
if (jsonConfig.allowServerSelection) {
destinationRoute = { path: serverSelectUrl, replace: true };
} else {
await until(() => remote.auth.currentServer).toBeTruthy({ flush: 'pre' });
if (
(
!jsonConfig.allowServerSelection
&& serverRoutes.has(to.path)
)
|| (
!isNil(remote.auth.currentServer)
&& !isNil(remote.auth.currentUser)
&& !isNil(remote.auth.currentUserToken)
&& routes.has(to.path)
)
) {
return doRedir({ path: '/', replace: true }, to);
}

return loginGuard(to);
}
} else if (isNil(remote.auth.currentUser)) {
destinationRoute = { path: serverLoginUrl, replace: true };
}
if (!remote.auth.servers.length) {
return doRedir({ path: serverAddUrl, replace: true }, to);
} else if (isNil(remote.auth.currentServer)) {
return doRedir({ path: serverSelectUrl, replace: true }, to);
} else if (isNil(remote.auth.currentUser)) {
return doRedir({ path: serverLoginUrl, replace: true }, to);
}

return destinationRoute && to.path !== destinationRoute.path
? destinationRoute
: true;
return true;
}
Loading