mirror of
https://github.com/fosrl/pangolin.git
synced 2026-05-26 10:43:09 +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 logger from "@server/logger";
|
||||||
import { resourceAccessToken, resources, sessions } from "@server/db";
|
import { resourceAccessToken, resources, sessions } from "@server/db";
|
||||||
import { db } from "@server/db";
|
import { db } from "@server/db";
|
||||||
import { eq } from "drizzle-orm";
|
import { and, eq, inArray, or, sql } from "drizzle-orm";
|
||||||
import {
|
import {
|
||||||
createResourceSession,
|
createResourceSession,
|
||||||
serializeResourceSessionCookie,
|
serializeResourceSessionCookie,
|
||||||
@@ -65,11 +65,31 @@ export async function exchangeSession(
|
|||||||
|
|
||||||
const clientIp = requestIp ? stripPortFromHost(requestIp) : undefined;
|
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()
|
.select()
|
||||||
.from(resources)
|
.from(resources)
|
||||||
.where(eq(resources.fullDomain, cleanHost))
|
.where(
|
||||||
.limit(1);
|
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) {
|
if (!resource) {
|
||||||
return next(
|
return next(
|
||||||
|
|||||||
Reference in New Issue
Block a user