Skip to content

Commit 7144a9d

Browse files
committed
fix test redirect
1 parent d39e4d6 commit 7144a9d

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { buildRouteAuthRedirectUris } from "../scripts/terrarium-traefik-sync";
3+
import { expectedRouteAuthRedirectUris } from "./integration/scenarios/common";
4+
5+
const routeAuthConfig = {
6+
terrarium_root_domain: "example.test",
7+
terrarium_manage_domain: "manage.example.test",
8+
terrarium_auth_domain: "auth.example.test"
9+
};
10+
11+
describe("integration route-auth redirect planning", () => {
12+
test("pre-registers the same route callback URIs that proxy sync will generate", () => {
13+
const labels = [
14+
"https://auth-run.example.test:8080@auth",
15+
"https://group-run.example.test:8080@auth:agents,admins",
16+
"https://*.example.test:8080@auth:admins~auth.example.test"
17+
];
18+
19+
const runtime = buildRouteAuthRedirectUris(labels, {
20+
...routeAuthConfig,
21+
terrarium_acme_dns_provider: "cloudflare"
22+
});
23+
24+
expect(runtime.errors).toEqual([]);
25+
expect(expectedRouteAuthRedirectUris(labels)).toEqual([...runtime.redirectUris].sort());
26+
});
27+
});

tests/integration/scenarios/common.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,26 @@ export function expectedRouteAuthRedirectUris(routeLabels: string[]): string[] {
8686

8787
const route = label.slice(0, authIndex);
8888
const suffix = label.slice(authIndex);
89-
if (!/^@auth(?::[A-Za-z0-9._,-]+)?$/.test(suffix)) {
89+
if (!/^@auth(?::[A-Za-z0-9._,-]+)?(?:~[A-Za-z0-9.-]+)?$/.test(suffix)) {
9090
throw new Error(`unsupported auth suffix: ${suffix}`);
9191
}
9292

9393
const parsed = new URL(route);
94+
const callbackIndex = suffix.indexOf("~");
95+
const policySuffix = callbackIndex === -1 ? suffix : suffix.slice(0, callbackIndex);
96+
const callbackHost = callbackIndex === -1 ? parsed.hostname : suffix.slice(callbackIndex + 1);
9497
const groups = [
9598
...new Set(
96-
suffix.includes(":")
97-
? suffix
98-
.slice(suffix.indexOf(":") + 1)
99+
policySuffix.includes(":")
100+
? policySuffix
101+
.slice(policySuffix.indexOf(":") + 1)
99102
.split(",")
100103
.map((group) => group.trim())
101104
.filter(Boolean)
102105
: []
103106
)
104107
].sort();
105-
const key = `${parsed.hostname}\n${groups.join("\n")}`;
108+
const key = `${parsed.hostname}\n${callbackHost}\n${groups.join("\n")}`;
106109
if (profiles.has(key)) {
107110
continue;
108111
}
@@ -112,7 +115,7 @@ export function expectedRouteAuthRedirectUris(routeLabels: string[]): string[] {
112115
const base = slugify(`${parsed.hostname}-${policy}`);
113116
const trimmed = base.length > 56 ? base.slice(0, 56).replace(/-+$/g, "") : base;
114117
const hash = createHash("sha256").update(key).digest("hex").slice(0, 10);
115-
redirectUris.push(`https://${parsed.hostname}/oauth2/route/${trimmed || "route"}-${hash}/callback`);
118+
redirectUris.push(`https://${callbackHost}/oauth2/route/${trimmed || "route"}-${hash}/callback`);
116119
}
117120
return redirectUris.sort();
118121
}

0 commit comments

Comments
 (0)