@leitwerk-dev/server 0.1.6 → 0.1.8
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 +31 -40
- 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-ES5RBLAT.js} +47 -99
- package/dist/chunk-ES5RBLAT.js.map +1 -0
- package/dist/{chunk-KVHAK5UR.js → chunk-UC3OOEQ3.js} +527 -410
- package/dist/chunk-UC3OOEQ3.js.map +1 -0
- package/dist/config/config-loader.d.ts.map +1 -1
- package/dist/config/config-loader.js +21 -89
- package/dist/config/config-loader.js.map +1 -1
- package/dist/config/config-types.d.ts +18 -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/model-providers/builtin-server-adapter.d.ts.map +1 -1
- package/dist/model-providers/builtin-server-adapter.js +10 -2
- package/dist/model-providers/builtin-server-adapter.js.map +1 -1
- 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 +5 -4
- 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 +4 -5
- 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-CVXLCYce.d.ts} +61 -13
- package/package.json +10 -10
- package/src/app.ts +37 -44
- 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 +42 -103
- package/src/config/config-types.ts +21 -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/model-providers/builtin-server-adapter.ts +10 -2
- 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 +21 -1
- package/src/supervisor/worker-supervisor.ts +14 -4
- 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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leitwerk-dev/server",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/leitwerk-dev/leitwerk.git",
|
|
@@ -28,14 +28,14 @@
|
|
|
28
28
|
"@earendil-works/pi-coding-agent": "^0.81.1",
|
|
29
29
|
"@fastify/cookie": "11.0.2",
|
|
30
30
|
"@fastify/websocket": "^11.2.0",
|
|
31
|
-
"@leitwerk-dev/domain": "0.1.
|
|
32
|
-
"@leitwerk-dev/extension-runtime": "0.1.
|
|
33
|
-
"@leitwerk-dev/external-writes": "0.1.
|
|
34
|
-
"@leitwerk-dev/process-sdk": "0.1.
|
|
35
|
-
"@leitwerk-dev/protocol": "0.1.
|
|
36
|
-
"@leitwerk-dev/watcher-utils": "0.1.
|
|
37
|
-
"@leitwerk-dev/worker-protocol": "0.1.
|
|
38
|
-
"@leitwerk-dev/worker-runners": "0.1.
|
|
31
|
+
"@leitwerk-dev/domain": "0.1.8",
|
|
32
|
+
"@leitwerk-dev/extension-runtime": "0.1.8",
|
|
33
|
+
"@leitwerk-dev/external-writes": "0.1.8",
|
|
34
|
+
"@leitwerk-dev/process-sdk": "0.1.8",
|
|
35
|
+
"@leitwerk-dev/protocol": "0.1.8",
|
|
36
|
+
"@leitwerk-dev/watcher-utils": "0.1.8",
|
|
37
|
+
"@leitwerk-dev/worker-protocol": "0.1.8",
|
|
38
|
+
"@leitwerk-dev/worker-runners": "0.1.8",
|
|
39
39
|
"better-sqlite3": "^12.8.0",
|
|
40
40
|
"cron-parser": "^5.5.0",
|
|
41
41
|
"defu": "^6.1.7",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"yaml": "^2.8.3"
|
|
49
49
|
},
|
|
50
50
|
"optionalDependencies": {
|
|
51
|
-
"@leitwerk-dev/worker": "0.1.
|
|
51
|
+
"@leitwerk-dev/worker": "0.1.8"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@types/better-sqlite3": "^7.6.13",
|
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,
|
|
@@ -226,6 +226,7 @@ async function createConfiguredWorkerRunnerRuntime(input: {
|
|
|
226
226
|
annotations: pod.annotations,
|
|
227
227
|
nodeSelector: pod.node_selector,
|
|
228
228
|
tolerations: pod.tolerations,
|
|
229
|
+
hostAliases: pod.host_aliases,
|
|
229
230
|
}
|
|
230
231
|
: {}),
|
|
231
232
|
},
|
|
@@ -368,6 +369,7 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
368
369
|
const processOperations = createProcessOperationCoordinator();
|
|
369
370
|
const processGraphs = extensionCatalog.processes;
|
|
370
371
|
const processActionRegistry = buildProcessActionRegistry(extensionCatalog);
|
|
372
|
+
const integrationTools = new IntegrationToolRegistry();
|
|
371
373
|
const processUiRegistry = buildProcessUiRegistry(extensionCatalog);
|
|
372
374
|
const processModelPolicy = createServerProcessModelPolicy({
|
|
373
375
|
config,
|
|
@@ -409,17 +411,10 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
409
411
|
commitMessages: config.commit_messages,
|
|
410
412
|
getModelProfilesForProcess,
|
|
411
413
|
});
|
|
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
414
|
const processWatcherService = buildProcessWatcherRegistry(extensionCatalog, config, {
|
|
421
415
|
modelProfiles: launcherModelProfiles,
|
|
422
416
|
getModelProfilesForProcess,
|
|
417
|
+
processModelPolicy,
|
|
423
418
|
});
|
|
424
419
|
const workerWebSocketIpc = createWorkerWebSocketIpcManager();
|
|
425
420
|
// This window must open before listen(): workers from the previous server
|
|
@@ -699,6 +694,11 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
699
694
|
),
|
|
700
695
|
});
|
|
701
696
|
|
|
697
|
+
const integrationToolRequests = createIntegrationToolRequestService({
|
|
698
|
+
registry: integrationTools,
|
|
699
|
+
repos: baseDeps,
|
|
700
|
+
processActionRegistry,
|
|
701
|
+
});
|
|
702
702
|
const ipcHandler = createIpcHandler(
|
|
703
703
|
{
|
|
704
704
|
processes: baseDeps.processes,
|
|
@@ -707,6 +707,10 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
707
707
|
events: baseDeps.events,
|
|
708
708
|
leases: baseDeps.leases,
|
|
709
709
|
turnRecords: baseDeps.turnRecords,
|
|
710
|
+
handleIntegrationToolRequest: (instanceId, payload) =>
|
|
711
|
+
integrationToolRequests.handle(instanceId, payload),
|
|
712
|
+
handleIntegrationToolCancel: (instanceId, payload) =>
|
|
713
|
+
void integrationToolRequests.cancel(instanceId, payload),
|
|
710
714
|
processQuestions,
|
|
711
715
|
broadcaster,
|
|
712
716
|
commands: processEngine,
|
|
@@ -746,6 +750,9 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
746
750
|
onCredentialUpdateResult(instanceId, workerId, payload) {
|
|
747
751
|
supervisor?.credentialUpdateResult(instanceId, workerId, payload);
|
|
748
752
|
},
|
|
753
|
+
onIntegrationToolResult(instanceId, workerId, payload) {
|
|
754
|
+
supervisor?.integrationToolResult(instanceId, workerId, payload);
|
|
755
|
+
},
|
|
749
756
|
},
|
|
750
757
|
);
|
|
751
758
|
|
|
@@ -778,15 +785,9 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
778
785
|
return piResourceBundles.get(digest);
|
|
779
786
|
},
|
|
780
787
|
resolveRepositoryCredentials: (input) => repositoryCredentials.resolveWorkerCredentials(input),
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
? {
|
|
785
|
-
revision: credential.revision,
|
|
786
|
-
values: credential.values,
|
|
787
|
-
}
|
|
788
|
-
: null;
|
|
789
|
-
},
|
|
788
|
+
integrationTools,
|
|
789
|
+
resolveCredential: (providerId, options) =>
|
|
790
|
+
modelProviderCredentials.resolve(providerId, options),
|
|
790
791
|
});
|
|
791
792
|
|
|
792
793
|
let staleHeartbeatWatchdog: ReturnType<typeof startStaleHeartbeatWatchdog> | null = null;
|
|
@@ -925,28 +926,6 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
925
926
|
logger: app.log,
|
|
926
927
|
});
|
|
927
928
|
|
|
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
929
|
const processActionListDeps = {
|
|
951
930
|
projects: baseDeps.projects,
|
|
952
931
|
futureExecutions: baseDeps.futureExecutions,
|
|
@@ -1002,6 +981,7 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
1002
981
|
provide: hostCapabilities.provide.bind(hostCapabilities),
|
|
1003
982
|
get: hostCapabilities.get.bind(hostCapabilities),
|
|
1004
983
|
require: hostCapabilities.require.bind(hostCapabilities),
|
|
984
|
+
tool: (definition) => integrationTools.register(definition),
|
|
1005
985
|
onStart(handler: () => void | Promise<void>) {
|
|
1006
986
|
startHooks.push(handler);
|
|
1007
987
|
},
|
|
@@ -1011,6 +991,19 @@ export async function createAppContext(opts: AppOptions = {}): Promise<AppContex
|
|
|
1011
991
|
},
|
|
1012
992
|
(id: string) => config.extensions?.[id],
|
|
1013
993
|
);
|
|
994
|
+
for (const process of extensionCatalog.processes.values()) {
|
|
995
|
+
for (const [turnId, binding] of process.turns) {
|
|
996
|
+
const definition = binding.definition;
|
|
997
|
+
if (definition.kind !== "llm" || !definition.integrationTools?.length) continue;
|
|
998
|
+
try {
|
|
999
|
+
integrationTools.declarations(definition.integrationTools);
|
|
1000
|
+
} catch (error) {
|
|
1001
|
+
throw new Error(
|
|
1002
|
+
`Invalid integration tools for process '${process.id}' turn '${turnId}': ${error instanceof Error ? error.message : String(error)}`,
|
|
1003
|
+
);
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1014
1007
|
markStartup("server_extensions");
|
|
1015
1008
|
|
|
1016
1009
|
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) {
|