Skip to content

Commit 41c09cd

Browse files
committed
feat: implement forward authentication settings and UI components
- Added a new `forward_auth_settings` table to manage authentication domains and their configurations. - Introduced UI components for handling forward authentication, including enabling/disabling SSO for domains and selecting SSO providers. - Updated existing tests to include validation for the new `forwardAuthProviderId` field in domain configurations. - Enhanced the dashboard to integrate forward authentication management, allowing users to configure SSO settings directly from the application interface. This update improves the flexibility and security of application authentication by allowing integration with various identity providers.
1 parent 6ff2ca0 commit 41c09cd

28 files changed

Lines changed: 27770 additions & 26 deletions

File tree

‎apps/dokploy/__test__/compose/domain/host-rule-format.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe("Host rule format regression tests", () => {
3434
stripPath: false,
3535
customEntrypoint: null,
3636
middlewares: null,
37+
forwardAuthProviderId: null,
3738
};
3839

3940
describe("Host rule format validation", () => {

‎apps/dokploy/__test__/compose/domain/labels.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe("createDomainLabels", () => {
2323
internalPath: "/",
2424
stripPath: false,
2525
middlewares: null,
26+
forwardAuthProviderId: null,
2627
};
2728

2829
it("should create basic labels for web entrypoint", async () => {
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
import type { ApplicationNested, Domain } from "@dokploy/server";
2+
import {
3+
buildForwardAuthEnv,
4+
createRouterConfig,
5+
deriveBaseDomain,
6+
deriveCookieSecret,
7+
forwardAuthCallbackUrl,
8+
forwardAuthMiddlewareName,
9+
} from "@dokploy/server";
10+
import { beforeAll, describe, expect, test } from "vitest";
11+
12+
const app = {
13+
appName: "my-app",
14+
redirects: [],
15+
security: [],
16+
} as unknown as ApplicationNested;
17+
18+
const baseDomain: Domain = {
19+
applicationId: "app-1",
20+
certificateType: "none",
21+
createdAt: "",
22+
domainId: "domain-1",
23+
host: "app.example.com",
24+
https: false,
25+
path: null,
26+
port: 3000,
27+
customEntrypoint: null,
28+
serviceName: "",
29+
composeId: "",
30+
customCertResolver: null,
31+
domainType: "application",
32+
uniqueConfigKey: 7,
33+
previewDeploymentId: "",
34+
internalPath: "/",
35+
stripPath: false,
36+
middlewares: null,
37+
forwardAuthProviderId: null,
38+
};
39+
40+
describe("forwardAuthMiddlewareName", () => {
41+
test("is stable and unique per app + uniqueConfigKey", () => {
42+
expect(forwardAuthMiddlewareName("my-app", 7)).toBe(
43+
"forward-auth-my-app-7",
44+
);
45+
expect(forwardAuthMiddlewareName("my-app", 7)).toBe(
46+
forwardAuthMiddlewareName("my-app", 7),
47+
);
48+
expect(forwardAuthMiddlewareName("my-app", 7)).not.toBe(
49+
forwardAuthMiddlewareName("my-app", 8),
50+
);
51+
});
52+
});
53+
54+
describe("createRouterConfig forward-auth wiring", () => {
55+
test("does NOT add forward-auth middleware when no provider is linked", async () => {
56+
const config = await createRouterConfig(app, baseDomain, "websecure");
57+
expect(config.middlewares).not.toContain(
58+
forwardAuthMiddlewareName("my-app", 7),
59+
);
60+
});
61+
62+
test("adds forward-auth middleware when a provider is linked", async () => {
63+
const domain: Domain = {
64+
...baseDomain,
65+
forwardAuthProviderId: "provider-abc",
66+
};
67+
const config = await createRouterConfig(app, domain, "websecure");
68+
expect(config.middlewares).toContain(
69+
forwardAuthMiddlewareName("my-app", 7),
70+
);
71+
});
72+
73+
test("forward-auth runs before custom domain middlewares", async () => {
74+
const domain: Domain = {
75+
...baseDomain,
76+
forwardAuthProviderId: "provider-abc",
77+
middlewares: ["rate-limit@file"],
78+
};
79+
const config = await createRouterConfig(app, domain, "websecure");
80+
const forwardAuthIdx = config.middlewares?.indexOf(
81+
forwardAuthMiddlewareName("my-app", 7),
82+
);
83+
const customIdx = config.middlewares?.indexOf("rate-limit@file");
84+
expect(forwardAuthIdx).toBeGreaterThanOrEqual(0);
85+
expect(customIdx).toBeGreaterThan(forwardAuthIdx as number);
86+
});
87+
88+
test("redirect-only web router does not get the forward-auth middleware", async () => {
89+
const domain: Domain = {
90+
...baseDomain,
91+
https: true,
92+
forwardAuthProviderId: "provider-abc",
93+
};
94+
const config = await createRouterConfig(app, domain, "web");
95+
expect(config.middlewares).toContain("redirect-to-https");
96+
expect(config.middlewares).not.toContain(
97+
forwardAuthMiddlewareName("my-app", 7),
98+
);
99+
});
100+
});
101+
102+
describe("buildForwardAuthEnv", () => {
103+
const baseOptions = {
104+
oidc: {
105+
clientId: "client-123",
106+
clientSecret: "secret-xyz",
107+
issuer: "https://idp.example.com",
108+
},
109+
cookieSecret: "cookie-secret-value",
110+
authDomain: "auth.acme.com",
111+
baseDomain: ".acme.com",
112+
authDomainHttps: true,
113+
};
114+
115+
test("emits the required oauth2-proxy OIDC env vars", () => {
116+
const env = buildForwardAuthEnv(baseOptions);
117+
expect(env).toContain("OAUTH2_PROXY_PROVIDER=oidc");
118+
expect(env).toContain(
119+
"OAUTH2_PROXY_OIDC_ISSUER_URL=https://idp.example.com",
120+
);
121+
expect(env).toContain("OAUTH2_PROXY_CLIENT_ID=client-123");
122+
expect(env).toContain("OAUTH2_PROXY_CLIENT_SECRET=secret-xyz");
123+
expect(env).toContain("OAUTH2_PROXY_COOKIE_SECRET=cookie-secret-value");
124+
expect(env).toContain("OAUTH2_PROXY_REVERSE_PROXY=true");
125+
expect(env).toContain("OAUTH2_PROXY_HTTP_ADDRESS=0.0.0.0:4180");
126+
});
127+
128+
test("uses the central auth domain for the single fixed callback", () => {
129+
const env = buildForwardAuthEnv(baseOptions);
130+
expect(env).toContain(
131+
"OAUTH2_PROXY_REDIRECT_URL=https://auth.acme.com/oauth2/callback",
132+
);
133+
});
134+
135+
test("shares cookie + whitelist on the base domain (no per-app redeploy)", () => {
136+
const env = buildForwardAuthEnv(baseOptions);
137+
expect(env).toContain("OAUTH2_PROXY_COOKIE_DOMAINS=.acme.com");
138+
expect(env).toContain("OAUTH2_PROXY_WHITELIST_DOMAINS=.acme.com");
139+
});
140+
141+
test("matches cookie Secure flag and callback scheme to https setting", () => {
142+
const https = buildForwardAuthEnv(baseOptions);
143+
expect(https).toContain("OAUTH2_PROXY_COOKIE_SECURE=true");
144+
145+
const http = buildForwardAuthEnv({
146+
...baseOptions,
147+
authDomainHttps: false,
148+
});
149+
expect(http).toContain("OAUTH2_PROXY_COOKIE_SECURE=false");
150+
expect(http).toContain(
151+
"OAUTH2_PROXY_REDIRECT_URL=http://auth.acme.com/oauth2/callback",
152+
);
153+
});
154+
155+
test("allows unverified emails so OIDC providers don't 500 the callback", () => {
156+
const env = buildForwardAuthEnv(baseOptions);
157+
expect(env).toContain(
158+
"OAUTH2_PROXY_INSECURE_OIDC_ALLOW_UNVERIFIED_EMAIL=true",
159+
);
160+
});
161+
162+
test("defaults to any authenticated user and standard scopes", () => {
163+
const env = buildForwardAuthEnv(baseOptions);
164+
expect(env).toContain("OAUTH2_PROXY_EMAIL_DOMAINS=*");
165+
expect(env).toContain("OAUTH2_PROXY_SCOPE=openid email profile");
166+
});
167+
168+
test("honors custom scopes and email domains", () => {
169+
const env = buildForwardAuthEnv({
170+
...baseOptions,
171+
oidc: { ...baseOptions.oidc, scopes: ["openid", "groups"] },
172+
emailDomains: ["acme.com", "corp.com"],
173+
});
174+
expect(env).toContain("OAUTH2_PROXY_SCOPE=openid groups");
175+
expect(env).toContain("OAUTH2_PROXY_EMAIL_DOMAINS=acme.com,corp.com");
176+
});
177+
178+
test("sets skip-discovery flag only when requested", () => {
179+
const withoutSkip = buildForwardAuthEnv(baseOptions);
180+
expect(withoutSkip).not.toContain("OAUTH2_PROXY_SKIP_OIDC_DISCOVERY=true");
181+
182+
const withSkip = buildForwardAuthEnv({
183+
...baseOptions,
184+
oidc: { ...baseOptions.oidc, skipDiscovery: true },
185+
});
186+
expect(withSkip).toContain("OAUTH2_PROXY_SKIP_OIDC_DISCOVERY=true");
187+
});
188+
});
189+
190+
describe("deriveBaseDomain", () => {
191+
test("strips the auth subdomain to the shared base", () => {
192+
expect(deriveBaseDomain("auth.acme.com")).toBe(".acme.com");
193+
expect(deriveBaseDomain("sso.apps.acme.com")).toBe(".apps.acme.com");
194+
});
195+
196+
test("keeps a two-label apex as the base", () => {
197+
expect(deriveBaseDomain("acme.com")).toBe(".acme.com");
198+
});
199+
});
200+
201+
describe("forwardAuthCallbackUrl", () => {
202+
test("builds the single IdP callback per scheme", () => {
203+
expect(forwardAuthCallbackUrl("auth.acme.com", true)).toBe(
204+
"https://auth.acme.com/oauth2/callback",
205+
);
206+
expect(forwardAuthCallbackUrl("auth.acme.com", false)).toBe(
207+
"http://auth.acme.com/oauth2/callback",
208+
);
209+
});
210+
});
211+
212+
describe("deriveCookieSecret", () => {
213+
beforeAll(() => {
214+
process.env.BETTER_AUTH_SECRET = "test-root-secret";
215+
});
216+
217+
test("is deterministic for the same salt (survives service updates)", () => {
218+
expect(deriveCookieSecret(".acme.com")).toBe(
219+
deriveCookieSecret(".acme.com"),
220+
);
221+
});
222+
223+
test("differs per salt", () => {
224+
expect(deriveCookieSecret(".acme.com")).not.toBe(
225+
deriveCookieSecret(".other.com"),
226+
);
227+
});
228+
229+
test("produces a 32-byte base64 secret (oauth2-proxy requirement)", () => {
230+
const secret = deriveCookieSecret(".acme.com");
231+
expect(Buffer.from(secret, "base64")).toHaveLength(32);
232+
});
233+
});

‎apps/dokploy/__test__/traefik/traefik.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ const baseDomain: Domain = {
148148
internalPath: "/",
149149
stripPath: false,
150150
middlewares: null,
151+
forwardAuthProviderId: null,
151152
};
152153

153154
const baseRedirect: Redirect = {

0 commit comments

Comments
 (0)