@leitwerk-dev/server 0.1.6 → 0.1.7
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.d.ts.map +1 -1
- package/dist/app.js +29 -31
- package/dist/app.js.map +1 -1
- package/dist/auth/auth-client.d.ts +25 -0
- package/dist/auth/auth-client.d.ts.map +1 -0
- package/dist/auth/auth-client.js +19 -0
- package/dist/auth/auth-client.js.map +1 -0
- package/dist/auth/auth-config.d.ts +8 -2
- package/dist/auth/auth-config.d.ts.map +1 -1
- package/dist/auth/auth-config.js +17 -7
- package/dist/auth/auth-config.js.map +1 -1
- package/dist/auth/auth-service.d.ts +2 -0
- package/dist/auth/auth-service.d.ts.map +1 -1
- package/dist/auth/auth-service.js +9 -5
- package/dist/auth/auth-service.js.map +1 -1
- package/dist/auth/auth-test-helpers.js +2 -2
- package/dist/auth/auth-test-helpers.js.map +1 -1
- package/dist/auth/github-oauth-client.d.ts +20 -0
- package/dist/auth/github-oauth-client.d.ts.map +1 -0
- package/dist/auth/github-oauth-client.js +79 -0
- package/dist/auth/github-oauth-client.js.map +1 -0
- package/dist/auth/oidc-client.d.ts +4 -4
- package/dist/{chunk-G4PCN46L.js → chunk-GIWHTJSL.js} +18 -93
- package/dist/chunk-GIWHTJSL.js.map +1 -0
- package/dist/{chunk-KVHAK5UR.js → chunk-OO6VH4FX.js} +515 -400
- package/dist/chunk-OO6VH4FX.js.map +1 -0
- package/dist/config/config-loader.d.ts.map +1 -1
- package/dist/config/config-loader.js +13 -88
- package/dist/config/config-loader.js.map +1 -1
- package/dist/config/config-types.d.ts +13 -3
- package/dist/config/config-types.d.ts.map +1 -1
- package/dist/domain-logic/outcome-tools.d.ts +2 -2
- package/dist/domain-logic/outcome-tools.d.ts.map +1 -1
- package/dist/domain-logic/outcome-tools.js +1 -1
- package/dist/domain-logic/outcome-tools.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/integration-tool-registry.d.ts +23 -0
- package/dist/integration-tool-registry.d.ts.map +1 -0
- package/dist/integration-tool-registry.js +140 -0
- package/dist/integration-tool-registry.js.map +1 -0
- package/dist/process-ui-snapshot-presenter.d.ts.map +1 -1
- package/dist/process-ui-snapshot-presenter.js +0 -2
- package/dist/process-ui-snapshot-presenter.js.map +1 -1
- package/dist/process-watcher-registry.d.ts +3 -31
- package/dist/process-watcher-registry.d.ts.map +1 -1
- package/dist/process-watcher-registry.js +96 -97
- package/dist/process-watcher-registry.js.map +1 -1
- package/dist/routes/watchers.d.ts.map +1 -1
- package/dist/routes/watchers.js +14 -50
- package/dist/routes/watchers.js.map +1 -1
- package/dist/semantic-state-json.d.ts +1 -2
- package/dist/semantic-state-json.d.ts.map +1 -1
- package/dist/semantic-state-json.js +1 -2
- package/dist/semantic-state-json.js.map +1 -1
- package/dist/supervisor/ipc-handler.d.ts +4 -1
- package/dist/supervisor/ipc-handler.d.ts.map +1 -1
- package/dist/supervisor/ipc-handler.js +18 -0
- package/dist/supervisor/ipc-handler.js.map +1 -1
- package/dist/supervisor/worker-start-payload-builder.d.ts +3 -0
- package/dist/supervisor/worker-start-payload-builder.d.ts.map +1 -1
- package/dist/supervisor/worker-start-payload-builder.js +9 -0
- package/dist/supervisor/worker-start-payload-builder.js.map +1 -1
- package/dist/supervisor/worker-supervisor.d.ts +3 -1
- package/dist/supervisor/worker-supervisor.d.ts.map +1 -1
- package/dist/supervisor/worker-supervisor.js +6 -0
- package/dist/supervisor/worker-supervisor.js.map +1 -1
- package/dist/test-helpers/fake-worker-supervisor.d.ts.map +1 -1
- package/dist/test-helpers/fake-worker-supervisor.js +1 -0
- package/dist/test-helpers/fake-worker-supervisor.js.map +1 -1
- package/dist/test-helpers/ipc-handler-harness.d.ts +1 -0
- package/dist/test-helpers/ipc-handler-harness.d.ts.map +1 -1
- package/dist/test-helpers/ipc-handler-harness.js +4 -3
- package/dist/test-helpers/ipc-handler-harness.js.map +1 -1
- package/dist/test-helpers/process-fixtures.js +3 -3
- package/dist/test-helpers/process-fixtures.js.map +1 -1
- package/dist/{writes-irEq9SlA.d.ts → writes-DUwQe8MD.d.ts} +22 -4
- package/package.json +10 -10
- package/src/app.ts +34 -35
- package/src/auth/auth-client.ts +40 -0
- package/src/auth/auth-config.ts +34 -9
- package/src/auth/auth-service.ts +11 -5
- package/src/auth/auth-test-helpers.ts +2 -2
- package/src/auth/github-oauth-client.ts +111 -0
- package/src/auth/oidc-client.ts +5 -5
- package/src/config/config-loader.ts +18 -102
- package/src/config/config-types.ts +15 -3
- package/src/domain-logic/outcome-tools.ts +3 -3
- package/src/index.ts +1 -4
- package/src/integration-tool-registry.ts +186 -0
- package/src/process-ui-snapshot-presenter.ts +0 -2
- package/src/process-watcher-registry.ts +124 -167
- package/src/routes/watchers.ts +14 -50
- package/src/semantic-state-json.ts +0 -2
- package/src/supervisor/ipc-handler.ts +35 -0
- package/src/supervisor/worker-start-payload-builder.ts +19 -0
- package/src/supervisor/worker-supervisor.ts +13 -0
- package/src/test-helpers/fake-worker-supervisor.ts +1 -0
- package/src/test-helpers/ipc-handler-harness.ts +7 -3
- package/src/test-helpers/process-fixtures.ts +3 -3
- package/dist/chunk-G4PCN46L.js.map +0 -1
- package/dist/chunk-KVHAK5UR.js.map +0 -1
- package/dist/domain-logic/idempotency.d.ts +0 -3
- package/dist/domain-logic/idempotency.d.ts.map +0 -1
- package/dist/domain-logic/idempotency.js +0 -3
- package/dist/domain-logic/idempotency.js.map +0 -1
- package/dist/domain-logic/index.d.ts +0 -7
- package/dist/domain-logic/index.d.ts.map +0 -1
- package/dist/domain-logic/index.js +0 -6
- package/dist/domain-logic/index.js.map +0 -1
- package/dist/domain-logic/input-sequencer.d.ts +0 -16
- package/dist/domain-logic/input-sequencer.d.ts.map +0 -1
- package/dist/domain-logic/input-sequencer.js +0 -2
- package/dist/domain-logic/input-sequencer.js.map +0 -1
- package/dist/domain-logic/string-normalize.d.ts +0 -2
- package/dist/domain-logic/string-normalize.d.ts.map +0 -1
- package/dist/domain-logic/string-normalize.js +0 -2
- package/dist/domain-logic/string-normalize.js.map +0 -1
- package/dist/filesystem-process-watchers.d.ts +0 -22
- package/dist/filesystem-process-watchers.d.ts.map +0 -1
- package/dist/filesystem-process-watchers.js +0 -79
- package/dist/filesystem-process-watchers.js.map +0 -1
- package/dist/test-helpers/in-process-worker.d.ts +0 -2
- package/dist/test-helpers/in-process-worker.d.ts.map +0 -1
- package/dist/test-helpers/in-process-worker.js +0 -2
- package/dist/test-helpers/in-process-worker.js.map +0 -1
- package/dist/test-helpers/leaf-outcome-fixtures.d.ts +0 -37
- package/dist/test-helpers/leaf-outcome-fixtures.d.ts.map +0 -1
- package/dist/test-helpers/leaf-outcome-fixtures.js +0 -81
- package/dist/test-helpers/leaf-outcome-fixtures.js.map +0 -1
- package/src/domain-logic/idempotency.ts +0 -3
- package/src/domain-logic/index.ts +0 -31
- package/src/domain-logic/input-sequencer.ts +0 -18
- package/src/domain-logic/string-normalize.ts +0 -1
- package/src/filesystem-process-watchers.ts +0 -147
- package/src/test-helpers/in-process-worker.ts +0 -2
- package/src/test-helpers/leaf-outcome-fixtures.ts +0 -140
package/src/app.ts
CHANGED
|
@@ -35,12 +35,15 @@ import { createAllRepos, createCredentialCipherFromEnvironment } from "./db/repo
|
|
|
35
35
|
import { buildExtensionUiCatalog, type ExtensionUiCatalog } from "./extension-ui/catalog.js";
|
|
36
36
|
import { createExtensionHost, type ExtensionHost } from "./extensions/extension-host.js";
|
|
37
37
|
import { createExternalSourceService } from "./external-source-service.js";
|
|
38
|
-
import { createFilesystemProcessWatchersService } from "./filesystem-process-watchers.js";
|
|
39
38
|
import {
|
|
40
39
|
createFutureExecutionLifecycle,
|
|
41
40
|
type FutureExecutionLifecycle,
|
|
42
41
|
} from "./future-execution/index.js";
|
|
43
42
|
import { startFutureExecutionScheduler } from "./future-execution-scheduler.js";
|
|
43
|
+
import {
|
|
44
|
+
createIntegrationToolRequestService,
|
|
45
|
+
IntegrationToolRegistry,
|
|
46
|
+
} from "./integration-tool-registry.js";
|
|
44
47
|
import { createLauncherModelConfigService } from "./launcher-model-config-service.js";
|
|
45
48
|
import { createLauncherRecentValuesService } from "./launcher-recent-values-service.js";
|
|
46
49
|
import {
|
|
@@ -84,10 +87,7 @@ import {
|
|
|
84
87
|
TITLE_SYSTEM_PROMPT,
|
|
85
88
|
} from "./process-title-generator.js";
|
|
86
89
|
import { buildProcessUiRegistry } from "./process-ui-registry.js";
|
|
87
|
-
import {
|
|
88
|
-
buildProcessWatcherRegistry,
|
|
89
|
-
validateConfiguredProcessWatchersAgainstCatalog,
|
|
90
|
-
} from "./process-watcher-registry.js";
|
|
90
|
+
import { buildProcessWatcherRegistry } from "./process-watcher-registry.js";
|
|
91
91
|
import {
|
|
92
92
|
createProjectMutationService,
|
|
93
93
|
type ProjectMutationService,
|
|
@@ -368,6 +368,7 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
368
368
|
const processOperations = createProcessOperationCoordinator();
|
|
369
369
|
const processGraphs = extensionCatalog.processes;
|
|
370
370
|
const processActionRegistry = buildProcessActionRegistry(extensionCatalog);
|
|
371
|
+
const integrationTools = new IntegrationToolRegistry();
|
|
371
372
|
const processUiRegistry = buildProcessUiRegistry(extensionCatalog);
|
|
372
373
|
const processModelPolicy = createServerProcessModelPolicy({
|
|
373
374
|
config,
|
|
@@ -409,17 +410,10 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
409
410
|
commitMessages: config.commit_messages,
|
|
410
411
|
getModelProfilesForProcess,
|
|
411
412
|
});
|
|
412
|
-
const processWatcherConfigErrors = validateConfiguredProcessWatchersAgainstCatalog({
|
|
413
|
-
config,
|
|
414
|
-
catalog: extensionCatalog,
|
|
415
|
-
processModelPolicy,
|
|
416
|
-
});
|
|
417
|
-
if (processWatcherConfigErrors.length > 0) {
|
|
418
|
-
throw new Error(`Invalid process watcher config:\n${processWatcherConfigErrors.join("\n")}`);
|
|
419
|
-
}
|
|
420
413
|
const processWatcherService = buildProcessWatcherRegistry(extensionCatalog, config, {
|
|
421
414
|
modelProfiles: launcherModelProfiles,
|
|
422
415
|
getModelProfilesForProcess,
|
|
416
|
+
processModelPolicy,
|
|
423
417
|
});
|
|
424
418
|
const workerWebSocketIpc = createWorkerWebSocketIpcManager();
|
|
425
419
|
// This window must open before listen(): workers from the previous server
|
|
@@ -699,6 +693,11 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
699
693
|
),
|
|
700
694
|
});
|
|
701
695
|
|
|
696
|
+
const integrationToolRequests = createIntegrationToolRequestService({
|
|
697
|
+
registry: integrationTools,
|
|
698
|
+
repos: baseDeps,
|
|
699
|
+
processActionRegistry,
|
|
700
|
+
});
|
|
702
701
|
const ipcHandler = createIpcHandler(
|
|
703
702
|
{
|
|
704
703
|
processes: baseDeps.processes,
|
|
@@ -707,6 +706,10 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
707
706
|
events: baseDeps.events,
|
|
708
707
|
leases: baseDeps.leases,
|
|
709
708
|
turnRecords: baseDeps.turnRecords,
|
|
709
|
+
handleIntegrationToolRequest: (instanceId, payload) =>
|
|
710
|
+
integrationToolRequests.handle(instanceId, payload),
|
|
711
|
+
handleIntegrationToolCancel: (instanceId, payload) =>
|
|
712
|
+
void integrationToolRequests.cancel(instanceId, payload),
|
|
710
713
|
processQuestions,
|
|
711
714
|
broadcaster,
|
|
712
715
|
commands: processEngine,
|
|
@@ -746,6 +749,9 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
746
749
|
onCredentialUpdateResult(instanceId, workerId, payload) {
|
|
747
750
|
supervisor?.credentialUpdateResult(instanceId, workerId, payload);
|
|
748
751
|
},
|
|
752
|
+
onIntegrationToolResult(instanceId, workerId, payload) {
|
|
753
|
+
supervisor?.integrationToolResult(instanceId, workerId, payload);
|
|
754
|
+
},
|
|
749
755
|
},
|
|
750
756
|
);
|
|
751
757
|
|
|
@@ -778,6 +784,7 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
778
784
|
return piResourceBundles.get(digest);
|
|
779
785
|
},
|
|
780
786
|
resolveRepositoryCredentials: (input) => repositoryCredentials.resolveWorkerCredentials(input),
|
|
787
|
+
integrationTools,
|
|
781
788
|
resolveCredential(providerId, options) {
|
|
782
789
|
const credential = modelProviderCredentials.resolve(providerId, options);
|
|
783
790
|
return credential && credential.revision !== null
|
|
@@ -925,28 +932,6 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
925
932
|
logger: app.log,
|
|
926
933
|
});
|
|
927
934
|
|
|
928
|
-
const filesystemProcessWatchers = createFilesystemProcessWatchersService({
|
|
929
|
-
processWatchers: processWatcherService,
|
|
930
|
-
launchPlans,
|
|
931
|
-
launchExecutorDeps: {
|
|
932
|
-
commitMessages: config.commit_messages,
|
|
933
|
-
processes: baseDeps.processes,
|
|
934
|
-
projects: baseDeps.projects,
|
|
935
|
-
processSkills: baseDeps.processSkills,
|
|
936
|
-
handoffDedupKeys: baseDeps.handoffDedupKeys,
|
|
937
|
-
futureExecutions: baseDeps.futureExecutions,
|
|
938
|
-
transaction: baseDeps.transaction,
|
|
939
|
-
broadcaster,
|
|
940
|
-
commands: processEngine,
|
|
941
|
-
processTitles,
|
|
942
|
-
extensionHost,
|
|
943
|
-
logger: app.log,
|
|
944
|
-
},
|
|
945
|
-
logger: app.log,
|
|
946
|
-
});
|
|
947
|
-
startHooks.push(() => filesystemProcessWatchers.start());
|
|
948
|
-
stopHooks.push(() => filesystemProcessWatchers.stop());
|
|
949
|
-
|
|
950
935
|
const processActionListDeps = {
|
|
951
936
|
projects: baseDeps.projects,
|
|
952
937
|
futureExecutions: baseDeps.futureExecutions,
|
|
@@ -1002,6 +987,7 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
1002
987
|
provide: hostCapabilities.provide.bind(hostCapabilities),
|
|
1003
988
|
get: hostCapabilities.get.bind(hostCapabilities),
|
|
1004
989
|
require: hostCapabilities.require.bind(hostCapabilities),
|
|
990
|
+
tool: (definition) => integrationTools.register(definition),
|
|
1005
991
|
onStart(handler: () => void | Promise<void>) {
|
|
1006
992
|
startHooks.push(handler);
|
|
1007
993
|
},
|
|
@@ -1011,6 +997,19 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
1011
997
|
},
|
|
1012
998
|
(id: string) => config.extensions?.[id],
|
|
1013
999
|
);
|
|
1000
|
+
for (const process of extensionCatalog.processes.values()) {
|
|
1001
|
+
for (const [turnId, binding] of process.turns) {
|
|
1002
|
+
const definition = binding.definition;
|
|
1003
|
+
if (definition.kind !== "llm" || !definition.integrationTools?.length) continue;
|
|
1004
|
+
try {
|
|
1005
|
+
integrationTools.declarations(definition.integrationTools);
|
|
1006
|
+
} catch (error) {
|
|
1007
|
+
throw new Error(
|
|
1008
|
+
`Invalid integration tools for process '${process.id}' turn '${turnId}': ${error instanceof Error ? error.message : String(error)}`,
|
|
1009
|
+
);
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1014
1013
|
markStartup("server_extensions");
|
|
1015
1014
|
|
|
1016
1015
|
const deps: RouteDeps = {
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ResolvedAuthProvider } from "./auth-config.js";
|
|
2
|
+
import { createGithubOauthClient, type GithubOauthClient } from "./github-oauth-client.js";
|
|
3
|
+
import { createOpenIdClient, type OidcClient } from "./oidc-client.js";
|
|
4
|
+
|
|
5
|
+
export interface AuthAuthorizationRequest {
|
|
6
|
+
provider: ResolvedAuthProvider;
|
|
7
|
+
state: string;
|
|
8
|
+
pkceVerifier: string;
|
|
9
|
+
redirectUrl: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface AuthClient {
|
|
13
|
+
createAuthorizationRequest(provider: ResolvedAuthProvider): Promise<AuthAuthorizationRequest>;
|
|
14
|
+
exchangeCallback(input: {
|
|
15
|
+
provider: ResolvedAuthProvider;
|
|
16
|
+
callbackUrl: URL;
|
|
17
|
+
state: string;
|
|
18
|
+
pkceVerifier: string;
|
|
19
|
+
}): Promise<{ claims: Record<string, unknown> }>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function createAuthClient(input?: {
|
|
23
|
+
oidc?: OidcClient;
|
|
24
|
+
github?: GithubOauthClient;
|
|
25
|
+
}): AuthClient {
|
|
26
|
+
const oidc = input?.oidc ?? createOpenIdClient();
|
|
27
|
+
const github = input?.github ?? createGithubOauthClient();
|
|
28
|
+
return {
|
|
29
|
+
createAuthorizationRequest(provider) {
|
|
30
|
+
return provider.kind === "oidc"
|
|
31
|
+
? oidc.createAuthorizationRequest(provider)
|
|
32
|
+
: github.createAuthorizationRequest(provider);
|
|
33
|
+
},
|
|
34
|
+
exchangeCallback(request) {
|
|
35
|
+
return request.provider.kind === "oidc"
|
|
36
|
+
? oidc.exchangeCallback({ ...request, provider: request.provider })
|
|
37
|
+
: github.exchangeCallback({ ...request, provider: request.provider });
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
package/src/auth/auth-config.ts
CHANGED
|
@@ -1,13 +1,25 @@
|
|
|
1
1
|
import type { Actor } from "@leitwerk-dev/domain";
|
|
2
2
|
import { parseDurationMs } from "@leitwerk-dev/watcher-utils";
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
AuthGithubProviderConfig,
|
|
5
|
+
AuthOidcProviderConfig,
|
|
6
|
+
LeitwerkConfig,
|
|
7
|
+
} from "../config/config-types.js";
|
|
4
8
|
|
|
5
|
-
export interface
|
|
9
|
+
export interface ResolvedOidcProvider extends AuthOidcProviderConfig {
|
|
6
10
|
redirect_uri: string;
|
|
7
11
|
scopes: string[];
|
|
8
12
|
identity_claim: string;
|
|
9
13
|
}
|
|
10
14
|
|
|
15
|
+
export interface ResolvedGithubProvider extends AuthGithubProviderConfig {
|
|
16
|
+
redirect_uri: string;
|
|
17
|
+
scopes: ["read:org"];
|
|
18
|
+
identity_claim: "login";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type ResolvedAuthProvider = ResolvedOidcProvider | ResolvedGithubProvider;
|
|
22
|
+
|
|
11
23
|
export interface ResolvedAuthConfig {
|
|
12
24
|
enabled: boolean;
|
|
13
25
|
sessionCookieName: string;
|
|
@@ -27,12 +39,23 @@ const DEFAULT_LOGIN_FLOW_TTL_MS = 10 * 60 * 1000;
|
|
|
27
39
|
export function resolveAuthConfig(config: LeitwerkConfig): ResolvedAuthConfig {
|
|
28
40
|
const auth = config.auth;
|
|
29
41
|
const serverBaseUrl = new URL(config.server.base_url);
|
|
30
|
-
const providers = (auth?.providers ?? []).map((provider) =>
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
42
|
+
const providers: ResolvedAuthProvider[] = (auth?.providers ?? []).map((provider) =>
|
|
43
|
+
provider.kind === "oidc"
|
|
44
|
+
? {
|
|
45
|
+
...provider,
|
|
46
|
+
redirect_uri:
|
|
47
|
+
provider.redirect_uri ?? new URL("/auth/callback", serverBaseUrl).toString(),
|
|
48
|
+
scopes: provider.scopes ?? ["openid", "profile", "email"],
|
|
49
|
+
identity_claim: provider.identity_claim ?? "preferred_username",
|
|
50
|
+
}
|
|
51
|
+
: {
|
|
52
|
+
...provider,
|
|
53
|
+
redirect_uri:
|
|
54
|
+
provider.redirect_uri ?? new URL("/auth/callback", serverBaseUrl).toString(),
|
|
55
|
+
scopes: ["read:org"],
|
|
56
|
+
identity_claim: "login",
|
|
57
|
+
},
|
|
58
|
+
);
|
|
36
59
|
const sessionCookieName = auth?.session?.cookie_name ?? DEFAULT_SESSION_COOKIE_NAME;
|
|
37
60
|
return {
|
|
38
61
|
enabled: auth?.enabled === true && providers.length > 0,
|
|
@@ -64,5 +87,7 @@ export function isActorAllowedByConfig(actor: Actor, auth: ResolvedAuthConfig):
|
|
|
64
87
|
return false;
|
|
65
88
|
}
|
|
66
89
|
const prefix = `${actor.provider}:`;
|
|
67
|
-
|
|
90
|
+
if (!actor.id.startsWith(prefix)) return false;
|
|
91
|
+
const provider = auth.providers.find((candidate) => candidate.id === actor.provider);
|
|
92
|
+
return provider?.kind === "oauth2" || auth.allowlist.has(actor.id.slice(prefix.length));
|
|
68
93
|
}
|
package/src/auth/auth-service.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Actor } from "@leitwerk-dev/domain";
|
|
2
2
|
import type { LeitwerkConfig } from "../config/config-types.js";
|
|
3
3
|
import type { AuthLoginFlowRecord, RepositoryBundle } from "../db/repositories.js";
|
|
4
|
+
import { type AuthClient, createAuthClient } from "./auth-client.js";
|
|
4
5
|
import {
|
|
5
6
|
isActorAllowedByConfig,
|
|
6
7
|
type ResolvedAuthConfig,
|
|
@@ -82,9 +83,14 @@ export function createAuthService(input: {
|
|
|
82
83
|
config: LeitwerkConfig;
|
|
83
84
|
repos: Pick<RepositoryBundle, "authSessions" | "authLoginFlows">;
|
|
84
85
|
oidcClient?: OidcClient;
|
|
86
|
+
authClient?: AuthClient;
|
|
85
87
|
}): AuthService {
|
|
86
88
|
const auth = resolveAuthConfig(input.config);
|
|
87
|
-
const
|
|
89
|
+
const authClient =
|
|
90
|
+
input.authClient ??
|
|
91
|
+
createAuthClient({
|
|
92
|
+
oidc: input.oidcClient ?? createOpenIdClient(),
|
|
93
|
+
});
|
|
88
94
|
const sweepExpiredAuthState = (): AuthSweepResult => ({
|
|
89
95
|
sessions: input.repos.authSessions.deleteExpired(),
|
|
90
96
|
loginFlows: input.repos.authLoginFlows.deleteExpired(),
|
|
@@ -99,7 +105,7 @@ export function createAuthService(input: {
|
|
|
99
105
|
throw new Error("Authentication is not enabled");
|
|
100
106
|
}
|
|
101
107
|
const provider = auth.providers[0];
|
|
102
|
-
const login = await
|
|
108
|
+
const login = await authClient.createAuthorizationRequest(provider);
|
|
103
109
|
const cookieValue = generateOpaqueToken();
|
|
104
110
|
const expiresAt = isoFromNow(auth.loginFlowTtlMs);
|
|
105
111
|
input.repos.authLoginFlows.create({
|
|
@@ -126,9 +132,9 @@ export function createAuthService(input: {
|
|
|
126
132
|
throw new Error("Login provider is no longer configured");
|
|
127
133
|
}
|
|
128
134
|
if (callbackUrl.searchParams.get("state") !== flow.state) {
|
|
129
|
-
throw new Error("Invalid
|
|
135
|
+
throw new Error("Invalid authentication state");
|
|
130
136
|
}
|
|
131
|
-
const exchanged = await
|
|
137
|
+
const exchanged = await authClient.exchangeCallback({
|
|
132
138
|
provider,
|
|
133
139
|
callbackUrl,
|
|
134
140
|
state: flow.state,
|
|
@@ -136,7 +142,7 @@ export function createAuthService(input: {
|
|
|
136
142
|
});
|
|
137
143
|
const actor = actorFromOidcClaims({ provider, claims: exchanged.claims });
|
|
138
144
|
if (!actor) {
|
|
139
|
-
throw new Error(`
|
|
145
|
+
throw new Error(`Authentication identity claim ${provider.identity_claim} was not present`);
|
|
140
146
|
}
|
|
141
147
|
if (!isActorAllowedByConfig(actor, auth)) {
|
|
142
148
|
throw new Error("Authenticated user is not allowlisted");
|
|
@@ -23,9 +23,9 @@ export function testAuthConfig(overrides?: {
|
|
|
23
23
|
},
|
|
24
24
|
providers: [
|
|
25
25
|
{
|
|
26
|
-
id: "
|
|
26
|
+
id: "identity",
|
|
27
27
|
kind: "oidc",
|
|
28
|
-
issuer: "https://
|
|
28
|
+
issuer: "https://identity.example.test",
|
|
29
29
|
client_id: "client",
|
|
30
30
|
client_secret: "secret",
|
|
31
31
|
...overrides?.provider,
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import type { ResolvedGithubProvider } from "./auth-config.js";
|
|
3
|
+
|
|
4
|
+
const GITHUB_AUTHORIZE_URL = "https://github.com/login/oauth/authorize";
|
|
5
|
+
const GITHUB_TOKEN_URL = "https://github.com/login/oauth/access_token";
|
|
6
|
+
const GITHUB_API_URL = "https://api.github.com";
|
|
7
|
+
const GITHUB_API_VERSION = "2022-11-28";
|
|
8
|
+
|
|
9
|
+
export interface GithubAuthorizationRequest {
|
|
10
|
+
provider: ResolvedGithubProvider;
|
|
11
|
+
state: string;
|
|
12
|
+
pkceVerifier: string;
|
|
13
|
+
redirectUrl: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface GithubOauthClient {
|
|
17
|
+
createAuthorizationRequest(provider: ResolvedGithubProvider): Promise<GithubAuthorizationRequest>;
|
|
18
|
+
exchangeCallback(input: {
|
|
19
|
+
provider: ResolvedGithubProvider;
|
|
20
|
+
callbackUrl: URL;
|
|
21
|
+
state: string;
|
|
22
|
+
pkceVerifier: string;
|
|
23
|
+
}): Promise<{ claims: Record<string, unknown> }>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function opaqueValue(): string {
|
|
27
|
+
return randomBytes(32).toString("base64url");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function githubHeaders(accessToken?: string): HeadersInit {
|
|
31
|
+
return {
|
|
32
|
+
Accept: "application/vnd.github+json",
|
|
33
|
+
"X-GitHub-Api-Version": GITHUB_API_VERSION,
|
|
34
|
+
"User-Agent": "leitwerk-server",
|
|
35
|
+
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function jsonObject(
|
|
40
|
+
response: Response,
|
|
41
|
+
errorMessage: string,
|
|
42
|
+
): Promise<Record<string, unknown>> {
|
|
43
|
+
if (!response.ok) throw new Error(errorMessage);
|
|
44
|
+
const value: unknown = await response.json();
|
|
45
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
46
|
+
throw new Error(errorMessage);
|
|
47
|
+
}
|
|
48
|
+
return value as Record<string, unknown>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function createGithubOauthClient(fetchImpl: typeof fetch = fetch): GithubOauthClient {
|
|
52
|
+
return {
|
|
53
|
+
async createAuthorizationRequest(provider) {
|
|
54
|
+
const state = opaqueValue();
|
|
55
|
+
const redirectUrl = new URL(GITHUB_AUTHORIZE_URL);
|
|
56
|
+
redirectUrl.searchParams.set("client_id", provider.client_id);
|
|
57
|
+
redirectUrl.searchParams.set("redirect_uri", provider.redirect_uri);
|
|
58
|
+
redirectUrl.searchParams.set("scope", provider.scopes.join(" "));
|
|
59
|
+
redirectUrl.searchParams.set("state", state);
|
|
60
|
+
return {
|
|
61
|
+
provider,
|
|
62
|
+
state,
|
|
63
|
+
// The persisted login-flow schema requires a verifier. GitHub OAuth Apps
|
|
64
|
+
// authenticate this confidential client with its secret instead of PKCE.
|
|
65
|
+
pkceVerifier: opaqueValue(),
|
|
66
|
+
redirectUrl: redirectUrl.toString(),
|
|
67
|
+
};
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
async exchangeCallback({ provider, callbackUrl }) {
|
|
71
|
+
const code = callbackUrl.searchParams.get("code");
|
|
72
|
+
if (!code) throw new Error("GitHub OAuth callback did not include a code");
|
|
73
|
+
|
|
74
|
+
const tokenResponse = await fetchImpl(GITHUB_TOKEN_URL, {
|
|
75
|
+
method: "POST",
|
|
76
|
+
headers: { ...githubHeaders(), "Content-Type": "application/x-www-form-urlencoded" },
|
|
77
|
+
body: new URLSearchParams({
|
|
78
|
+
client_id: provider.client_id,
|
|
79
|
+
client_secret: provider.client_secret,
|
|
80
|
+
code,
|
|
81
|
+
redirect_uri: provider.redirect_uri,
|
|
82
|
+
}).toString(),
|
|
83
|
+
});
|
|
84
|
+
const token = await jsonObject(tokenResponse, "GitHub OAuth token exchange failed");
|
|
85
|
+
const accessToken = typeof token.access_token === "string" ? token.access_token : "";
|
|
86
|
+
if (!accessToken) throw new Error("GitHub OAuth token exchange failed");
|
|
87
|
+
|
|
88
|
+
const userResponse = await fetchImpl(`${GITHUB_API_URL}/user`, {
|
|
89
|
+
headers: githubHeaders(accessToken),
|
|
90
|
+
});
|
|
91
|
+
const user = await jsonObject(userResponse, "GitHub user lookup failed");
|
|
92
|
+
const login = typeof user.login === "string" ? user.login.trim().toLowerCase() : "";
|
|
93
|
+
if (!login) throw new Error("GitHub user lookup failed");
|
|
94
|
+
|
|
95
|
+
const organization = encodeURIComponent(provider.organization);
|
|
96
|
+
const membershipResponse = await fetchImpl(
|
|
97
|
+
`${GITHUB_API_URL}/user/memberships/orgs/${organization}`,
|
|
98
|
+
{ headers: githubHeaders(accessToken) },
|
|
99
|
+
);
|
|
100
|
+
const membership = await jsonObject(
|
|
101
|
+
membershipResponse,
|
|
102
|
+
"GitHub organization membership is not active",
|
|
103
|
+
);
|
|
104
|
+
if (membership.state !== "active") {
|
|
105
|
+
throw new Error("GitHub organization membership is not active");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return { claims: { ...user, login } };
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
package/src/auth/oidc-client.ts
CHANGED
|
@@ -12,10 +12,10 @@ import {
|
|
|
12
12
|
skipSubjectCheck,
|
|
13
13
|
} from "openid-client";
|
|
14
14
|
import { isLoopbackHttpUrl } from "../config/url-policy.js";
|
|
15
|
-
import type {
|
|
15
|
+
import type { ResolvedOidcProvider } from "./auth-config.js";
|
|
16
16
|
|
|
17
17
|
export interface OidcAuthorizationRequest {
|
|
18
|
-
provider:
|
|
18
|
+
provider: ResolvedOidcProvider;
|
|
19
19
|
state: string;
|
|
20
20
|
pkceVerifier: string;
|
|
21
21
|
redirectUrl: string;
|
|
@@ -26,9 +26,9 @@ export interface OidcTokensAndClaims {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
export interface OidcClient {
|
|
29
|
-
createAuthorizationRequest(provider:
|
|
29
|
+
createAuthorizationRequest(provider: ResolvedOidcProvider): Promise<OidcAuthorizationRequest>;
|
|
30
30
|
exchangeCallback(input: {
|
|
31
|
-
provider:
|
|
31
|
+
provider: ResolvedOidcProvider;
|
|
32
32
|
callbackUrl: URL;
|
|
33
33
|
state: string;
|
|
34
34
|
pkceVerifier: string;
|
|
@@ -38,7 +38,7 @@ export interface OidcClient {
|
|
|
38
38
|
export function createOpenIdClient(): OidcClient {
|
|
39
39
|
const configs = new Map<string, Promise<Configuration>>();
|
|
40
40
|
|
|
41
|
-
async function getConfiguration(provider:
|
|
41
|
+
async function getConfiguration(provider: ResolvedOidcProvider): Promise<Configuration> {
|
|
42
42
|
const key = `${provider.id}:${provider.issuer}:${provider.client_id}`;
|
|
43
43
|
let configPromise = configs.get(key);
|
|
44
44
|
if (!configPromise) {
|
|
@@ -134,50 +134,10 @@ const modelTurnConfigSchema = v.looseObject({
|
|
|
134
134
|
model_profile: v.optional(v.string()),
|
|
135
135
|
});
|
|
136
136
|
|
|
137
|
-
const watcherLaunchConfigSchema = v.looseObject({
|
|
138
|
-
default_model_profile: v.optional(v.string()),
|
|
139
|
-
turn_configs: v.optional(v.record(v.string(), modelTurnConfigSchema)),
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
const jiraWatcherSchema = v.looseObject({
|
|
143
|
-
type: v.literal("jira"),
|
|
144
|
-
enabled: v.boolean(),
|
|
145
|
-
project: v.string(),
|
|
146
|
-
poll_interval: v.string(),
|
|
147
|
-
labels: v.looseObject({
|
|
148
|
-
trigger: v.string(),
|
|
149
|
-
done: v.string(),
|
|
150
|
-
required: v.optional(stringArraySchema),
|
|
151
|
-
forbidden: v.optional(stringArraySchema),
|
|
152
|
-
}),
|
|
153
|
-
target_branch_label_prefix: v.string(),
|
|
154
|
-
launch: v.optional(watcherLaunchConfigSchema),
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
const gitlabMrWatcherSchema = v.looseObject({
|
|
158
|
-
type: v.literal("gitlab_mr"),
|
|
159
|
-
enabled: v.boolean(),
|
|
160
|
-
group: v.string(),
|
|
161
|
-
poll_interval: v.string(),
|
|
162
|
-
labels: v.looseObject({
|
|
163
|
-
trigger: v.string(),
|
|
164
|
-
done: v.string(),
|
|
165
|
-
}),
|
|
166
|
-
launch: v.optional(watcherLaunchConfigSchema),
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
const filesystemWatcherSchema = v.looseObject({
|
|
170
|
-
type: v.literal("filesystem"),
|
|
171
|
-
enabled: v.boolean(),
|
|
172
|
-
poll_interval: v.string(),
|
|
173
|
-
file_path: v.string(),
|
|
174
|
-
launch: v.optional(watcherLaunchConfigSchema),
|
|
175
|
-
});
|
|
176
|
-
|
|
177
137
|
const authNonEmptyString = v.pipe(v.string(), v.nonEmpty());
|
|
178
138
|
|
|
179
139
|
const authOidcProviderSchema = v.looseObject({
|
|
180
|
-
id:
|
|
140
|
+
id: authNonEmptyString,
|
|
181
141
|
kind: v.literal("oidc"),
|
|
182
142
|
issuer: authNonEmptyString,
|
|
183
143
|
client_id: authNonEmptyString,
|
|
@@ -187,6 +147,15 @@ const authOidcProviderSchema = v.looseObject({
|
|
|
187
147
|
identity_claim: v.optional(authNonEmptyString),
|
|
188
148
|
});
|
|
189
149
|
|
|
150
|
+
const authGithubProviderSchema = v.looseObject({
|
|
151
|
+
id: v.literal("github"),
|
|
152
|
+
kind: v.literal("oauth2"),
|
|
153
|
+
client_id: authNonEmptyString,
|
|
154
|
+
client_secret: authNonEmptyString,
|
|
155
|
+
organization: authNonEmptyString,
|
|
156
|
+
redirect_uri: v.optional(authNonEmptyString),
|
|
157
|
+
});
|
|
158
|
+
|
|
190
159
|
const authConfigSchema = v.looseObject({
|
|
191
160
|
enabled: v.optional(v.boolean()),
|
|
192
161
|
session: v.optional(
|
|
@@ -195,16 +164,12 @@ const authConfigSchema = v.looseObject({
|
|
|
195
164
|
ttl: v.optional(authNonEmptyString),
|
|
196
165
|
}),
|
|
197
166
|
),
|
|
198
|
-
providers: v.optional(
|
|
167
|
+
providers: v.optional(
|
|
168
|
+
v.array(v.variant("kind", [authOidcProviderSchema, authGithubProviderSchema])),
|
|
169
|
+
),
|
|
199
170
|
allowlist: v.optional(stringArraySchema),
|
|
200
171
|
});
|
|
201
172
|
|
|
202
|
-
const processWatcherConfigSchema = v.variant("type", [
|
|
203
|
-
jiraWatcherSchema,
|
|
204
|
-
gitlabMrWatcherSchema,
|
|
205
|
-
filesystemWatcherSchema,
|
|
206
|
-
]);
|
|
207
|
-
|
|
208
173
|
const safeSkillIdSchema = v.pipe(v.string(), v.regex(SAFE_SKILL_ID_PATTERN));
|
|
209
174
|
const skillSchema = v.strictObject({
|
|
210
175
|
id: safeSkillIdSchema,
|
|
@@ -333,7 +298,7 @@ const configSchema = v.looseObject({
|
|
|
333
298
|
}),
|
|
334
299
|
),
|
|
335
300
|
turn_configs: v.optional(v.record(v.string(), modelTurnConfigSchema)),
|
|
336
|
-
watchers: v.optional(v.record(v.string(),
|
|
301
|
+
watchers: v.optional(v.record(v.string(), v.unknown())),
|
|
337
302
|
}),
|
|
338
303
|
),
|
|
339
304
|
),
|
|
@@ -470,27 +435,6 @@ function collectConfigSchemaErrors(config: unknown): string[] {
|
|
|
470
435
|
return parsed.issues.map((issue) => formatConfigSchemaIssue(issue).message);
|
|
471
436
|
}
|
|
472
437
|
|
|
473
|
-
function validateModelProfileReference(args: {
|
|
474
|
-
value: unknown;
|
|
475
|
-
path: string;
|
|
476
|
-
errors: string[];
|
|
477
|
-
configuredModelProfileIds: Set<string>;
|
|
478
|
-
allowedModelProfileIds: Set<string> | null;
|
|
479
|
-
}) {
|
|
480
|
-
if (typeof args.value !== "string" || args.value.trim() === "") {
|
|
481
|
-
return;
|
|
482
|
-
}
|
|
483
|
-
if (!args.configuredModelProfileIds.has(args.value)) {
|
|
484
|
-
args.errors.push(`Unknown model profile '${args.value}' at ${args.path}`);
|
|
485
|
-
return;
|
|
486
|
-
}
|
|
487
|
-
if (args.allowedModelProfileIds !== null && !args.allowedModelProfileIds.has(args.value)) {
|
|
488
|
-
args.errors.push(
|
|
489
|
-
`Model profile '${args.value}' at ${args.path} is not in allowed_model_profiles`,
|
|
490
|
-
);
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
|
|
494
438
|
function collectModelProfileReferenceErrors(config: LeitwerkConfig): string[] {
|
|
495
439
|
const errors: string[] = [];
|
|
496
440
|
const configuredModelProfileIds = new Set(
|
|
@@ -512,9 +456,7 @@ function collectModelProfileReferenceErrors(config: LeitwerkConfig): string[] {
|
|
|
512
456
|
}
|
|
513
457
|
for (const [processId, processConfig] of Object.entries(config.process_configs ?? {})) {
|
|
514
458
|
const processConfigPath = `process_configs.${processId}`;
|
|
515
|
-
let processAllowedProfiles: Set<string> | null = null;
|
|
516
459
|
if (processConfig.allowed_model_profiles !== undefined) {
|
|
517
|
-
processAllowedProfiles = new Set<string>();
|
|
518
460
|
for (const [index, profileId] of processConfig.allowed_model_profiles.entries()) {
|
|
519
461
|
const allowedProfilePath = `${processConfigPath}.allowed_model_profiles[${index}]`;
|
|
520
462
|
if (typeof profileId !== "string" || profileId.trim() === "") {
|
|
@@ -522,32 +464,7 @@ function collectModelProfileReferenceErrors(config: LeitwerkConfig): string[] {
|
|
|
522
464
|
}
|
|
523
465
|
if (!configuredModelProfileIds.has(profileId)) {
|
|
524
466
|
errors.push(`Unknown model profile '${profileId}' at ${allowedProfilePath}`);
|
|
525
|
-
continue;
|
|
526
467
|
}
|
|
527
|
-
processAllowedProfiles.add(profileId);
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
for (const [watcherId, watcherConfig] of Object.entries(processConfig.watchers ?? {})) {
|
|
531
|
-
const launch = watcherConfig.launch;
|
|
532
|
-
if (!launch) {
|
|
533
|
-
continue;
|
|
534
|
-
}
|
|
535
|
-
const watcherPath = `${processConfigPath}.watchers.${watcherId}`;
|
|
536
|
-
validateModelProfileReference({
|
|
537
|
-
value: launch.default_model_profile,
|
|
538
|
-
path: `${watcherPath}.launch.default_model_profile`,
|
|
539
|
-
errors,
|
|
540
|
-
configuredModelProfileIds,
|
|
541
|
-
allowedModelProfileIds: processAllowedProfiles,
|
|
542
|
-
});
|
|
543
|
-
for (const [turnId, turnConfig] of Object.entries(launch.turn_configs ?? {})) {
|
|
544
|
-
validateModelProfileReference({
|
|
545
|
-
value: turnConfig.model_profile,
|
|
546
|
-
path: `${watcherPath}.launch.turn_configs.${turnId}.model_profile`,
|
|
547
|
-
errors,
|
|
548
|
-
configuredModelProfileIds,
|
|
549
|
-
allowedModelProfileIds: processAllowedProfiles,
|
|
550
|
-
});
|
|
551
468
|
}
|
|
552
469
|
}
|
|
553
470
|
}
|
|
@@ -829,7 +746,9 @@ function collectAuthConfigErrors(config: LeitwerkConfig): string[] {
|
|
|
829
746
|
}
|
|
830
747
|
const providers = auth.providers ?? [];
|
|
831
748
|
if (providers.length === 0) {
|
|
832
|
-
errors.push(
|
|
749
|
+
errors.push(
|
|
750
|
+
"auth.providers must include one authentication provider when auth.enabled is true",
|
|
751
|
+
);
|
|
833
752
|
}
|
|
834
753
|
if (providers.length > 1) {
|
|
835
754
|
errors.push("auth.providers currently supports exactly one provider");
|
|
@@ -838,10 +757,7 @@ function collectAuthConfigErrors(config: LeitwerkConfig): string[] {
|
|
|
838
757
|
errors.push("server.base_url must use https when auth is enabled, except for localhost");
|
|
839
758
|
}
|
|
840
759
|
for (const [index, provider] of providers.entries()) {
|
|
841
|
-
if (provider.
|
|
842
|
-
errors.push(`auth.providers[${index}].id must be forgejo in Phase 1`);
|
|
843
|
-
}
|
|
844
|
-
if (!isHttpsOrLoopbackHttpUrl(provider.issuer)) {
|
|
760
|
+
if (provider.kind === "oidc" && !isHttpsOrLoopbackHttpUrl(provider.issuer)) {
|
|
845
761
|
errors.push(
|
|
846
762
|
`auth.providers[${index}].issuer must be an absolute https URL, except for localhost`,
|
|
847
763
|
);
|