/
home
/
techb158
/
cosmic-risk.abdallabala.com
/
src
/
app
/
api
/
auth
/
sso
/
callback
/
/home/techb158/cosmic-risk.abdallabala.com/src/app/api/auth/sso/callback
mkdir
upload
Name
Size
Mode
Actions
route.js
2512
0644
edit
dl
rm
Edit:
/home/techb158/cosmic-risk.abdallabala.com/src/app/api/auth/sso/callback/route.js
(2512B)
const { handleSsoCallback } = require("../../../../../services/sso-service"); const { getOidcConfig, stateCookieName } = require("../../../../../lib/oidc"); const SESSION_MAX_AGE = 60 * 60 * 24 * 7; function getCookie(name, header) { const match = header?.match(new RegExp(`(?:^|;)\\s*${name}=([^;]+)`)); return match ? decodeURIComponent(match[1]) : null; } function setCookie(name, value, maxAge) { return `${name}=${value}; HttpOnly; ${process.env.NODE_ENV === "production" ? "Secure; " : ""}SameSite=Lax; Path=/; Max-Age=${maxAge}`; } export async function GET(request) { try { const { searchParams } = new URL(request.url); const code = searchParams.get("code"); const returnedState = searchParams.get("state"); const error = searchParams.get("error"); if (error) { return new Response(JSON.stringify({ error: `SSO provider returned: ${error}` }), { status: 400, headers: { "Content-Type": "application/json" }, }); } if (!code) { return new Response(JSON.stringify({ error: "Missing authorization code" }), { status: 400, headers: { "Content-Type": "application/json" }, }); } const cookieHeader = request.headers.get("cookie") || ""; const storedState = getCookie(stateCookieName(), cookieHeader); if (returnedState && storedState && returnedState !== storedState) { return new Response(JSON.stringify({ error: "State mismatch — possible CSRF" }), { status: 403, headers: { "Content-Type": "application/json" }, }); } const { session } = await handleSsoCallback(code); const stateClear = `${stateCookieName()}=; HttpOnly; SameSite=Lax; Path=/; Max-Age=0`; const sessionCookie = setCookie("cosmic_session", session.token, SESSION_MAX_AGE); const config = getOidcConfig(); const baseUrl = config?.redirectUrl ? new URL(config.redirectUrl).origin : (process.env.NEXT_PUBLIC_APP_URL || "http://localhost:8090"); return new Response(null, { status: 302, headers: { Location: `${baseUrl}/dashboard`, "Set-Cookie": `${sessionCookie}, ${stateClear}`, "Cache-Control": "no-store", }, }); } catch (error) { const msg = error.message || "SSO callback failed"; const status = error.status || 500; return new Response( `<html><body><h2>SSO Error</h2><p>${msg}</p><a href="/login">Back to login</a></body></html>`, { status, headers: { "Content-Type": "text/html" } } ); } }
Save
cmd:
run