@leitwerk-dev/server 0.4.2 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{app-CtlotzYm.d.ts → app-DZUsf76a.d.ts} +30 -1
- package/dist/app-MELYT4QS.js +8 -0
- package/dist/auth/api-token-routes.d.ts.map +1 -1
- package/dist/auth/api-token-service.d.ts +2 -1
- package/dist/auth/api-token-service.d.ts.map +1 -1
- package/dist/auth/auth-client.d.ts +2 -4
- package/dist/auth/auth-client.d.ts.map +1 -1
- package/dist/auth/auth-config.d.ts +2 -1
- package/dist/auth/auth-config.d.ts.map +1 -1
- package/dist/auth/auth-service.d.ts +3 -1
- package/dist/auth/auth-service.d.ts.map +1 -1
- package/dist/auth/auth-test-helpers.d.ts +17 -0
- package/dist/auth/auth-test-helpers.d.ts.map +1 -1
- package/dist/auth/oidc-client.d.ts +2 -0
- package/dist/auth/oidc-client.d.ts.map +1 -1
- package/dist/auth/oidc-evidence.d.ts +18 -0
- package/dist/auth/oidc-evidence.d.ts.map +1 -0
- package/dist/auth/routes.d.ts.map +1 -1
- package/dist/{chunk-YLWY4UTT.js → chunk-2OC3SK6F.js} +115 -5
- package/dist/chunk-2OC3SK6F.js.map +1 -0
- package/dist/{chunk-OUA5EZS2.js → chunk-SKQGTUDA.js} +108 -61
- package/dist/chunk-SKQGTUDA.js.map +1 -0
- package/dist/config/config-loader.d.ts.map +1 -1
- package/dist/config/config-types.d.ts +17 -0
- package/dist/config/config-types.d.ts.map +1 -1
- package/dist/db/api-token-repo.d.ts +3 -0
- package/dist/db/api-token-repo.d.ts.map +1 -1
- package/dist/db/auth-repo.d.ts +5 -0
- package/dist/db/auth-repo.d.ts.map +1 -1
- package/dist/db/database.d.ts.map +1 -1
- package/dist/db/schema.d.ts +32 -0
- package/dist/db/schema.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/main.js +3 -3
- package/dist/testing.js +1 -1
- package/package.json +12 -12
- package/src/auth/api-token-routes.ts +5 -2
- package/src/auth/api-token-service.ts +16 -4
- package/src/auth/auth-client.ts +2 -2
- package/src/auth/auth-config.ts +18 -19
- package/src/auth/auth-service.ts +56 -22
- package/src/auth/auth-test-helpers.ts +71 -0
- package/src/auth/oidc-client.ts +30 -12
- package/src/auth/oidc-evidence.ts +113 -0
- package/src/auth/routes.ts +22 -6
- package/src/config/config-loader.ts +34 -0
- package/src/config/config-types.ts +18 -0
- package/src/db/api-token-repo.ts +6 -1
- package/src/db/auth-repo.ts +7 -0
- package/src/db/database.ts +14 -0
- package/src/db/schema.ts +2 -0
- package/dist/app-KR3N2JAE.js +0 -8
- package/dist/chunk-OUA5EZS2.js.map +0 -1
- package/dist/chunk-YLWY4UTT.js.map +0 -1
- /package/dist/{app-KR3N2JAE.js.map → app-MELYT4QS.js.map} +0 -0
|
@@ -4,6 +4,61 @@ import type {
|
|
|
4
4
|
AuthOidcProviderConfig,
|
|
5
5
|
AuthSessionConfig,
|
|
6
6
|
} from "../config/config-types.js";
|
|
7
|
+
import { createAllRepos } from "../db/repositories.js";
|
|
8
|
+
import { createOwnedInMemoryDatabase } from "../test-helpers/owned-test-deps.js";
|
|
9
|
+
import { createAuthService } from "./auth-service.js";
|
|
10
|
+
import type { OidcClient, OidcTokensAndClaims } from "./oidc-client.js";
|
|
11
|
+
import type { OidcEvidence } from "./oidc-evidence.js";
|
|
12
|
+
|
|
13
|
+
/** @internal */
|
|
14
|
+
export function createTestOidcClient(options: {
|
|
15
|
+
state?: string;
|
|
16
|
+
claims?: Record<string, unknown>;
|
|
17
|
+
result?: OidcTokensAndClaims;
|
|
18
|
+
startError?: Error;
|
|
19
|
+
callbackError?: Error;
|
|
20
|
+
}): OidcClient {
|
|
21
|
+
return {
|
|
22
|
+
async createAuthorizationRequest(provider) {
|
|
23
|
+
if (options.startError) throw options.startError;
|
|
24
|
+
const state = options.state ?? "state";
|
|
25
|
+
return {
|
|
26
|
+
provider,
|
|
27
|
+
state,
|
|
28
|
+
pkceVerifier: `verifier-${state}`,
|
|
29
|
+
redirectUrl: "https://identity.example.test/oauth/authorize",
|
|
30
|
+
};
|
|
31
|
+
},
|
|
32
|
+
async exchangeCallback() {
|
|
33
|
+
if (options.callbackError) throw options.callbackError;
|
|
34
|
+
if (options.result) return options.result;
|
|
35
|
+
if (!options.claims) throw new Error("not used");
|
|
36
|
+
return { claims: options.claims, idTokenClaims: options.claims, userInfoClaims: null };
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** @internal */
|
|
42
|
+
export function createTestOidcLogin(
|
|
43
|
+
config: ReturnType<typeof testAuthConfig>,
|
|
44
|
+
options: Parameters<typeof createTestOidcClient>[0],
|
|
45
|
+
) {
|
|
46
|
+
const repos = createAllRepos(createOwnedInMemoryDatabase());
|
|
47
|
+
const auth = createAuthService({ config, repos, oidcClient: createTestOidcClient(options) });
|
|
48
|
+
return {
|
|
49
|
+
auth,
|
|
50
|
+
repos,
|
|
51
|
+
async completeLogin() {
|
|
52
|
+
const flow = await auth.startLogin();
|
|
53
|
+
const callbackUrl = new URL("/auth/callback", config.server.base_url);
|
|
54
|
+
callbackUrl.search = new URLSearchParams({
|
|
55
|
+
code: "c",
|
|
56
|
+
state: options.state ?? "state",
|
|
57
|
+
}).toString();
|
|
58
|
+
return auth.completeLogin({ loginCookieValue: flow.cookieValue, callbackUrl });
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
7
62
|
|
|
8
63
|
export function testAuthConfig(overrides?: {
|
|
9
64
|
cookieName?: string;
|
|
@@ -37,6 +92,22 @@ export function testAuthConfig(overrides?: {
|
|
|
37
92
|
return config;
|
|
38
93
|
}
|
|
39
94
|
|
|
95
|
+
export function testOidcEvidence(
|
|
96
|
+
identity = "alice",
|
|
97
|
+
claims: Record<string, unknown> = {},
|
|
98
|
+
): OidcEvidence {
|
|
99
|
+
return {
|
|
100
|
+
providerId: "identity",
|
|
101
|
+
issuer: "https://identity.example.test",
|
|
102
|
+
identityClaim: "preferred_username",
|
|
103
|
+
subject: `subject-${identity}`,
|
|
104
|
+
loginAt: "2026-01-01T00:00:00.000Z",
|
|
105
|
+
idTokenClaims: { sub: `subject-${identity}`, preferred_username: identity, ...claims },
|
|
106
|
+
userInfoClaims: null,
|
|
107
|
+
facts: { identity, email: null, emailVerified: false, groups: [] },
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
40
111
|
export function setCookieValues(headers: string | string[] | undefined): string[] {
|
|
41
112
|
return Array.isArray(headers) ? headers : headers ? [headers] : [];
|
|
42
113
|
}
|
package/src/auth/oidc-client.ts
CHANGED
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
allowInsecureRequests,
|
|
3
3
|
authorizationCodeGrant,
|
|
4
4
|
buildAuthorizationUrl,
|
|
5
|
+
ClientError,
|
|
5
6
|
ClientSecretPost,
|
|
6
7
|
type Configuration,
|
|
7
8
|
calculatePKCECodeChallenge,
|
|
@@ -9,7 +10,6 @@ import {
|
|
|
9
10
|
fetchUserInfo,
|
|
10
11
|
randomPKCECodeVerifier,
|
|
11
12
|
randomState,
|
|
12
|
-
skipSubjectCheck,
|
|
13
13
|
} from "openid-client";
|
|
14
14
|
import { isLoopbackHttpUrl } from "../config/url-policy.js";
|
|
15
15
|
import type { ResolvedOidcProvider } from "./auth-config.js";
|
|
@@ -23,6 +23,8 @@ export interface OidcAuthorizationRequest {
|
|
|
23
23
|
|
|
24
24
|
export interface OidcTokensAndClaims {
|
|
25
25
|
claims: Record<string, unknown>;
|
|
26
|
+
idTokenClaims?: Record<string, unknown>;
|
|
27
|
+
userInfoClaims?: Record<string, unknown> | null;
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
export interface OidcClient {
|
|
@@ -85,21 +87,37 @@ export function createOpenIdClient(): OidcClient {
|
|
|
85
87
|
{ redirect_uri: input.provider.redirect_uri },
|
|
86
88
|
);
|
|
87
89
|
const idClaims = tokens.claims() as Record<string, unknown> | undefined;
|
|
90
|
+
if (!idClaims || typeof idClaims.sub !== "string" || !idClaims.sub.trim()) {
|
|
91
|
+
throw new Error("OIDC ID token requires a valid subject");
|
|
92
|
+
}
|
|
93
|
+
let userInfo: Record<string, unknown> | null = null;
|
|
88
94
|
if (tokens.access_token) {
|
|
89
|
-
|
|
95
|
+
// Fetch without suppressing subject mismatch errors as optional network failures.
|
|
90
96
|
try {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
97
|
+
userInfo = (await fetchUserInfo(config, tokens.access_token, idClaims.sub)) as Record<
|
|
98
|
+
string,
|
|
99
|
+
unknown
|
|
100
|
+
>;
|
|
101
|
+
} catch (error) {
|
|
102
|
+
// Only availability failures are optional. Invalid responses fail closed.
|
|
103
|
+
if (
|
|
104
|
+
!(error instanceof TypeError) &&
|
|
105
|
+
!(
|
|
106
|
+
error instanceof ClientError &&
|
|
107
|
+
(error.code === "OAUTH_TIMEOUT" ||
|
|
108
|
+
(error.cause instanceof Response && error.cause.status >= 500))
|
|
109
|
+
)
|
|
110
|
+
)
|
|
111
|
+
throw new Error("OIDC UserInfo validation failed");
|
|
99
112
|
}
|
|
113
|
+
if (userInfo && userInfo.sub !== idClaims.sub)
|
|
114
|
+
throw new Error("OIDC UserInfo subject mismatch");
|
|
100
115
|
}
|
|
101
|
-
|
|
102
|
-
|
|
116
|
+
return {
|
|
117
|
+
claims: userInfo ? { ...idClaims, ...userInfo } : idClaims,
|
|
118
|
+
idTokenClaims: idClaims,
|
|
119
|
+
userInfoClaims: userInfo,
|
|
120
|
+
};
|
|
103
121
|
},
|
|
104
122
|
};
|
|
105
123
|
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { Actor } from "@leitwerk-dev/domain";
|
|
2
|
+
import type { UserInformationResponseBody } from "@leitwerk-dev/protocol/http-contracts";
|
|
3
|
+
import type { OidcAccessPolicy } from "../config/config-types.js";
|
|
4
|
+
import type { ResolvedOidcProvider } from "./auth-config.js";
|
|
5
|
+
|
|
6
|
+
/** Session/token evidence, never attached to an Actor. @internal */
|
|
7
|
+
export interface OidcEvidence extends UserInformationResponseBody {
|
|
8
|
+
/** @internal */
|
|
9
|
+
identityClaim: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Exclude credential-shaped claims even when returned by a custom provider. @internal */
|
|
13
|
+
export function safeClaims(claims: Record<string, unknown>): Record<string, unknown> {
|
|
14
|
+
return JSON.parse(
|
|
15
|
+
JSON.stringify(claims, (key, value) =>
|
|
16
|
+
/^(access_token|refresh_token|id_token|client_secret|authorization)$/i.test(key)
|
|
17
|
+
? undefined
|
|
18
|
+
: value,
|
|
19
|
+
),
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @internal */
|
|
24
|
+
export function mergedClaims(evidence: OidcEvidence): Record<string, unknown> {
|
|
25
|
+
return { ...evidence.idTokenClaims, ...evidence.userInfoClaims };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function validDomain(domain: string): boolean {
|
|
29
|
+
return (
|
|
30
|
+
domain.length <= 253 &&
|
|
31
|
+
domain.split(".").every((label) => /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/i.test(label))
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** @internal */
|
|
36
|
+
export function policyErrors(policy: OidcAccessPolicy, legacy: boolean): string[] {
|
|
37
|
+
const errors: string[] = [];
|
|
38
|
+
if (legacy) errors.push("cannot combine access with deprecated auth.allowlist");
|
|
39
|
+
const lists = [policy.users, policy.groups, policy.email_domains];
|
|
40
|
+
if (
|
|
41
|
+
policy.mode === "all" &&
|
|
42
|
+
(lists.some((list) => list !== undefined) ||
|
|
43
|
+
policy.match !== undefined ||
|
|
44
|
+
policy.groups_claim !== undefined)
|
|
45
|
+
)
|
|
46
|
+
errors.push("access mode all cannot include restrictions, match, or groups_claim");
|
|
47
|
+
if (policy.mode === "restricted" && !lists.some((list) => list && list.length > 0))
|
|
48
|
+
errors.push("restricted access requires at least one non-empty criterion");
|
|
49
|
+
if (
|
|
50
|
+
lists.some(
|
|
51
|
+
(list) => list !== undefined && (list.length === 0 || list.some((item) => !item.trim())),
|
|
52
|
+
)
|
|
53
|
+
)
|
|
54
|
+
errors.push("access criteria must be non-empty lists of non-empty strings");
|
|
55
|
+
if (policy.email_domains?.some((domain) => !validDomain(domain)))
|
|
56
|
+
errors.push("access email_domains must contain exact domain names");
|
|
57
|
+
return errors;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** @internal */
|
|
61
|
+
export function evidenceAllows(
|
|
62
|
+
actor: Actor,
|
|
63
|
+
provider: ResolvedOidcProvider,
|
|
64
|
+
evidence: OidcEvidence | null | undefined,
|
|
65
|
+
allowlist: Set<string>,
|
|
66
|
+
): boolean {
|
|
67
|
+
if (
|
|
68
|
+
!evidence ||
|
|
69
|
+
evidence.providerId !== provider.id ||
|
|
70
|
+
evidence.issuer !== provider.issuer ||
|
|
71
|
+
evidence.identityClaim !== provider.identity_claim ||
|
|
72
|
+
typeof evidence.subject !== "string" ||
|
|
73
|
+
!evidence.subject.trim() ||
|
|
74
|
+
evidence.idTokenClaims.sub !== evidence.subject ||
|
|
75
|
+
(evidence.userInfoClaims !== null && evidence.userInfoClaims.sub !== evidence.subject)
|
|
76
|
+
)
|
|
77
|
+
return false;
|
|
78
|
+
const claims = mergedClaims(evidence);
|
|
79
|
+
const identity =
|
|
80
|
+
typeof claims[provider.identity_claim] === "string"
|
|
81
|
+
? (claims[provider.identity_claim] as string).trim()
|
|
82
|
+
: "";
|
|
83
|
+
if (!identity || actor.id !== `${provider.id}:${identity}`) return false;
|
|
84
|
+
const policy = provider.access;
|
|
85
|
+
if (!policy) return allowlist.has(identity);
|
|
86
|
+
if (policy.mode === "all") return true;
|
|
87
|
+
const matches: boolean[] = [];
|
|
88
|
+
if (policy.users) matches.push(policy.users.includes(identity));
|
|
89
|
+
if (policy.groups) {
|
|
90
|
+
const groups = claims[policy.groups_claim ?? "groups"];
|
|
91
|
+
matches.push(
|
|
92
|
+
Array.isArray(groups) &&
|
|
93
|
+
groups.every((group) => typeof group === "string") &&
|
|
94
|
+
policy.groups.some((group) => groups.includes(group)),
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
if (policy.email_domains) {
|
|
98
|
+
const email = claims.email;
|
|
99
|
+
const domain =
|
|
100
|
+
typeof email === "string" && /^[^\s@]+@[^\s@]+$/.test(email)
|
|
101
|
+
? email.split("@")[1]?.toLowerCase()
|
|
102
|
+
: undefined;
|
|
103
|
+
matches.push(
|
|
104
|
+
claims.email_verified === true &&
|
|
105
|
+
domain !== undefined &&
|
|
106
|
+
validDomain(domain) &&
|
|
107
|
+
policy.email_domains.some((allowed) => allowed.toLowerCase() === domain),
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
return (
|
|
111
|
+
matches.length > 0 && (policy.match === "any" ? matches.some(Boolean) : matches.every(Boolean))
|
|
112
|
+
);
|
|
113
|
+
}
|
package/src/auth/routes.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { AuthMeResponseBody } from "@leitwerk-dev/protocol/http-contracts";
|
|
|
2
2
|
import type { FastifyInstance } from "fastify";
|
|
3
3
|
import { registerApiTokenRoutes } from "./api-token-routes.js";
|
|
4
4
|
import type { AuthService } from "./auth-service.js";
|
|
5
|
-
import { authenticateRequest } from "./fastify-auth.js";
|
|
5
|
+
import { authenticateRequest, hasAuthorization } from "./fastify-auth.js";
|
|
6
6
|
|
|
7
7
|
function cookieMaxAgeSeconds(expiresAt: string): number {
|
|
8
8
|
return Math.max(0, Math.floor((new Date(expiresAt).getTime() - Date.now()) / 1000));
|
|
@@ -20,6 +20,14 @@ function cookieOptions(input: { secure: boolean; maxAge: number }) {
|
|
|
20
20
|
|
|
21
21
|
export function registerAuthRoutes(app: FastifyInstance, auth: AuthService): void {
|
|
22
22
|
registerApiTokenRoutes(app, auth);
|
|
23
|
+
app.get("/api/auth/user-information", async (request, reply) => {
|
|
24
|
+
reply.header("Cache-Control", "no-store");
|
|
25
|
+
if (hasAuthorization(request))
|
|
26
|
+
return reply.code(401).send({ error: "Session access required" });
|
|
27
|
+
const information = auth.userInformation(request.cookies?.[auth.config.sessionCookieName]);
|
|
28
|
+
if (!information) return reply.code(401).send({ error: "OIDC session required" });
|
|
29
|
+
return reply.send(information);
|
|
30
|
+
});
|
|
23
31
|
app.get("/api/auth/me", async (request, reply) => {
|
|
24
32
|
const actor = authenticateRequest(auth, request);
|
|
25
33
|
if (!actor) {
|
|
@@ -27,7 +35,15 @@ export function registerAuthRoutes(app: FastifyInstance, auth: AuthService): voi
|
|
|
27
35
|
.code(401)
|
|
28
36
|
.send({ authEnabled: auth.config.enabled, actor: null } satisfies AuthMeResponseBody);
|
|
29
37
|
}
|
|
30
|
-
return reply.send({
|
|
38
|
+
return reply.send({
|
|
39
|
+
authEnabled: auth.config.enabled,
|
|
40
|
+
actor,
|
|
41
|
+
userInformationAvailable:
|
|
42
|
+
!request.headers.authorization &&
|
|
43
|
+
auth.config.providers.some(
|
|
44
|
+
(provider) => provider.kind === "oidc" && provider.id === actor.provider,
|
|
45
|
+
),
|
|
46
|
+
} satisfies AuthMeResponseBody);
|
|
31
47
|
});
|
|
32
48
|
|
|
33
49
|
app.get("/auth/login", async (_request, reply) => {
|
|
@@ -45,8 +61,8 @@ export function registerAuthRoutes(app: FastifyInstance, auth: AuthService): voi
|
|
|
45
61
|
}),
|
|
46
62
|
);
|
|
47
63
|
return reply.redirect(login.redirectUrl);
|
|
48
|
-
} catch
|
|
49
|
-
app.log.warn(
|
|
64
|
+
} catch {
|
|
65
|
+
app.log.warn("Failed to start OIDC login");
|
|
50
66
|
return reply.code(503).send({ error: "Login is temporarily unavailable" });
|
|
51
67
|
}
|
|
52
68
|
});
|
|
@@ -74,8 +90,8 @@ export function registerAuthRoutes(app: FastifyInstance, auth: AuthService): voi
|
|
|
74
90
|
}),
|
|
75
91
|
);
|
|
76
92
|
return reply.redirect("/");
|
|
77
|
-
} catch
|
|
78
|
-
app.log.warn(
|
|
93
|
+
} catch {
|
|
94
|
+
app.log.warn("Failed to complete OIDC login");
|
|
79
95
|
reply.clearCookie(auth.config.loginFlowCookieName, { path: "/" });
|
|
80
96
|
return reply.code(403).send({ error: "Login failed" });
|
|
81
97
|
}
|
|
@@ -12,6 +12,7 @@ import { createDefu } from "defu";
|
|
|
12
12
|
import * as v from "valibot";
|
|
13
13
|
import { parse as parseYaml } from "yaml";
|
|
14
14
|
import { resolveApiTokenPolicy } from "../auth/api-token-policy.js";
|
|
15
|
+
import { policyErrors } from "../auth/oidc-evidence.js";
|
|
15
16
|
import { normalizeRepositoryLocator } from "../commit-message-policy.js";
|
|
16
17
|
import { resolveDockerRegistryCredentials } from "../docker-registry-credentials.js";
|
|
17
18
|
import { isValidStorageSize } from "../process-storage-size.js";
|
|
@@ -122,6 +123,20 @@ export function loadConfigFromFile(filePath: string): ConfigLoadResult | ConfigL
|
|
|
122
123
|
};
|
|
123
124
|
}
|
|
124
125
|
const config = applyConfigDefaults(parsedRecord.output);
|
|
126
|
+
for (const [index, provider] of (config.auth?.providers ?? []).entries()) {
|
|
127
|
+
if (provider.kind !== "oidc" || typeof provider.client_secret !== "string") continue;
|
|
128
|
+
provider.client_secret = provider.client_secret.replace(
|
|
129
|
+
/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g,
|
|
130
|
+
(_placeholder, variable: string) => {
|
|
131
|
+
const value = process.env[variable];
|
|
132
|
+
if (!value)
|
|
133
|
+
throw new Error(
|
|
134
|
+
`auth.providers[${index}].client_secret: environment variable ${variable} is missing or empty`,
|
|
135
|
+
);
|
|
136
|
+
return value;
|
|
137
|
+
},
|
|
138
|
+
);
|
|
139
|
+
}
|
|
125
140
|
const errors = validateResolvedConfig(config as unknown as Record<string, unknown>);
|
|
126
141
|
if (errors.length > 0) {
|
|
127
142
|
return { ok: false, error: `Config validation errors:\n${errors.join("\n")}` };
|
|
@@ -164,6 +179,16 @@ const authOidcProviderSchema = v.looseObject({
|
|
|
164
179
|
redirect_uri: v.optional(authNonEmptyString),
|
|
165
180
|
scopes: v.optional(v.pipe(stringArraySchema, v.nonEmpty())),
|
|
166
181
|
identity_claim: v.optional(authNonEmptyString),
|
|
182
|
+
access: v.optional(
|
|
183
|
+
v.strictObject({
|
|
184
|
+
mode: v.picklist(["all", "restricted"]),
|
|
185
|
+
match: v.optional(v.picklist(["all", "any"])),
|
|
186
|
+
users: v.optional(v.array(authNonEmptyString)),
|
|
187
|
+
groups: v.optional(v.array(authNonEmptyString)),
|
|
188
|
+
groups_claim: v.optional(authNonEmptyString),
|
|
189
|
+
email_domains: v.optional(v.array(authNonEmptyString)),
|
|
190
|
+
}),
|
|
191
|
+
),
|
|
167
192
|
});
|
|
168
193
|
|
|
169
194
|
const authGithubProviderSchema = v.looseObject({
|
|
@@ -822,6 +847,15 @@ function collectAuthConfigErrors(config: LeitwerkConfig): string[] {
|
|
|
822
847
|
} catch (error) {
|
|
823
848
|
errors.push(error instanceof Error ? error.message : "Invalid auth.api_tokens configuration");
|
|
824
849
|
}
|
|
850
|
+
for (const [index, provider] of (auth?.providers ?? []).entries()) {
|
|
851
|
+
if (provider.kind === "oidc" && provider.access) {
|
|
852
|
+
errors.push(
|
|
853
|
+
...policyErrors(provider.access, auth?.allowlist !== undefined).map(
|
|
854
|
+
(message) => `auth.providers[${index}].access: ${message}`,
|
|
855
|
+
),
|
|
856
|
+
);
|
|
857
|
+
}
|
|
858
|
+
}
|
|
825
859
|
if (!auth || auth.enabled !== true) {
|
|
826
860
|
return errors;
|
|
827
861
|
}
|
|
@@ -45,6 +45,22 @@ export interface AuthSessionConfig {
|
|
|
45
45
|
ttl?: string;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
/** @internal */
|
|
49
|
+
export interface OidcAccessPolicy {
|
|
50
|
+
/** @internal */
|
|
51
|
+
mode: "all" | "restricted";
|
|
52
|
+
/** Defaults to all. @internal */
|
|
53
|
+
match?: "all" | "any";
|
|
54
|
+
/** @internal */
|
|
55
|
+
users?: string[];
|
|
56
|
+
/** @internal */
|
|
57
|
+
groups?: string[];
|
|
58
|
+
/** @internal */
|
|
59
|
+
groups_claim?: string;
|
|
60
|
+
/** @internal */
|
|
61
|
+
email_domains?: string[];
|
|
62
|
+
}
|
|
63
|
+
|
|
48
64
|
/** @internal */
|
|
49
65
|
export interface AuthOidcProviderConfig {
|
|
50
66
|
/** @internal */
|
|
@@ -63,6 +79,8 @@ export interface AuthOidcProviderConfig {
|
|
|
63
79
|
scopes?: string[];
|
|
64
80
|
/** @internal */
|
|
65
81
|
identity_claim?: string;
|
|
82
|
+
/** @internal */
|
|
83
|
+
access?: OidcAccessPolicy;
|
|
66
84
|
}
|
|
67
85
|
|
|
68
86
|
/** @internal */
|
package/src/db/api-token-repo.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Actor } from "@leitwerk-dev/domain";
|
|
2
2
|
import type { ApiTokenMetadata } from "@leitwerk-dev/protocol/http-contracts";
|
|
3
3
|
import { and, desc, eq, isNull } from "drizzle-orm";
|
|
4
|
+
import type { OidcEvidence } from "../auth/oidc-evidence.js";
|
|
4
5
|
import type { LeitwerkDb } from "./database.js";
|
|
5
6
|
import { apiTokens } from "./schema.js";
|
|
6
7
|
|
|
@@ -34,6 +35,8 @@ export interface ApiTokenOwner {
|
|
|
34
35
|
actor: Actor;
|
|
35
36
|
/** @internal */
|
|
36
37
|
providerBinding: TokenProviderBinding | null;
|
|
38
|
+
/** @internal */
|
|
39
|
+
oidcEvidence?: OidcEvidence | null;
|
|
37
40
|
}
|
|
38
41
|
/** @internal */
|
|
39
42
|
export interface ApiTokenRecord extends ApiTokenMetadata {
|
|
@@ -43,10 +46,11 @@ export interface ApiTokenRecord extends ApiTokenMetadata {
|
|
|
43
46
|
owner: ApiTokenOwner;
|
|
44
47
|
}
|
|
45
48
|
function record(row: typeof apiTokens.$inferSelect): ApiTokenRecord {
|
|
46
|
-
const { ownerKind, ownerId, actorJson, providerBindingJson, ...fields } = row;
|
|
49
|
+
const { ownerKind, ownerId, actorJson, providerBindingJson, oidcEvidenceJson, ...fields } = row;
|
|
47
50
|
return {
|
|
48
51
|
...fields,
|
|
49
52
|
owner: {
|
|
53
|
+
oidcEvidence: oidcEvidenceJson ? JSON.parse(oidcEvidenceJson) : null,
|
|
50
54
|
kind: ownerKind,
|
|
51
55
|
id: ownerId,
|
|
52
56
|
actor: JSON.parse(actorJson),
|
|
@@ -68,6 +72,7 @@ export function createApiTokenRepo(db: LeitwerkDb) {
|
|
|
68
72
|
ownerKind: owner.kind,
|
|
69
73
|
ownerId: owner.id,
|
|
70
74
|
actorJson: JSON.stringify(owner.actor),
|
|
75
|
+
oidcEvidenceJson: owner.oidcEvidence ? JSON.stringify(owner.oidcEvidence) : null,
|
|
71
76
|
providerBindingJson: owner.providerBinding ? JSON.stringify(owner.providerBinding) : null,
|
|
72
77
|
})
|
|
73
78
|
.run();
|
package/src/db/auth-repo.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Actor } from "@leitwerk-dev/domain";
|
|
2
2
|
import { and, eq, gt, lt } from "drizzle-orm";
|
|
3
|
+
import type { OidcEvidence } from "../auth/oidc-evidence.js";
|
|
3
4
|
import type { LeitwerkDb } from "./database.js";
|
|
4
5
|
import { now } from "./repo-helpers.js";
|
|
5
6
|
import * as s from "./schema.js";
|
|
@@ -11,6 +12,8 @@ export interface AuthSessionRecord {
|
|
|
11
12
|
/** @internal */
|
|
12
13
|
actor: Actor;
|
|
13
14
|
/** @internal */
|
|
15
|
+
oidcEvidence: OidcEvidence | null;
|
|
16
|
+
/** @internal */
|
|
14
17
|
createdAt: string;
|
|
15
18
|
/** @internal */
|
|
16
19
|
expiresAt: string;
|
|
@@ -35,6 +38,7 @@ export interface AuthLoginFlowRecord {
|
|
|
35
38
|
function sessionRowToRecord(row: typeof s.authSessions.$inferSelect): AuthSessionRecord {
|
|
36
39
|
return {
|
|
37
40
|
idHash: row.idHash,
|
|
41
|
+
oidcEvidence: row.oidcEvidenceJson ? JSON.parse(row.oidcEvidenceJson) : null,
|
|
38
42
|
actor: {
|
|
39
43
|
id: row.actorId,
|
|
40
44
|
kind: "user",
|
|
@@ -56,12 +60,15 @@ export function createAuthSessionRepo(db: LeitwerkDb) {
|
|
|
56
60
|
/** @internal */
|
|
57
61
|
actor: Actor;
|
|
58
62
|
/** @internal */
|
|
63
|
+
oidcEvidence?: OidcEvidence | null;
|
|
64
|
+
/** @internal */
|
|
59
65
|
expiresAt: string;
|
|
60
66
|
}): AuthSessionRecord {
|
|
61
67
|
const ts = now();
|
|
62
68
|
const values = {
|
|
63
69
|
idHash: input.idHash,
|
|
64
70
|
actorId: input.actor.id,
|
|
71
|
+
oidcEvidenceJson: input.oidcEvidence ? JSON.stringify(input.oidcEvidence) : null,
|
|
65
72
|
actorProvider: input.actor.provider,
|
|
66
73
|
displayName: input.actor.displayName ?? null,
|
|
67
74
|
createdAt: ts,
|
package/src/db/database.ts
CHANGED
|
@@ -466,6 +466,20 @@ const KNOWN_MIGRATIONS: readonly KnownMigration[] = [
|
|
|
466
466
|
existingTableSql(sqlite, "auth_sessions") !== null &&
|
|
467
467
|
existingTableSql(sqlite, "api_tokens") === null,
|
|
468
468
|
),
|
|
469
|
+
{
|
|
470
|
+
id: "20260909_add_oidc_evidence",
|
|
471
|
+
tableNames: ["auth_sessions", "api_tokens"],
|
|
472
|
+
matches: (sqlite) =>
|
|
473
|
+
existingTableSql(sqlite, "auth_sessions") !== null &&
|
|
474
|
+
(!tableHasColumn(sqlite, "auth_sessions", "oidc_evidence_json") ||
|
|
475
|
+
!tableHasColumn(sqlite, "api_tokens", "oidc_evidence_json")),
|
|
476
|
+
apply(sqlite) {
|
|
477
|
+
for (const table of ["auth_sessions", "api_tokens"]) {
|
|
478
|
+
if (!tableHasColumn(sqlite, table, "oidc_evidence_json"))
|
|
479
|
+
sqlite.exec(`ALTER TABLE ${table} ADD COLUMN oidc_evidence_json text`);
|
|
480
|
+
}
|
|
481
|
+
},
|
|
482
|
+
},
|
|
469
483
|
{
|
|
470
484
|
id: "20260901_add_session_transfers",
|
|
471
485
|
tableNames: ["session_transfer_grants", "session_transfer_attempts"],
|
package/src/db/schema.ts
CHANGED
|
@@ -574,6 +574,7 @@ export const authSessions = sqliteTable(
|
|
|
574
574
|
displayName: text("display_name"),
|
|
575
575
|
createdAt: text("created_at").notNull(),
|
|
576
576
|
expiresAt: text("expires_at").notNull(),
|
|
577
|
+
oidcEvidenceJson: text("oidc_evidence_json"),
|
|
577
578
|
},
|
|
578
579
|
(t) => [
|
|
579
580
|
index("idx_auth_sessions_expires").on(t.expiresAt),
|
|
@@ -957,6 +958,7 @@ export const apiTokens = sqliteTable(
|
|
|
957
958
|
expiresAt: text("expires_at"),
|
|
958
959
|
revokedAt: text("revoked_at"),
|
|
959
960
|
lastUsedAt: text("last_used_at"),
|
|
961
|
+
oidcEvidenceJson: text("oidc_evidence_json"),
|
|
960
962
|
},
|
|
961
963
|
(t) => [
|
|
962
964
|
uniqueIndex("idx_api_tokens_secret_hash").on(t.secretHash),
|