Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+31
-11
@@ -2,16 +2,16 @@
|
||||
* Phase 09 Task #82 — Admin-SSO logout helpers.
|
||||
*
|
||||
* Stores the raw OIDC `id_token` in a separate HMAC-signed HttpOnly
|
||||
* cookie (scoped to /auth/admin) so that /auth/admin/logout can supply
|
||||
* cookie (scoped to the dedicated admin host) so that /auth/admin/logout can supply
|
||||
* `id_token_hint` for RP-initiated logout without bloating the main
|
||||
* session cookie or persisting tokens in the DB.
|
||||
*/
|
||||
import { createHmac, timingSafeEqual } from "node:crypto";
|
||||
import { loadRequiredWebConfig } from "@hivegrid/config";
|
||||
import { loadRequiredWebConfig, resolveHostOnlyCookiePolicy } from "@hivegrid/config";
|
||||
|
||||
export const ADMIN_SSO_ID_TOKEN_COOKIE = "hivegrid_admin_sso_id_token";
|
||||
export const ADMIN_SSO_ID_TOKEN_TTL_MS = 12 * 60 * 60 * 1000; // mirror app-session lifetime
|
||||
const COOKIE_PATH = "/auth/admin";
|
||||
export const ADMIN_SSO_ID_TOKEN_COOKIE = "__Host-hivegrid_admin_sso_id_token";
|
||||
export const LOCAL_ADMIN_SSO_ID_TOKEN_COOKIE = "hivegrid_admin_sso_id_token";
|
||||
export const ADMIN_SSO_ID_TOKEN_TTL_MS = 15 * 60 * 1000; // bounded to the absolute AdminUser session lifetime
|
||||
|
||||
function getSigningSecret(override?: string): string {
|
||||
if (override) return override;
|
||||
@@ -27,19 +27,33 @@ function sign(payload: string, secret: string): string {
|
||||
return createHmac("sha256", secret).update(payload).digest("base64url");
|
||||
}
|
||||
|
||||
function getCookiePolicy() {
|
||||
const webConfig = loadRequiredWebConfig();
|
||||
// `__Host-` requires Path=/. The dedicated admin hostname is the narrowest
|
||||
// safe scope that preserves the browser-enforced no-Domain guarantee.
|
||||
return resolveHostOnlyCookiePolicy({
|
||||
nodeEnv: webConfig.nodeEnv,
|
||||
routing: webConfig.routing,
|
||||
secureName: ADMIN_SSO_ID_TOKEN_COOKIE,
|
||||
localName: LOCAL_ADMIN_SSO_ID_TOKEN_COOKIE,
|
||||
maxAge: Math.ceil(ADMIN_SSO_ID_TOKEN_TTL_MS / 1000),
|
||||
});
|
||||
}
|
||||
|
||||
export function serializeIdTokenCookie(idToken: string, opts: { secret?: string } = {}): string {
|
||||
const secret = getSigningSecret(opts.secret);
|
||||
const sig = sign(idToken, secret);
|
||||
const value = `${Buffer.from(idToken, "utf8").toString("base64url")}.${sig}`;
|
||||
const maxAge = Math.ceil(ADMIN_SSO_ID_TOKEN_TTL_MS / 1000);
|
||||
const attrs = [`${ADMIN_SSO_ID_TOKEN_COOKIE}=${value}`, `Path=${COOKIE_PATH}`, "HttpOnly", "SameSite=Lax", `Max-Age=${maxAge}`];
|
||||
if (process.env.NODE_ENV !== "test") attrs.push("Secure");
|
||||
const policy = getCookiePolicy();
|
||||
const attrs = [`${policy.name}=${value}`, `Path=${policy.path}`, "HttpOnly", "SameSite=Lax", `Max-Age=${policy.maxAge}`];
|
||||
if (policy.secure) attrs.push("Secure");
|
||||
return attrs.join("; ");
|
||||
}
|
||||
|
||||
export function clearIdTokenCookie(): string {
|
||||
const attrs = [`${ADMIN_SSO_ID_TOKEN_COOKIE}=`, `Path=${COOKIE_PATH}`, "HttpOnly", "SameSite=Lax", "Max-Age=0"];
|
||||
if (process.env.NODE_ENV !== "test") attrs.push("Secure");
|
||||
const policy = getCookiePolicy();
|
||||
const attrs = [`${policy.name}=`, `Path=${policy.path}`, "HttpOnly", "SameSite=Lax", "Max-Age=0"];
|
||||
if (policy.secure) attrs.push("Secure");
|
||||
return attrs.join("; ");
|
||||
}
|
||||
|
||||
@@ -55,7 +69,13 @@ function readCookie(request: Request, name: string): string | null {
|
||||
}
|
||||
|
||||
export function readIdTokenFromRequest(request: Request, opts: { secret?: string } = {}): string | null {
|
||||
const value = readCookie(request, ADMIN_SSO_ID_TOKEN_COOKIE);
|
||||
let cookieName: string;
|
||||
try {
|
||||
cookieName = getCookiePolicy().name;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
const value = readCookie(request, cookieName);
|
||||
if (!value) return null;
|
||||
const dot = value.lastIndexOf(".");
|
||||
if (dot <= 0) return null;
|
||||
|
||||
Reference in new issue
Block a user