修改登录逻辑
This commit is contained in:
@@ -19,23 +19,24 @@ export type AuthRuntimeConfig = {
|
||||
clockSkewSeconds: number;
|
||||
};
|
||||
|
||||
export function getAuthRuntimeConfig(): AuthRuntimeConfig {
|
||||
export type AuthClientMode = "default" | "admin";
|
||||
|
||||
export function getAuthRuntimeConfig(options: { clientMode?: AuthClientMode } = {}): AuthRuntimeConfig {
|
||||
const authBaseUrl = trimTrailingSlash(envValue("ZHINIAN_AUTH_BASE_URL", "AUTH_BASE"));
|
||||
const clientId = envValue("ZHINIAN_AUTH_CLIENT_ID", "AUTH_CLIENT_ID") || "app";
|
||||
const clientSecret = envValue("ZHINIAN_AUTH_CLIENT_SECRET", "AUTH_CLIENT_SECRET");
|
||||
const client = authClientConfig(options.clientMode || "default");
|
||||
const scope = envValue("ZHINIAN_AUTH_SCOPE", "AUTH_SCOPE") || "server";
|
||||
const issuer = envValue("ZHINIAN_AUTH_ISSUER", "AUTH_ISSUER") || "https://pig4cloud.com";
|
||||
const passwordEncryptionKey = envValue("ZHINIAN_AUTH_PASSWORD_ENC_KEY", "AUTH_PASSWORD_ENC_KEY", "AGENTBUS_SSO_PASSWORD_ENC_KEY");
|
||||
const sessionSecret = envValue("ZHINIAN_AUTH_SESSION_SECRET", "AUTH_SESSION_SECRET", "NEXTAUTH_SECRET");
|
||||
const explicitRequired = boolEnv("ZHINIAN_AUTH_REQUIRED");
|
||||
const disabled = boolEnv("ZHINIAN_AUTH_DISABLED") === true;
|
||||
const hasAnyAuthConfig = Boolean(authBaseUrl || clientSecret || sessionSecret);
|
||||
const hasAnyAuthConfig = Boolean(authBaseUrl || client.clientSecret || sessionSecret);
|
||||
const required = disabled ? false : explicitRequired ?? (process.env.NODE_ENV === "production" || Boolean(authBaseUrl));
|
||||
const wantsConfiguration = required || hasAnyAuthConfig;
|
||||
const missing: string[] = [];
|
||||
|
||||
if (wantsConfiguration && !authBaseUrl) missing.push("ZHINIAN_AUTH_BASE_URL");
|
||||
if (wantsConfiguration && !clientSecret) missing.push("ZHINIAN_AUTH_CLIENT_SECRET");
|
||||
if (wantsConfiguration && !client.clientSecret) missing.push(client.missingSecretKey);
|
||||
if (wantsConfiguration && !sessionSecret) missing.push("ZHINIAN_AUTH_SESSION_SECRET");
|
||||
|
||||
return {
|
||||
@@ -47,8 +48,8 @@ export function getAuthRuntimeConfig(): AuthRuntimeConfig {
|
||||
tokenUrl: endpointUrl(authBaseUrl, "ZHINIAN_AUTH_TOKEN_URL", "/oauth2/token"),
|
||||
jwksUrl: endpointUrl(authBaseUrl, "ZHINIAN_AUTH_JWKS_URL", "/oauth2/jwks"),
|
||||
logoutUrl: endpointUrl(authBaseUrl, "ZHINIAN_AUTH_LOGOUT_URL", "/token/logout"),
|
||||
clientId,
|
||||
clientSecret,
|
||||
clientId: client.clientId,
|
||||
clientSecret: client.clientSecret,
|
||||
scope,
|
||||
issuer,
|
||||
passwordEncryptionKey,
|
||||
@@ -63,7 +64,7 @@ export function safeNextPath(value: string | null | undefined, fallback = "/crea
|
||||
const parsed = new URL(value, "http://zhinian.local");
|
||||
if (parsed.origin !== "http://zhinian.local") return fallback;
|
||||
const path = `${parsed.pathname}${parsed.search}${parsed.hash}`;
|
||||
if (path.startsWith("/api/auth") || path.startsWith("/auth/login")) return fallback;
|
||||
if (path.startsWith("/api/auth") || path.startsWith("/auth/login") || path.startsWith("/auth/admin-login")) return fallback;
|
||||
return path;
|
||||
} catch {
|
||||
return fallback;
|
||||
@@ -102,6 +103,25 @@ function envValue(...names: string[]): string | undefined {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function authClientConfig(mode: AuthClientMode): {
|
||||
clientId: string;
|
||||
clientSecret?: string;
|
||||
missingSecretKey: string;
|
||||
} {
|
||||
if (mode === "admin") {
|
||||
return {
|
||||
clientId: envValue("ZHINIAN_ADMIN_AUTH_CLIENT_ID", "ADMIN_AUTH_CLIENT_ID") || "app",
|
||||
clientSecret: envValue("ZHINIAN_ADMIN_AUTH_CLIENT_SECRET", "ADMIN_AUTH_CLIENT_SECRET") || "app",
|
||||
missingSecretKey: "ZHINIAN_ADMIN_AUTH_CLIENT_SECRET"
|
||||
};
|
||||
}
|
||||
return {
|
||||
clientId: envValue("ZHINIAN_AUTH_CLIENT_ID", "AUTH_CLIENT_ID") || "app",
|
||||
clientSecret: envValue("ZHINIAN_AUTH_CLIENT_SECRET", "AUTH_CLIENT_SECRET"),
|
||||
missingSecretKey: "ZHINIAN_AUTH_CLIENT_SECRET"
|
||||
};
|
||||
}
|
||||
|
||||
function boolEnv(name: string): boolean | undefined {
|
||||
const value = process.env[name]?.trim().toLowerCase();
|
||||
if (!value || value === "auto") return undefined;
|
||||
|
||||
Reference in New Issue
Block a user