Update exchange session to support wildcards

This commit is contained in:
Owen
2026-04-23 21:38:12 -07:00
parent b4f0b4e285
commit 6a96f743aa

View File

@@ -6,7 +6,7 @@ import { fromError } from "zod-validation-error";
import logger from "@server/logger";
import { resourceAccessToken, resources, sessions } from "@server/db";
import { db } from "@server/db";
import { eq } from "drizzle-orm";
import { and, eq, inArray, or, sql } from "drizzle-orm";
import {
createResourceSession,
serializeResourceSessionCookie,
@@ -65,11 +65,31 @@ export async function exchangeSession(
const clientIp = requestIp ? stripPortFromHost(requestIp) : undefined;
const [resource] = await db
const parts = cleanHost.split(".");
const wildcardCandidates: string[] = [];
for (let i = 1; i < parts.length; i++) {
wildcardCandidates.push(`*.${parts.slice(i).join(".")}`);
}
const potentialResources = await db
.select()
.from(resources)
.where(eq(resources.fullDomain, cleanHost))
.limit(1);
.where(
or(
eq(resources.fullDomain, cleanHost),
wildcardCandidates.length > 0
? and(
eq(resources.wildcard, true),
inArray(resources.fullDomain, wildcardCandidates)
)
: sql`false`
)
);
const exactMatch = potentialResources.find(
(r) => r.fullDomain === cleanHost
);
const resource = exactMatch ?? potentialResources[0];
if (!resource) {
return next(