fix(auth): sso auth bug when deploying with subpath (#3764)

This commit is contained in:
YANG QIA 2024-03-20 14:46:22 +08:00 committed by GitHub
parent a2ae4f7b70
commit 6a1be0fcd5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 10 deletions

View File

@ -3,20 +3,25 @@ import { AppSupervisor } from '@nocobase/server';
import { CASAuth } from '../auth';
export const service = async (ctx: Context, next: Next) => {
const { authenticator, __appName: appName, redirect = '/admin' } = ctx.action.params;
const { authenticator, __appName: appName, redirect } = ctx.action.params;
let prefix = '';
let prefix = process.env.APP_PUBLIC_PATH || '';
if (appName && appName !== 'main') {
const appSupervisor = AppSupervisor.getInstance();
if (appSupervisor?.runningMode !== 'single') {
prefix = process.env.APP_PUBLIC_PATH + `apps/${appName}`;
prefix += `apps/${appName}`;
}
}
const auth = (await ctx.app.authManager.get(authenticator, ctx)) as CASAuth;
if (prefix.endsWith('/')) {
prefix = prefix.slice(0, -1);
}
try {
const { token } = await auth.signIn();
ctx.redirect(`${prefix}${redirect}?authenticator=${authenticator}&token=${token}`);
ctx.redirect(`${prefix}${redirect || '/admin'}?authenticator=${authenticator}&token=${token}`);
} catch (error) {
ctx.redirect(`${prefix}/signin?authenticator=${authenticator}&error=${error.message}&redirect=${redirect}`);
}

View File

@ -10,14 +10,17 @@ export const redirect = async (ctx: Context, next: Next) => {
const authenticator = search.get('name');
const appName = search.get('app');
const redirect = search.get('redirect') || '/admin';
let prefix = '';
let prefix = process.env.APP_PUBLIC_PATH || '';
if (appName && appName !== 'main') {
const appSupervisor = AppSupervisor.getInstance();
if (appSupervisor?.runningMode !== 'single') {
prefix = process.env.APP_PUBLIC_PATH + `apps/${appName}`;
prefix += `apps/${appName}`;
}
}
const auth = (await ctx.app.authManager.get(authenticator, ctx)) as OIDCAuth;
if (prefix.endsWith('/')) {
prefix = prefix.slice(0, -1);
}
try {
const { token } = await auth.signIn();
ctx.redirect(`${prefix}${redirect}?authenticator=${authenticator}&token=${token}`);

View File

@ -4,18 +4,21 @@ import { SAMLAuth } from '../saml-auth';
export const redirect = async (ctx: Context, next: Next) => {
const { authenticator, __appName: appName } = ctx.action.params || {};
const { RelayState: redirect = '/admin' } = ctx.action.params.values || {};
let prefix = '';
const { RelayState: redirect } = ctx.action.params.values || {};
let prefix = process.env.APP_PUBLIC_PATH || '';
if (appName && appName !== 'main') {
const appSupervisor = AppSupervisor.getInstance();
if (appSupervisor?.runningMode !== 'single') {
prefix = process.env.APP_PUBLIC_PATH + `apps/${appName}`;
prefix += `/apps/${appName}`;
}
}
const auth = (await ctx.app.authManager.get(authenticator, ctx)) as SAMLAuth;
if (prefix.endsWith('/')) {
prefix = prefix.slice(0, -1);
}
try {
const { token } = await auth.signIn();
ctx.redirect(`${prefix}${redirect}?authenticator=${authenticator}&token=${token}`);
ctx.redirect(`${prefix}${redirect || '/admin'}?authenticator=${authenticator}&token=${token}`);
} catch (error) {
ctx.redirect(`${prefix}/signin?authenticator=${authenticator}&error=${error.message}&redirect=${redirect}`);
}