mirror of
https://github.com/fosrl/pangolin.git
synced 2026-05-26 02:32:36 +00:00
Update exchange session to support wildcards
This commit is contained in:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user