82 lines
3.0 KiB
TypeScript
82 lines
3.0 KiB
TypeScript
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
|
|
import { redirect } from "react-router";
|
|
import { AdminSsoConfigError, getAdminSsoClient, readAdminSsoEnv } from "~/modules/admin-sso";
|
|
import { clearIdTokenCookie, readIdTokenFromRequest } from "~/modules/admin-sso/logout.server";
|
|
import { writeAdminSsoAuditEvent } from "~/modules/admin-sso/audit.server";
|
|
import { getUserInfo, getUserSession, storage, validateCSRFToken } from "~/utils/session.server";
|
|
import { endServerSession } from "~/services/auth/sessionLifecycle.server";
|
|
|
|
/**
|
|
* Phase 09 Task #82 — POST /auth/admin/logout
|
|
*
|
|
* Always tears down the local App-Session and clears the id-token cookie.
|
|
* If an id_token is present AND Authentik is reachable, additionally
|
|
* redirects to Authentik's end_session_endpoint for RP-initiated logout.
|
|
* Otherwise falls back to /login?logged_out=1 directly so a stale
|
|
* IdP doesn't trap users inside the panel.
|
|
*/
|
|
|
|
async function destroyAppSession(request: Request): Promise<string> {
|
|
const session = await getUserSession(request);
|
|
const sessionToken = session.get("sessionToken");
|
|
if (typeof sessionToken === "string" && sessionToken.length > 0) {
|
|
// Logout must still clear the browser cookie if the store is unavailable.
|
|
await endServerSession(sessionToken).catch(() => undefined);
|
|
}
|
|
return storage.destroySession(session);
|
|
}
|
|
|
|
async function buildLogoutRedirect(request: Request): Promise<Response> {
|
|
const userInfo = await getUserInfo(request);
|
|
const idToken = readIdTokenFromRequest(request);
|
|
|
|
await writeAdminSsoAuditEvent({
|
|
action: "admin.logout.sso",
|
|
actorAdminUserId: userInfo.userId ?? null,
|
|
request,
|
|
meta: { hadIdToken: Boolean(idToken) },
|
|
});
|
|
|
|
let target = "/login?logged_out=1";
|
|
if (idToken) {
|
|
try {
|
|
const env = readAdminSsoEnv();
|
|
const client = await getAdminSsoClient(env);
|
|
target = client.endSessionUrl({
|
|
id_token_hint: idToken,
|
|
post_logout_redirect_uri: env.postLogoutRedirectUri ?? target,
|
|
});
|
|
} catch (err) {
|
|
// If Authentik is unreachable we still want to terminate the local
|
|
// session; just skip the IdP redirect.
|
|
if (!(err instanceof AdminSsoConfigError)) {
|
|
// swallow - logger absent here, audit already written
|
|
}
|
|
}
|
|
}
|
|
|
|
const headers = new Headers();
|
|
headers.append("Set-Cookie", await destroyAppSession(request));
|
|
headers.append("Set-Cookie", clearIdTokenCookie());
|
|
|
|
return redirect(target, { headers });
|
|
}
|
|
|
|
export async function action({ request }: ActionFunctionArgs): Promise<Response> {
|
|
await validateCSRFToken(request);
|
|
return buildLogoutRedirect(request);
|
|
}
|
|
|
|
export async function loader(_args: LoaderFunctionArgs): Promise<Response> {
|
|
// Logout changes authentication state and must never be triggerable by a
|
|
// cross-site image/link navigation.
|
|
return new Response(null, {
|
|
status: 405,
|
|
headers: { Allow: "POST", "Cache-Control": "no-store" },
|
|
});
|
|
}
|
|
|
|
export default function AuthAdminLogout() {
|
|
return null;
|
|
}
|