feat(hub): issue trusted Device Core session claims
This commit is contained in:
+43
-27
@@ -1,6 +1,6 @@
|
||||
import express from "express";
|
||||
import { createServer as createHttpServer } from "node:http";
|
||||
import { randomBytes, randomUUID, createHash, timingSafeEqual } from "node:crypto";
|
||||
import { randomBytes, randomUUID, createHash } from "node:crypto";
|
||||
import { existsSync, readFileSync } from "node:fs";
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { dirname, extname, isAbsolute, join, resolve } from "node:path";
|
||||
@@ -8,6 +8,11 @@ import { fileURLToPath } from "node:url";
|
||||
import { createRemoteJWKSet, jwtVerify } from "jose";
|
||||
import { createAuthentikSyncClient, resolveRequiredGroups } from "./authentik-sync.mjs";
|
||||
import { createControlPlaneStore } from "./control-plane-store.mjs";
|
||||
import { resolveDeviceCoreSessionAccess } from "./device-core-session-access.mjs";
|
||||
import {
|
||||
isInternalRequestTokenAuthorized,
|
||||
isInternalRequestTokenConfigured,
|
||||
} from "./internal-request-auth.mjs";
|
||||
import { createNotificationCoreClient } from "./notification-core-client.mjs";
|
||||
|
||||
const serverRoot = dirname(fileURLToPath(import.meta.url));
|
||||
@@ -68,6 +73,7 @@ app.get("/healthz", (_req, res) => {
|
||||
oidcConfigured: config.oidcConfigured,
|
||||
authentikApiConfigured: authentikSyncClient.isConfigured(),
|
||||
internalAccessApiConfigured: Boolean(config.internalAccessToken),
|
||||
deviceCoreInternalAccessConfigured: Boolean(config.deviceCoreInternalAccessToken),
|
||||
notificationCoreConfigured: notificationCoreClient.isConfigured(),
|
||||
});
|
||||
});
|
||||
@@ -454,16 +460,17 @@ app.get("/api/services/:serviceSlug/launch", requireSession, (req, res) => {
|
||||
});
|
||||
|
||||
app.post("/api/internal/handoff/consume", (req, res) => {
|
||||
if (!isInternalRequestAuthorized(req)) {
|
||||
res.status(config.internalAccessToken ? 401 : 503).json({
|
||||
const serviceSlug = sanitizeServiceSlug(req.body?.serviceSlug);
|
||||
if (!isInternalRequestAuthorized(req, { serviceSlug })) {
|
||||
const configured = isInternalRequestConfigured(serviceSlug);
|
||||
res.status(configured ? 401 : 503).json({
|
||||
ok: false,
|
||||
error: config.internalAccessToken ? "internal_handoff_unauthorized" : "internal_handoff_not_configured",
|
||||
error: configured ? "internal_handoff_unauthorized" : "internal_handoff_not_configured",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const token = typeof req.body?.token === "string" ? req.body.token : "";
|
||||
const serviceSlug = sanitizeServiceSlug(req.body?.serviceSlug);
|
||||
const handoff = consumeServiceHandoff(token, serviceSlug);
|
||||
|
||||
if (!handoff) {
|
||||
@@ -488,6 +495,7 @@ app.post("/api/internal/handoff/consume", (req, res) => {
|
||||
ok: true,
|
||||
serviceSlug,
|
||||
launcherSessionId: handoff.launcherSessionId ?? null,
|
||||
access: resolveDeviceCoreSessionAccess(snapshot.data, user, serviceSlug),
|
||||
user: {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
@@ -501,16 +509,17 @@ app.post("/api/internal/handoff/consume", (req, res) => {
|
||||
});
|
||||
|
||||
app.post("/api/internal/session/validate", (req, res) => {
|
||||
if (!isInternalRequestAuthorized(req)) {
|
||||
res.status(config.internalAccessToken ? 401 : 503).json({
|
||||
const serviceSlug = sanitizeServiceSlug(req.body?.serviceSlug);
|
||||
if (!isInternalRequestAuthorized(req, { serviceSlug })) {
|
||||
const configured = isInternalRequestConfigured(serviceSlug);
|
||||
res.status(configured ? 401 : 503).json({
|
||||
ok: false,
|
||||
active: false,
|
||||
error: config.internalAccessToken ? "internal_session_unauthorized" : "internal_session_not_configured",
|
||||
error: configured ? "internal_session_unauthorized" : "internal_session_not_configured",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const serviceSlug = sanitizeServiceSlug(req.body?.serviceSlug);
|
||||
const launcherSessionId = typeof req.body?.launcherSessionId === "string" ? req.body.launcherSessionId : "";
|
||||
|
||||
if (!launcherSessionId) {
|
||||
@@ -551,6 +560,7 @@ app.post("/api/internal/session/validate", (req, res) => {
|
||||
active: true,
|
||||
serviceSlug,
|
||||
launcherSessionId,
|
||||
access: resolveDeviceCoreSessionAccess(snapshot.data, user, serviceSlug),
|
||||
user: {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
@@ -2531,6 +2541,9 @@ function readConfig() {
|
||||
process.env.NODEDC_INTERNAL_ACCESS_TOKEN ??
|
||||
process.env.NODEDC_PLATFORM_SERVICE_TOKEN ??
|
||||
"",
|
||||
deviceCoreInternalAccessToken: readSecretFile(
|
||||
process.env.NODEDC_DEVICE_CORE_INTERNAL_TOKEN_FILE,
|
||||
),
|
||||
taskLogoutUrl:
|
||||
process.env.TASK_LOGOUT_URL ??
|
||||
`${(process.env.TASK_BASE_URL ?? `http://${process.env.TASK_DOMAIN ?? "task.local.nodedc"}`).replace(/\/$/, "")}/logout`,
|
||||
@@ -2549,6 +2562,13 @@ function readConfig() {
|
||||
};
|
||||
}
|
||||
|
||||
function readSecretFile(value) {
|
||||
const pathname = String(value || "").trim();
|
||||
if (!pathname) return "";
|
||||
const resolvedPath = isAbsolute(pathname) ? pathname : resolve(projectRoot, pathname);
|
||||
return readFileSync(resolvedPath, "utf8").trim();
|
||||
}
|
||||
|
||||
function ensureOidcConfigured() {
|
||||
if (!config.oidcConfigured) {
|
||||
throw new Error("Launcher OIDC is not configured. Set LAUNCHER_OIDC_ISSUER, LAUNCHER_OIDC_CLIENT_ID and LAUNCHER_OIDC_CLIENT_SECRET.");
|
||||
@@ -4883,28 +4903,24 @@ function readRequestHeader(req, name) {
|
||||
return typeof value === "string" ? value.trim() : "";
|
||||
}
|
||||
|
||||
function isInternalRequestAuthorized(req) {
|
||||
if (!config.internalAccessToken) {
|
||||
return false;
|
||||
}
|
||||
|
||||
function isInternalRequestAuthorized(req, { serviceSlug = "" } = {}) {
|
||||
const authorization = typeof req.headers.authorization === "string" ? req.headers.authorization : "";
|
||||
const bearerToken = authorization.match(/^Bearer\s+(.+)$/i)?.[1] ?? "";
|
||||
const headerToken = typeof req.headers["x-nodedc-internal-token"] === "string" ? req.headers["x-nodedc-internal-token"] : "";
|
||||
const requestToken = bearerToken || headerToken;
|
||||
|
||||
return safeTokenEquals(requestToken, config.internalAccessToken);
|
||||
return isInternalRequestTokenAuthorized({
|
||||
authorization,
|
||||
headerToken,
|
||||
globalToken: config.internalAccessToken,
|
||||
scopedToken: config.deviceCoreInternalAccessToken,
|
||||
serviceSlug,
|
||||
});
|
||||
}
|
||||
|
||||
function safeTokenEquals(actual, expected) {
|
||||
if (!actual || !expected) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const actualBuffer = Buffer.from(String(actual));
|
||||
const expectedBuffer = Buffer.from(String(expected));
|
||||
|
||||
return actualBuffer.length === expectedBuffer.length && timingSafeEqual(actualBuffer, expectedBuffer);
|
||||
function isInternalRequestConfigured(serviceSlug = "") {
|
||||
return isInternalRequestTokenConfigured({
|
||||
globalToken: config.internalAccessToken,
|
||||
scopedToken: config.deviceCoreInternalAccessToken,
|
||||
serviceSlug,
|
||||
});
|
||||
}
|
||||
|
||||
function findInternalAccessUser(data, payload) {
|
||||
|
||||
Reference in New Issue
Block a user