261102_01_ENH: 프로젝트 접근 권한 확인 및 탭별 선택 상태 분리, 공용 인증 쿠키 설정
This commit is contained in:
35
src/lib/auth-cookie.ts
Normal file
35
src/lib/auth-cookie.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import "server-only";
|
||||
import { NextResponse } from "next/server";
|
||||
import { AUTH_COOKIE_MAX_AGE } from "@/const/common.const";
|
||||
import { serverEnv } from "@/lib/env.server";
|
||||
import { CookieKeys } from "@/store/cookie/cookie-keys";
|
||||
|
||||
/** Issue/renew the normal-login cookie, or expire it when the token is empty. */
|
||||
export function setAuthCookie(response: NextResponse, token: string): void {
|
||||
const production = serverEnv.ENV === "prod";
|
||||
const domain = production ? serverEnv.AUTH_COOKIE_DOMAIN : undefined;
|
||||
if (production && !domain)
|
||||
throw new Error("AUTH_COOKIE_DOMAIN must be configured when ENV is prod.");
|
||||
|
||||
const options = {
|
||||
httpOnly: true,
|
||||
secure: production,
|
||||
sameSite: "lax" as const,
|
||||
path: "/",
|
||||
};
|
||||
// Serialize each cookie separately: ResponseCookies indexes cookies by name,
|
||||
// so setting the shared and host-only variants on one instance loses a header.
|
||||
const append = (value: string, cookieDomain: string | undefined, maxAge: number) => {
|
||||
const cookieResponse = new NextResponse(null);
|
||||
cookieResponse.cookies.set(CookieKeys.AuthToken, value, {
|
||||
...options,
|
||||
...(cookieDomain ? { domain: cookieDomain } : {}),
|
||||
maxAge,
|
||||
});
|
||||
for (const header of cookieResponse.headers.getSetCookie())
|
||||
response.headers.append("Set-Cookie", header);
|
||||
};
|
||||
|
||||
if (production) append("", undefined, 0);
|
||||
append(token, domain, token ? AUTH_COOKIE_MAX_AGE : 0);
|
||||
}
|
||||
@@ -4,4 +4,8 @@ import { createEnv, env } from "@kri/common/config";
|
||||
export const serverEnv = createEnv({
|
||||
ENV: env.required("ENV", process.env.ENV),
|
||||
API_SERVER: env.required("API_SERVER", process.env.API_SERVER),
|
||||
AUTH_COOKIE_DOMAIN: env.optional(
|
||||
"AUTH_COOKIE_DOMAIN",
|
||||
process.env.AUTH_COOKIE_DOMAIN,
|
||||
),
|
||||
});
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { apiError, createHttpClient, redirectToLogout } from "@kri/common/http";
|
||||
import { clientEnv } from "@/lib/env.client";
|
||||
import { announceProjectAccess, projectFromRequest } from "@/lib/project-access";
|
||||
|
||||
const resolveBaseUrl = () =>
|
||||
typeof window === "undefined"
|
||||
@@ -8,7 +9,14 @@ const resolveBaseUrl = () =>
|
||||
|
||||
export const nextApi = createHttpClient(resolveBaseUrl(), {
|
||||
onForbidden: (error) => {
|
||||
if (apiError(error).code === "EXTERNAL_SCOPE_DENIED") redirectToLogout();
|
||||
const info = apiError(error);
|
||||
if (info.code === "EXTERNAL_SCOPE_DENIED" || info.code === "PROJECT_IDENTITY_REQUIRED") {
|
||||
redirectToLogout();
|
||||
return;
|
||||
}
|
||||
const project = projectFromRequest(error.url);
|
||||
if (info.code === "PROJECT_ACCESS_DENIED" && project && !new URL(error.url).pathname.endsWith("/access"))
|
||||
announceProjectAccess({ project, allowed: false, message: info.message });
|
||||
},
|
||||
onResponseError: false,
|
||||
});
|
||||
|
||||
47
src/lib/project-access.ts
Normal file
47
src/lib/project-access.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { apiError } from "@kri/common/http";
|
||||
import { isProjectRef, type ProjectRef } from "@/types/kart";
|
||||
|
||||
export const PROJECT_ACCESS_EVENT = "kart:project-access";
|
||||
export type ProjectAccessEvent = {
|
||||
project: ProjectRef;
|
||||
allowed: boolean;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
export function announceProjectAccess(detail: ProjectAccessEvent) {
|
||||
if (typeof window !== "undefined")
|
||||
window.dispatchEvent(new CustomEvent(PROJECT_ACCESS_EVENT, { detail }));
|
||||
}
|
||||
|
||||
/** Match the project in a BFF request, not whichever project is selected later. */
|
||||
export function projectFromRequest(url: string): ProjectRef | null {
|
||||
try {
|
||||
const path = new URL(url, "https://kart.invalid").pathname;
|
||||
const match = path.match(/\/(?:project|quota|derived|bookmark|export|import|share)\/([01])\/([^/]+)(?:\/|$)/);
|
||||
if (!match) return null;
|
||||
const project = { projectType: Number(match[1]), pkey: decodeURIComponent(match[2]) };
|
||||
return isProjectRef(project) ? project : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function projectAccessMessage(error: unknown): string {
|
||||
const info = apiError(error, { fallbackMessage: "프로젝트 접근 권한을 확인하지 못했습니다. 다시 시도해 주세요." });
|
||||
return info.code === "PROJECT_ACCESS_DENIED"
|
||||
? "프로젝트에 접근할 권한이 없습니다."
|
||||
: info.message;
|
||||
}
|
||||
|
||||
export function projectFromSearch(search: URLSearchParams): ProjectRef | null {
|
||||
const type = search.get("projectType");
|
||||
if (search.getAll("projectType").length !== 1 || search.getAll("pkey").length !== 1 ||
|
||||
(type !== "0" && type !== "1")) return null;
|
||||
const project = { projectType: Number(type), pkey: search.get("pkey") };
|
||||
return isProjectRef(project) ? project : null;
|
||||
}
|
||||
|
||||
export function projectPageUrl(pathname: string, project: ProjectRef): string {
|
||||
const search = new URLSearchParams({ projectType: String(project.projectType), pkey: project.pkey });
|
||||
return pathname + "?" + search;
|
||||
}
|
||||
@@ -2,6 +2,8 @@ const progress = new Set(["day", "type", "loi", "screen"]);
|
||||
/** Browser inputs select only these implemented read operations, never an arbitrary upstream URL. */
|
||||
export function isProjectReadPath(slug: string[]): boolean {
|
||||
if (slug.length === 1) return slug[0] === "list" || slug[0] === "summary";
|
||||
if (slug.length === 3 && slug[2] === "access")
|
||||
return isProjectReadPath([slug[0], slug[1], "question", "list"]);
|
||||
return (
|
||||
slug.length === 4 &&
|
||||
(slug[0] === "0" || slug[0] === "1") &&
|
||||
|
||||
@@ -12,8 +12,8 @@ export function isProjectPage(path: string): boolean {
|
||||
}
|
||||
|
||||
/**
|
||||
* Project pages carry no project in the address (legacy KART kept it in client state); the
|
||||
* selected project lives in useSelectedProject. Other paths open the progress page.
|
||||
* ProjectEntry verifies and consumes incoming project parameters. Subsequent navigation
|
||||
* uses the tab's selected project; other paths open the progress page.
|
||||
*/
|
||||
export function projectPagePath(path: string): Route {
|
||||
return (isProjectPage(path) ? path : "/Report/Current") as Route;
|
||||
|
||||
Reference in New Issue
Block a user