@lobu/gateway 3.0.9 → 3.0.13
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/api/platform.d.ts.map +1 -1
- package/dist/api/platform.js +7 -26
- package/dist/api/platform.js.map +1 -1
- package/dist/auth/mcp/proxy.d.ts +14 -0
- package/dist/auth/mcp/proxy.d.ts.map +1 -1
- package/dist/auth/mcp/proxy.js +149 -13
- package/dist/auth/mcp/proxy.js.map +1 -1
- package/dist/cli/gateway.d.ts.map +1 -1
- package/dist/cli/gateway.js +29 -0
- package/dist/cli/gateway.js.map +1 -1
- package/dist/connections/chat-instance-manager.d.ts.map +1 -1
- package/dist/connections/chat-instance-manager.js +2 -1
- package/dist/connections/chat-instance-manager.js.map +1 -1
- package/dist/connections/interaction-bridge.d.ts +9 -2
- package/dist/connections/interaction-bridge.d.ts.map +1 -1
- package/dist/connections/interaction-bridge.js +121 -261
- package/dist/connections/interaction-bridge.js.map +1 -1
- package/dist/gateway/index.js +1 -1
- package/dist/gateway/index.js.map +1 -1
- package/dist/interactions.d.ts +9 -43
- package/dist/interactions.d.ts.map +1 -1
- package/dist/interactions.js +10 -52
- package/dist/interactions.js.map +1 -1
- package/dist/routes/public/agent.d.ts +4 -0
- package/dist/routes/public/agent.d.ts.map +1 -1
- package/dist/routes/public/agent.js +21 -0
- package/dist/routes/public/agent.js.map +1 -1
- package/dist/services/core-services.d.ts.map +1 -1
- package/dist/services/core-services.js +4 -0
- package/dist/services/core-services.js.map +1 -1
- package/package.json +9 -9
- package/src/__tests__/agent-config-routes.test.ts +0 -254
- package/src/__tests__/agent-history-routes.test.ts +0 -72
- package/src/__tests__/agent-routes.test.ts +0 -68
- package/src/__tests__/agent-schedules-routes.test.ts +0 -59
- package/src/__tests__/agent-settings-store.test.ts +0 -323
- package/src/__tests__/bedrock-model-catalog.test.ts +0 -40
- package/src/__tests__/bedrock-openai-service.test.ts +0 -157
- package/src/__tests__/bedrock-provider-module.test.ts +0 -56
- package/src/__tests__/chat-instance-manager-slack.test.ts +0 -204
- package/src/__tests__/chat-response-bridge.test.ts +0 -131
- package/src/__tests__/config-memory-plugins.test.ts +0 -92
- package/src/__tests__/config-request-store.test.ts +0 -127
- package/src/__tests__/connection-routes.test.ts +0 -144
- package/src/__tests__/core-services-store-selection.test.ts +0 -92
- package/src/__tests__/docker-deployment.test.ts +0 -1211
- package/src/__tests__/embedded-deployment.test.ts +0 -342
- package/src/__tests__/grant-store.test.ts +0 -148
- package/src/__tests__/http-proxy.test.ts +0 -281
- package/src/__tests__/instruction-service.test.ts +0 -37
- package/src/__tests__/link-buttons.test.ts +0 -112
- package/src/__tests__/lobu.test.ts +0 -32
- package/src/__tests__/mcp-config-service.test.ts +0 -347
- package/src/__tests__/mcp-proxy.test.ts +0 -694
- package/src/__tests__/message-handler-bridge.test.ts +0 -17
- package/src/__tests__/model-selection.test.ts +0 -172
- package/src/__tests__/oauth-templates.test.ts +0 -39
- package/src/__tests__/platform-adapter-slack-send.test.ts +0 -114
- package/src/__tests__/platform-helpers-model-resolution.test.ts +0 -253
- package/src/__tests__/provider-inheritance.test.ts +0 -212
- package/src/__tests__/routes/cli-auth.test.ts +0 -337
- package/src/__tests__/routes/interactions.test.ts +0 -121
- package/src/__tests__/secret-proxy.test.ts +0 -85
- package/src/__tests__/session-manager.test.ts +0 -572
- package/src/__tests__/setup.ts +0 -133
- package/src/__tests__/skill-and-mcp-registry.test.ts +0 -203
- package/src/__tests__/slack-routes.test.ts +0 -161
- package/src/__tests__/system-config-resolver.test.ts +0 -75
- package/src/__tests__/system-message-limiter.test.ts +0 -89
- package/src/__tests__/system-skills-service.test.ts +0 -362
- package/src/__tests__/transcription-service.test.ts +0 -222
- package/src/__tests__/utils/rate-limiter.test.ts +0 -102
- package/src/__tests__/worker-connection-manager.test.ts +0 -497
- package/src/__tests__/worker-job-router.test.ts +0 -722
- package/src/api/index.ts +0 -1
- package/src/api/platform.ts +0 -292
- package/src/api/response-renderer.ts +0 -157
- package/src/auth/agent-metadata-store.ts +0 -168
- package/src/auth/api-auth-middleware.ts +0 -69
- package/src/auth/api-key-provider-module.ts +0 -213
- package/src/auth/base-provider-module.ts +0 -201
- package/src/auth/bedrock/provider-module.ts +0 -110
- package/src/auth/chatgpt/chatgpt-oauth-module.ts +0 -185
- package/src/auth/chatgpt/device-code-client.ts +0 -218
- package/src/auth/chatgpt/index.ts +0 -1
- package/src/auth/claude/oauth-module.ts +0 -280
- package/src/auth/cli/token-service.ts +0 -249
- package/src/auth/external/client.ts +0 -560
- package/src/auth/external/device-code-client.ts +0 -235
- package/src/auth/mcp/config-service.ts +0 -420
- package/src/auth/mcp/proxy.ts +0 -1086
- package/src/auth/mcp/string-substitution.ts +0 -17
- package/src/auth/mcp/tool-cache.ts +0 -90
- package/src/auth/oauth/base-client.ts +0 -267
- package/src/auth/oauth/client.ts +0 -153
- package/src/auth/oauth/credentials.ts +0 -7
- package/src/auth/oauth/providers.ts +0 -69
- package/src/auth/oauth/state-store.ts +0 -150
- package/src/auth/oauth-templates.ts +0 -179
- package/src/auth/provider-catalog.ts +0 -220
- package/src/auth/provider-model-options.ts +0 -41
- package/src/auth/settings/agent-settings-store.ts +0 -565
- package/src/auth/settings/auth-profiles-manager.ts +0 -216
- package/src/auth/settings/index.ts +0 -12
- package/src/auth/settings/model-preference-store.ts +0 -52
- package/src/auth/settings/model-selection.ts +0 -135
- package/src/auth/settings/resolved-settings-view.ts +0 -298
- package/src/auth/settings/template-utils.ts +0 -44
- package/src/auth/settings/token-service.ts +0 -88
- package/src/auth/system-env-store.ts +0 -98
- package/src/auth/user-agents-store.ts +0 -68
- package/src/channels/binding-service.ts +0 -214
- package/src/channels/index.ts +0 -4
- package/src/cli/gateway.ts +0 -1312
- package/src/cli/index.ts +0 -74
- package/src/commands/built-in-commands.ts +0 -80
- package/src/commands/command-dispatcher.ts +0 -94
- package/src/commands/command-reply-adapters.ts +0 -27
- package/src/config/file-loader.ts +0 -618
- package/src/config/index.ts +0 -588
- package/src/config/network-allowlist.ts +0 -71
- package/src/connections/chat-instance-manager.ts +0 -1284
- package/src/connections/chat-response-bridge.ts +0 -618
- package/src/connections/index.ts +0 -7
- package/src/connections/interaction-bridge.ts +0 -831
- package/src/connections/message-handler-bridge.ts +0 -440
- package/src/connections/platform-auth-methods.ts +0 -15
- package/src/connections/types.ts +0 -84
- package/src/gateway/connection-manager.ts +0 -291
- package/src/gateway/index.ts +0 -698
- package/src/gateway/job-router.ts +0 -201
- package/src/gateway-main.ts +0 -200
- package/src/index.ts +0 -41
- package/src/infrastructure/queue/index.ts +0 -12
- package/src/infrastructure/queue/queue-producer.ts +0 -148
- package/src/infrastructure/queue/redis-queue.ts +0 -361
- package/src/infrastructure/queue/types.ts +0 -133
- package/src/infrastructure/redis/system-message-limiter.ts +0 -94
- package/src/interactions/config-request-store.ts +0 -198
- package/src/interactions.ts +0 -363
- package/src/lobu.ts +0 -311
- package/src/metrics/prometheus.ts +0 -159
- package/src/modules/module-system.ts +0 -179
- package/src/orchestration/base-deployment-manager.ts +0 -900
- package/src/orchestration/deployment-utils.ts +0 -98
- package/src/orchestration/impl/docker-deployment.ts +0 -620
- package/src/orchestration/impl/embedded-deployment.ts +0 -268
- package/src/orchestration/impl/index.ts +0 -8
- package/src/orchestration/impl/k8s/deployment.ts +0 -1061
- package/src/orchestration/impl/k8s/helpers.ts +0 -610
- package/src/orchestration/impl/k8s/index.ts +0 -1
- package/src/orchestration/index.ts +0 -333
- package/src/orchestration/message-consumer.ts +0 -584
- package/src/orchestration/scheduled-wakeup.ts +0 -704
- package/src/permissions/approval-policy.ts +0 -36
- package/src/permissions/grant-store.ts +0 -219
- package/src/platform/file-handler.ts +0 -66
- package/src/platform/link-buttons.ts +0 -57
- package/src/platform/renderer-utils.ts +0 -44
- package/src/platform/response-renderer.ts +0 -84
- package/src/platform/unified-thread-consumer.ts +0 -194
- package/src/platform.ts +0 -318
- package/src/proxy/http-proxy.ts +0 -752
- package/src/proxy/proxy-manager.ts +0 -81
- package/src/proxy/secret-proxy.ts +0 -402
- package/src/proxy/token-refresh-job.ts +0 -143
- package/src/routes/internal/audio.ts +0 -141
- package/src/routes/internal/device-auth.ts +0 -652
- package/src/routes/internal/files.ts +0 -226
- package/src/routes/internal/history.ts +0 -69
- package/src/routes/internal/images.ts +0 -127
- package/src/routes/internal/interactions.ts +0 -84
- package/src/routes/internal/middleware.ts +0 -23
- package/src/routes/internal/schedule.ts +0 -226
- package/src/routes/internal/types.ts +0 -22
- package/src/routes/openapi-auto.ts +0 -239
- package/src/routes/public/agent-access.ts +0 -23
- package/src/routes/public/agent-config.ts +0 -675
- package/src/routes/public/agent-history.ts +0 -422
- package/src/routes/public/agent-schedules.ts +0 -296
- package/src/routes/public/agent.ts +0 -1086
- package/src/routes/public/agents.ts +0 -373
- package/src/routes/public/channels.ts +0 -191
- package/src/routes/public/cli-auth.ts +0 -896
- package/src/routes/public/connections.ts +0 -574
- package/src/routes/public/landing.ts +0 -16
- package/src/routes/public/oauth.ts +0 -147
- package/src/routes/public/settings-auth.ts +0 -104
- package/src/routes/public/slack.ts +0 -173
- package/src/routes/shared/agent-ownership.ts +0 -101
- package/src/routes/shared/token-verifier.ts +0 -34
- package/src/services/bedrock-model-catalog.ts +0 -217
- package/src/services/bedrock-openai-service.ts +0 -658
- package/src/services/core-services.ts +0 -1072
- package/src/services/image-generation-service.ts +0 -257
- package/src/services/instruction-service.ts +0 -318
- package/src/services/mcp-registry.ts +0 -94
- package/src/services/platform-helpers.ts +0 -287
- package/src/services/session-manager.ts +0 -262
- package/src/services/settings-resolver.ts +0 -74
- package/src/services/system-config-resolver.ts +0 -89
- package/src/services/system-skills-service.ts +0 -229
- package/src/services/transcription-service.ts +0 -684
- package/src/session.ts +0 -110
- package/src/spaces/index.ts +0 -1
- package/src/spaces/space-resolver.ts +0 -17
- package/src/stores/in-memory-agent-store.ts +0 -403
- package/src/stores/redis-agent-store.ts +0 -279
- package/src/utils/public-url.ts +0 -44
- package/src/utils/rate-limiter.ts +0 -94
- package/tsconfig.json +0 -33
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -1,560 +0,0 @@
|
|
|
1
|
-
import { randomBytes } from "node:crypto";
|
|
2
|
-
import { createLogger } from "@lobu/core";
|
|
3
|
-
import { OAuthClient } from "../oauth/client";
|
|
4
|
-
import type { OAuthCredentials } from "../oauth/credentials";
|
|
5
|
-
import type { OAuthProviderConfig } from "../oauth/providers";
|
|
6
|
-
import {
|
|
7
|
-
DEVICE_CODE_GRANT_TYPE,
|
|
8
|
-
type DeviceAuthorizationStartResult,
|
|
9
|
-
GenericDeviceCodeClient,
|
|
10
|
-
} from "./device-code-client";
|
|
11
|
-
|
|
12
|
-
const logger = createLogger("external-auth-client");
|
|
13
|
-
const EXTERNAL_AUTH_CACHE_KEY = "external:auth:client:v3";
|
|
14
|
-
const DISCOVERY_CACHE_TTL_MS = 5 * 60 * 1000;
|
|
15
|
-
const DEFAULT_SCOPE = "profile:read";
|
|
16
|
-
|
|
17
|
-
export interface ExternalAuthConfig {
|
|
18
|
-
issuerUrl: string;
|
|
19
|
-
clientId?: string;
|
|
20
|
-
clientSecret?: string;
|
|
21
|
-
authorizeUrl?: string;
|
|
22
|
-
tokenUrl?: string;
|
|
23
|
-
userinfoUrl?: string;
|
|
24
|
-
deviceAuthorizationUrl?: string;
|
|
25
|
-
redirectUri: string;
|
|
26
|
-
/** Additional redirect URIs to register (e.g. PUBLIC_GATEWAY_URL alongside localhost) */
|
|
27
|
-
additionalRedirectUris?: string[];
|
|
28
|
-
scope?: string;
|
|
29
|
-
cacheStore?: {
|
|
30
|
-
get: (key: string) => Promise<string | null>;
|
|
31
|
-
set: (key: string, value: string, ttlSeconds: number) => Promise<void>;
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
interface WellKnownMetadata {
|
|
36
|
-
issuer?: string;
|
|
37
|
-
authorization_endpoint?: string;
|
|
38
|
-
token_endpoint?: string;
|
|
39
|
-
registration_endpoint?: string;
|
|
40
|
-
userinfo_endpoint?: string;
|
|
41
|
-
device_authorization_endpoint?: string;
|
|
42
|
-
token_endpoint_auth_methods_supported?: string[];
|
|
43
|
-
grant_types_supported?: string[];
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
interface UserInfoResponse {
|
|
47
|
-
sub: string;
|
|
48
|
-
email: string;
|
|
49
|
-
name?: string;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
interface DynamicClientCredentials {
|
|
53
|
-
client_id: string;
|
|
54
|
-
client_secret?: string;
|
|
55
|
-
token_endpoint_auth_method?:
|
|
56
|
-
| "none"
|
|
57
|
-
| "client_secret_post"
|
|
58
|
-
| "client_secret_basic";
|
|
59
|
-
client_secret_expires_at?: number;
|
|
60
|
-
grant_types?: string[];
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
interface ResolvedExternalAuthConfig {
|
|
64
|
-
clientId: string;
|
|
65
|
-
clientSecret?: string;
|
|
66
|
-
authUrl?: string;
|
|
67
|
-
tokenUrl?: string;
|
|
68
|
-
userinfoUrl?: string;
|
|
69
|
-
deviceAuthorizationUrl?: string;
|
|
70
|
-
grantTypesSupported: string[];
|
|
71
|
-
tokenEndpointAuthMethod:
|
|
72
|
-
| "none"
|
|
73
|
-
| "client_secret_post"
|
|
74
|
-
| "client_secret_basic";
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export interface ExternalAuthCapabilities {
|
|
78
|
-
browser: boolean;
|
|
79
|
-
device: boolean;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export type ExternalDeviceAuthorizationPollResult =
|
|
83
|
-
| {
|
|
84
|
-
status: "pending";
|
|
85
|
-
interval?: number;
|
|
86
|
-
}
|
|
87
|
-
| {
|
|
88
|
-
status: "error";
|
|
89
|
-
error: string;
|
|
90
|
-
errorCode?: string;
|
|
91
|
-
}
|
|
92
|
-
| {
|
|
93
|
-
status: "complete";
|
|
94
|
-
credentials: OAuthCredentials;
|
|
95
|
-
user?: UserInfoResponse;
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
export class ExternalAuthClient {
|
|
99
|
-
private discoveryCache: {
|
|
100
|
-
metadata: WellKnownMetadata | null;
|
|
101
|
-
resolvedAt: number;
|
|
102
|
-
} | null = null;
|
|
103
|
-
|
|
104
|
-
constructor(private readonly config: ExternalAuthConfig) {}
|
|
105
|
-
|
|
106
|
-
generateCodeVerifier(): string {
|
|
107
|
-
return randomBytes(32).toString("base64url");
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
async buildAuthUrl(
|
|
111
|
-
state: string,
|
|
112
|
-
codeVerifier: string,
|
|
113
|
-
redirectUri?: string
|
|
114
|
-
): Promise<string> {
|
|
115
|
-
const resolved = await this.resolveConfig();
|
|
116
|
-
if (!resolved.authUrl || !resolved.tokenUrl) {
|
|
117
|
-
throw new Error(
|
|
118
|
-
"External auth: authorization and token URLs are required for browser login"
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return this.buildOAuthClient(resolved).buildAuthUrl(
|
|
123
|
-
state,
|
|
124
|
-
codeVerifier,
|
|
125
|
-
redirectUri
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
async exchangeCodeForToken(
|
|
130
|
-
code: string,
|
|
131
|
-
codeVerifier: string,
|
|
132
|
-
redirectUri?: string
|
|
133
|
-
): Promise<OAuthCredentials> {
|
|
134
|
-
const resolved = await this.resolveConfig();
|
|
135
|
-
if (!resolved.authUrl || !resolved.tokenUrl) {
|
|
136
|
-
throw new Error(
|
|
137
|
-
"External auth: authorization and token URLs are required for browser login"
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return this.buildOAuthClient(resolved).exchangeCodeForToken(
|
|
142
|
-
code,
|
|
143
|
-
codeVerifier,
|
|
144
|
-
redirectUri
|
|
145
|
-
);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
async fetchUserInfo(accessToken: string): Promise<UserInfoResponse> {
|
|
149
|
-
const resolved = await this.resolveConfig();
|
|
150
|
-
if (!resolved.userinfoUrl) {
|
|
151
|
-
throw new Error(
|
|
152
|
-
"External auth: userinfo endpoint not available (expose it via OIDC discovery)"
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const response = await fetch(resolved.userinfoUrl, {
|
|
157
|
-
headers: {
|
|
158
|
-
Authorization: `Bearer ${accessToken}`,
|
|
159
|
-
Accept: "application/json",
|
|
160
|
-
},
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
if (!response.ok) {
|
|
164
|
-
const errorText = await response.text();
|
|
165
|
-
throw new Error(
|
|
166
|
-
`Failed to fetch user info: ${response.status} ${errorText}`
|
|
167
|
-
);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const data = (await response.json()) as UserInfoResponse;
|
|
171
|
-
logger.info("Fetched external auth user info", {
|
|
172
|
-
sub: data.sub,
|
|
173
|
-
email: data.email,
|
|
174
|
-
});
|
|
175
|
-
return data;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
async getCapabilities(): Promise<ExternalAuthCapabilities> {
|
|
179
|
-
const resolved = await this.resolveConfig();
|
|
180
|
-
return {
|
|
181
|
-
browser: !!(resolved.authUrl && resolved.tokenUrl),
|
|
182
|
-
device: !!(resolved.deviceAuthorizationUrl && resolved.tokenUrl),
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
async startDeviceAuthorization(): Promise<DeviceAuthorizationStartResult> {
|
|
187
|
-
const resolved = await this.resolveConfig();
|
|
188
|
-
if (!resolved.deviceAuthorizationUrl || !resolved.tokenUrl) {
|
|
189
|
-
throw new Error("External auth: device authorization is not supported");
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
try {
|
|
193
|
-
return await this.buildDeviceCodeClient(resolved).requestDeviceCode();
|
|
194
|
-
} catch (error) {
|
|
195
|
-
if (
|
|
196
|
-
this.config.clientId &&
|
|
197
|
-
error instanceof Error &&
|
|
198
|
-
error.message.includes("invalid_client")
|
|
199
|
-
) {
|
|
200
|
-
logger.warn(
|
|
201
|
-
"Static external auth client was rejected for device flow, retrying with dynamic registration"
|
|
202
|
-
);
|
|
203
|
-
const dynamicResolved = await this.resolveConfig({
|
|
204
|
-
forceDynamicClient: true,
|
|
205
|
-
});
|
|
206
|
-
return this.buildDeviceCodeClient(dynamicResolved).requestDeviceCode();
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
throw error;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
async pollDeviceAuthorization(
|
|
214
|
-
deviceAuthId: string,
|
|
215
|
-
intervalSeconds?: number
|
|
216
|
-
): Promise<ExternalDeviceAuthorizationPollResult> {
|
|
217
|
-
const resolved = await this.resolveConfig();
|
|
218
|
-
if (!resolved.deviceAuthorizationUrl || !resolved.tokenUrl) {
|
|
219
|
-
throw new Error("External auth: device authorization is not supported");
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
const result = await this.buildDeviceCodeClient(resolved).pollForToken(
|
|
223
|
-
deviceAuthId,
|
|
224
|
-
intervalSeconds
|
|
225
|
-
);
|
|
226
|
-
if (result.status !== "complete") {
|
|
227
|
-
return result;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
const user = resolved.userinfoUrl
|
|
231
|
-
? await this.fetchUserInfo(result.credentials.accessToken)
|
|
232
|
-
: undefined;
|
|
233
|
-
|
|
234
|
-
return {
|
|
235
|
-
...result,
|
|
236
|
-
user,
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
static isConfigured(): boolean {
|
|
241
|
-
return !!process.env.MEMORY_URL;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
static fromEnv(
|
|
245
|
-
publicGatewayUrl: string,
|
|
246
|
-
cacheStore?: ExternalAuthConfig["cacheStore"]
|
|
247
|
-
): ExternalAuthClient | null {
|
|
248
|
-
const authMcpUrl = process.env.MEMORY_URL;
|
|
249
|
-
if (!authMcpUrl) return null;
|
|
250
|
-
|
|
251
|
-
const issuerUrl = authMcpUrl.replace(/\/+$/, "");
|
|
252
|
-
const callbackPath = "/connect/oauth/callback";
|
|
253
|
-
|
|
254
|
-
// Register redirect URIs for both the configured public URL and localhost
|
|
255
|
-
// so OAuth works regardless of how the user accesses the gateway
|
|
256
|
-
const redirectUri = `${publicGatewayUrl}${callbackPath}`;
|
|
257
|
-
const additionalRedirectUris = [
|
|
258
|
-
`http://localhost:8080${callbackPath}`,
|
|
259
|
-
].filter((uri) => uri !== redirectUri);
|
|
260
|
-
|
|
261
|
-
return new ExternalAuthClient({
|
|
262
|
-
issuerUrl,
|
|
263
|
-
redirectUri,
|
|
264
|
-
additionalRedirectUris,
|
|
265
|
-
scope: DEFAULT_SCOPE,
|
|
266
|
-
cacheStore,
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
private async resolveConfig(options?: {
|
|
271
|
-
forceDynamicClient?: boolean;
|
|
272
|
-
}): Promise<ResolvedExternalAuthConfig> {
|
|
273
|
-
const metadata = await this.discoverMetadata();
|
|
274
|
-
const dynamicCredentials = await this.getDynamicClientCredentials(
|
|
275
|
-
metadata,
|
|
276
|
-
{
|
|
277
|
-
forceRegistration: options?.forceDynamicClient,
|
|
278
|
-
}
|
|
279
|
-
);
|
|
280
|
-
|
|
281
|
-
const clientId = dynamicCredentials?.client_id || this.config.clientId;
|
|
282
|
-
const clientSecret =
|
|
283
|
-
dynamicCredentials?.client_secret || this.config.clientSecret;
|
|
284
|
-
|
|
285
|
-
if (!clientId) {
|
|
286
|
-
throw new Error(
|
|
287
|
-
"External auth: client registration failed and no static client ID is configured"
|
|
288
|
-
);
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
const authMethods = metadata?.token_endpoint_auth_methods_supported;
|
|
292
|
-
const tokenEndpointAuthMethod =
|
|
293
|
-
dynamicCredentials?.token_endpoint_auth_method ||
|
|
294
|
-
this.selectTokenEndpointAuthMethod(authMethods, clientSecret);
|
|
295
|
-
|
|
296
|
-
return {
|
|
297
|
-
clientId,
|
|
298
|
-
clientSecret,
|
|
299
|
-
authUrl: this.config.authorizeUrl || metadata?.authorization_endpoint,
|
|
300
|
-
tokenUrl: this.config.tokenUrl || metadata?.token_endpoint,
|
|
301
|
-
userinfoUrl: this.config.userinfoUrl || metadata?.userinfo_endpoint,
|
|
302
|
-
deviceAuthorizationUrl:
|
|
303
|
-
this.config.deviceAuthorizationUrl ||
|
|
304
|
-
metadata?.device_authorization_endpoint,
|
|
305
|
-
grantTypesSupported: metadata?.grant_types_supported || [],
|
|
306
|
-
tokenEndpointAuthMethod,
|
|
307
|
-
};
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
private buildOAuthClient(resolved: ResolvedExternalAuthConfig): OAuthClient {
|
|
311
|
-
const providerConfig: OAuthProviderConfig = {
|
|
312
|
-
id: "external-auth",
|
|
313
|
-
name: "External Auth",
|
|
314
|
-
clientId: resolved.clientId,
|
|
315
|
-
clientSecret: resolved.clientSecret,
|
|
316
|
-
authUrl: resolved.authUrl!,
|
|
317
|
-
tokenUrl: resolved.tokenUrl!,
|
|
318
|
-
redirectUri: this.config.redirectUri,
|
|
319
|
-
scope: this.config.scope || DEFAULT_SCOPE,
|
|
320
|
-
usePKCE: true,
|
|
321
|
-
responseType: "code",
|
|
322
|
-
grantType: "authorization_code",
|
|
323
|
-
tokenEndpointAuthMethod: resolved.tokenEndpointAuthMethod,
|
|
324
|
-
requireRefreshToken: false,
|
|
325
|
-
};
|
|
326
|
-
|
|
327
|
-
return new OAuthClient(providerConfig);
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
private buildDeviceCodeClient(
|
|
331
|
-
resolved: ResolvedExternalAuthConfig
|
|
332
|
-
): GenericDeviceCodeClient {
|
|
333
|
-
return new GenericDeviceCodeClient({
|
|
334
|
-
clientId: resolved.clientId,
|
|
335
|
-
clientSecret: resolved.clientSecret,
|
|
336
|
-
tokenUrl: resolved.tokenUrl!,
|
|
337
|
-
deviceAuthorizationUrl: resolved.deviceAuthorizationUrl!,
|
|
338
|
-
scope: this.config.scope || DEFAULT_SCOPE,
|
|
339
|
-
tokenEndpointAuthMethod: resolved.tokenEndpointAuthMethod,
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
private async discoverMetadata(): Promise<WellKnownMetadata | null> {
|
|
344
|
-
if (
|
|
345
|
-
this.discoveryCache &&
|
|
346
|
-
Date.now() - this.discoveryCache.resolvedAt < DISCOVERY_CACHE_TTL_MS
|
|
347
|
-
) {
|
|
348
|
-
return this.discoveryCache.metadata;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
const discoveryUrls = this.getDiscoveryUrls();
|
|
352
|
-
|
|
353
|
-
for (const wellKnownUrl of discoveryUrls) {
|
|
354
|
-
try {
|
|
355
|
-
logger.info(`Discovering external auth endpoints from ${wellKnownUrl}`);
|
|
356
|
-
const response = await fetch(wellKnownUrl);
|
|
357
|
-
if (!response.ok) {
|
|
358
|
-
logger.warn(
|
|
359
|
-
`Failed to fetch external auth metadata from ${wellKnownUrl}: ${response.status}`
|
|
360
|
-
);
|
|
361
|
-
continue;
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
const metadata = (await response.json()) as WellKnownMetadata;
|
|
365
|
-
logger.info("Discovered external auth endpoints", {
|
|
366
|
-
discoveryUrl: wellKnownUrl,
|
|
367
|
-
authUrl: this.config.authorizeUrl || metadata.authorization_endpoint,
|
|
368
|
-
tokenUrl: this.config.tokenUrl || metadata.token_endpoint,
|
|
369
|
-
userinfoUrl:
|
|
370
|
-
this.config.userinfoUrl || metadata.userinfo_endpoint || null,
|
|
371
|
-
deviceAuthorizationUrl:
|
|
372
|
-
this.config.deviceAuthorizationUrl ||
|
|
373
|
-
metadata.device_authorization_endpoint ||
|
|
374
|
-
null,
|
|
375
|
-
registrationEndpoint: metadata.registration_endpoint || null,
|
|
376
|
-
});
|
|
377
|
-
this.discoveryCache = { metadata, resolvedAt: Date.now() };
|
|
378
|
-
return metadata;
|
|
379
|
-
} catch (error) {
|
|
380
|
-
logger.warn("Failed to discover external auth endpoints", {
|
|
381
|
-
discoveryUrl: wellKnownUrl,
|
|
382
|
-
error,
|
|
383
|
-
});
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
this.discoveryCache = { metadata: null, resolvedAt: Date.now() };
|
|
388
|
-
return null;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
private getDiscoveryUrls(): string[] {
|
|
392
|
-
const trimmedIssuerUrl = this.config.issuerUrl.replace(/\/+$/, "");
|
|
393
|
-
const candidates = [`${trimmedIssuerUrl}/.well-known/openid-configuration`];
|
|
394
|
-
|
|
395
|
-
try {
|
|
396
|
-
const origin = new URL(trimmedIssuerUrl).origin;
|
|
397
|
-
const rootDiscoveryUrl = `${origin}/.well-known/openid-configuration`;
|
|
398
|
-
if (!candidates.includes(rootDiscoveryUrl)) {
|
|
399
|
-
candidates.push(rootDiscoveryUrl);
|
|
400
|
-
}
|
|
401
|
-
} catch {
|
|
402
|
-
// Ignore invalid issuer URLs here; fetch will surface the real error.
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
return candidates;
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
private async getDynamicClientCredentials(
|
|
409
|
-
metadata: WellKnownMetadata | null,
|
|
410
|
-
options?: { forceRegistration?: boolean }
|
|
411
|
-
): Promise<DynamicClientCredentials | null> {
|
|
412
|
-
if (!options?.forceRegistration) {
|
|
413
|
-
const cached = await this.getCachedClientCredentials();
|
|
414
|
-
if (cached) {
|
|
415
|
-
return cached;
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
if (!metadata?.registration_endpoint) {
|
|
420
|
-
return null;
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
if (!options?.forceRegistration && this.config.clientId) {
|
|
424
|
-
return null;
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
try {
|
|
428
|
-
const requestedAuthMethod = this.selectTokenEndpointAuthMethod(
|
|
429
|
-
metadata.token_endpoint_auth_methods_supported,
|
|
430
|
-
undefined
|
|
431
|
-
);
|
|
432
|
-
const supportsDeviceGrant =
|
|
433
|
-
!!metadata.device_authorization_endpoint ||
|
|
434
|
-
metadata.grant_types_supported?.includes(DEVICE_CODE_GRANT_TYPE);
|
|
435
|
-
|
|
436
|
-
logger.info("Registering external auth client dynamically", {
|
|
437
|
-
registrationEndpoint: metadata.registration_endpoint,
|
|
438
|
-
requestedAuthMethod,
|
|
439
|
-
supportsDeviceGrant,
|
|
440
|
-
});
|
|
441
|
-
|
|
442
|
-
const response = await fetch(metadata.registration_endpoint, {
|
|
443
|
-
method: "POST",
|
|
444
|
-
headers: {
|
|
445
|
-
Accept: "application/json",
|
|
446
|
-
"Content-Type": "application/json",
|
|
447
|
-
},
|
|
448
|
-
body: JSON.stringify({
|
|
449
|
-
client_name: "Lobu CLI and Settings",
|
|
450
|
-
redirect_uris: [
|
|
451
|
-
this.config.redirectUri,
|
|
452
|
-
...(this.config.additionalRedirectUris || []),
|
|
453
|
-
].filter((v, i, a) => a.indexOf(v) === i),
|
|
454
|
-
grant_types: supportsDeviceGrant
|
|
455
|
-
? ["authorization_code", "refresh_token", DEVICE_CODE_GRANT_TYPE]
|
|
456
|
-
: ["authorization_code", "refresh_token"],
|
|
457
|
-
response_types: ["code"],
|
|
458
|
-
token_endpoint_auth_method: requestedAuthMethod,
|
|
459
|
-
}),
|
|
460
|
-
});
|
|
461
|
-
|
|
462
|
-
if (!response.ok) {
|
|
463
|
-
const errorText = await response.text();
|
|
464
|
-
logger.warn("External auth client registration failed", {
|
|
465
|
-
status: response.status,
|
|
466
|
-
errorText,
|
|
467
|
-
});
|
|
468
|
-
return null;
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
const credentials = (await response.json()) as DynamicClientCredentials;
|
|
472
|
-
await this.cacheClientCredentials(credentials);
|
|
473
|
-
logger.info("External auth client registered", {
|
|
474
|
-
clientId: credentials.client_id,
|
|
475
|
-
tokenEndpointAuthMethod:
|
|
476
|
-
credentials.token_endpoint_auth_method || requestedAuthMethod,
|
|
477
|
-
});
|
|
478
|
-
return credentials;
|
|
479
|
-
} catch (error) {
|
|
480
|
-
logger.warn("External auth client registration failed", { error });
|
|
481
|
-
return null;
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
private async getCachedClientCredentials(): Promise<DynamicClientCredentials | null> {
|
|
486
|
-
if (!this.config.cacheStore) {
|
|
487
|
-
return null;
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
try {
|
|
491
|
-
const raw = await this.config.cacheStore.get(EXTERNAL_AUTH_CACHE_KEY);
|
|
492
|
-
if (!raw) {
|
|
493
|
-
return null;
|
|
494
|
-
}
|
|
495
|
-
return JSON.parse(raw) as DynamicClientCredentials;
|
|
496
|
-
} catch (error) {
|
|
497
|
-
logger.warn("Failed to load cached external auth client", { error });
|
|
498
|
-
return null;
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
private async cacheClientCredentials(
|
|
503
|
-
credentials: DynamicClientCredentials
|
|
504
|
-
): Promise<void> {
|
|
505
|
-
if (!this.config.cacheStore) {
|
|
506
|
-
return;
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
const ttlSeconds =
|
|
510
|
-
credentials.client_secret_expires_at &&
|
|
511
|
-
credentials.client_secret_expires_at > 0
|
|
512
|
-
? Math.max(
|
|
513
|
-
60,
|
|
514
|
-
Math.floor(credentials.client_secret_expires_at - Date.now() / 1000)
|
|
515
|
-
)
|
|
516
|
-
: 7 * 24 * 60 * 60;
|
|
517
|
-
|
|
518
|
-
try {
|
|
519
|
-
await this.config.cacheStore.set(
|
|
520
|
-
EXTERNAL_AUTH_CACHE_KEY,
|
|
521
|
-
JSON.stringify(credentials),
|
|
522
|
-
ttlSeconds
|
|
523
|
-
);
|
|
524
|
-
} catch (error) {
|
|
525
|
-
logger.warn("Failed to cache external auth client", { error });
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
private selectTokenEndpointAuthMethod(
|
|
530
|
-
supportedMethods: string[] | undefined,
|
|
531
|
-
clientSecret?: string
|
|
532
|
-
): "none" | "client_secret_post" | "client_secret_basic" {
|
|
533
|
-
const methods = new Set(supportedMethods || []);
|
|
534
|
-
|
|
535
|
-
if (!clientSecret) {
|
|
536
|
-
if (methods.size === 0 || methods.has("none")) {
|
|
537
|
-
return "none";
|
|
538
|
-
}
|
|
539
|
-
if (methods.has("client_secret_post")) {
|
|
540
|
-
return "client_secret_post";
|
|
541
|
-
}
|
|
542
|
-
if (methods.has("client_secret_basic")) {
|
|
543
|
-
return "client_secret_basic";
|
|
544
|
-
}
|
|
545
|
-
return "none";
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
if (methods.has("client_secret_post")) {
|
|
549
|
-
return "client_secret_post";
|
|
550
|
-
}
|
|
551
|
-
if (methods.has("client_secret_basic")) {
|
|
552
|
-
return "client_secret_basic";
|
|
553
|
-
}
|
|
554
|
-
if (methods.has("none")) {
|
|
555
|
-
return "none";
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
return "client_secret_post";
|
|
559
|
-
}
|
|
560
|
-
}
|