@huaqiu/dsh-auth 0.5.7 → 0.5.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/lib/index.d.mts +50 -1
- package/lib/index.mjs +128 -1
- package/package.json +3 -2
- package/src/apiKeySync.ts +136 -0
- package/src/index.ts +41 -1
package/lib/index.d.mts
CHANGED
|
@@ -226,6 +226,55 @@ declare const AUTH_ROUTE_PREFIX = "/api/v1/huaqiu/auth";
|
|
|
226
226
|
type AuthHandler = (req: IncomingMessage, res: ServerResponse) => Promise<void> | void;
|
|
227
227
|
declare function createAuthHandler(service: HuaqiuAuthService): AuthHandler;
|
|
228
228
|
//#endregion
|
|
229
|
+
//#region src/apiKeySync.d.ts
|
|
230
|
+
/** Credential reference the `huaqiu` provider resolves per request. */
|
|
231
|
+
declare const HUAQIU_API_KEY_REF = "HUAQIU_API_KEY";
|
|
232
|
+
/** Default poll interval: 5 s, matching the DSH browser half's host refresh. */
|
|
233
|
+
declare const DEFAULT_HUAQIU_API_KEY_SYNC_MS = 5000;
|
|
234
|
+
/** Minimal writable credential store (the DSH `ctx.credentials` seam). */
|
|
235
|
+
interface CredentialStore {
|
|
236
|
+
set(ref: string, value: string): Promise<void>;
|
|
237
|
+
unset(ref: string): Promise<void>;
|
|
238
|
+
}
|
|
239
|
+
/** Logger surface used by the sync (kept small so tests can pass a spy). */
|
|
240
|
+
interface SyncLogger {
|
|
241
|
+
debug(msg: string, ctx?: Record<string, unknown>): void;
|
|
242
|
+
info(msg: string, ctx?: Record<string, unknown>): void;
|
|
243
|
+
warn(msg: string, ctx?: Record<string, unknown>): void;
|
|
244
|
+
}
|
|
245
|
+
declare class HuaqiuApiKeySync {
|
|
246
|
+
/** HQ Edge base URL ("" → disabled, matches HostSessionResolver.enabled). */
|
|
247
|
+
readonly baseUrl: string;
|
|
248
|
+
/** Token path on the host; default "/api/v1/auth/token". */
|
|
249
|
+
readonly path: string;
|
|
250
|
+
private readonly credentials;
|
|
251
|
+
private readonly log;
|
|
252
|
+
private readonly intervalMs;
|
|
253
|
+
private readonly doFetch;
|
|
254
|
+
private timer;
|
|
255
|
+
/** Guards the poll timer so overlapping rounds can never run (task §5.4). */
|
|
256
|
+
private inFlight;
|
|
257
|
+
/** Last token mirrored into the credential store (change detection). */
|
|
258
|
+
private lastToken;
|
|
259
|
+
constructor(
|
|
260
|
+
/** HQ Edge base URL ("" → disabled, matches HostSessionResolver.enabled). */
|
|
261
|
+
baseUrl: string,
|
|
262
|
+
/** Token path on the host; default "/api/v1/auth/token". */
|
|
263
|
+
path: string, credentials: CredentialStore, log: SyncLogger, intervalMs?: number, doFetch?: typeof fetch);
|
|
264
|
+
/** Host-mode sync is active iff a base URL was configured. */
|
|
265
|
+
get enabled(): boolean;
|
|
266
|
+
/** Kick off an immediate sync, then poll on a non-overlapping interval. */
|
|
267
|
+
start(): void;
|
|
268
|
+
/** Stop the poll timer. Safe to call more than once. */
|
|
269
|
+
dispose(): void;
|
|
270
|
+
/**
|
|
271
|
+
* One sync round: fetch the host token endpoint and mirror the verdict into
|
|
272
|
+
* the credential store. Exported separately from `start()` so tests can run
|
|
273
|
+
* rounds deterministically without timers.
|
|
274
|
+
*/
|
|
275
|
+
syncNow(): Promise<void>;
|
|
276
|
+
}
|
|
277
|
+
//#endregion
|
|
229
278
|
//#region src/index.d.ts
|
|
230
279
|
declare const name = "@huaqiu/dsh-auth";
|
|
231
280
|
declare const inject: readonly ["webServer"];
|
|
@@ -244,4 +293,4 @@ declare module '@deepseek-ai/cordis' {
|
|
|
244
293
|
*/
|
|
245
294
|
declare function apply(ctx: Context, config?: Partial<HuaqiuAuthConfig>): void;
|
|
246
295
|
//#endregion
|
|
247
|
-
export { AUTH_ROUTE_PREFIX, type AuthHandler, type HuaqiuAuthApi, type HuaqiuAuthConfig, type HuaqiuAuthService, type HuaqiuUserInfo, InMemoryHuaqiuAuthService, apply, createAuthHandler, inject, name };
|
|
296
|
+
export { AUTH_ROUTE_PREFIX, type AuthHandler, type CredentialStore, DEFAULT_HUAQIU_API_KEY_SYNC_MS, HUAQIU_API_KEY_REF, HuaqiuApiKeySync, type HuaqiuAuthApi, type HuaqiuAuthConfig, type HuaqiuAuthService, type HuaqiuUserInfo, InMemoryHuaqiuAuthService, apply, createAuthHandler, inject, name };
|
package/lib/index.mjs
CHANGED
|
@@ -173,6 +173,120 @@ function normalizeHostUser(data) {
|
|
|
173
173
|
...expiresAt !== void 0 ? { expiresAt } : {}
|
|
174
174
|
};
|
|
175
175
|
}
|
|
176
|
+
//#endregion
|
|
177
|
+
//#region src/apiKeySync.ts
|
|
178
|
+
/**
|
|
179
|
+
* Huaqiu AI gateway credential sync (host mode).
|
|
180
|
+
*
|
|
181
|
+
* The bundled DSH's `huaqiu` provider resolves its api key through the
|
|
182
|
+
* `HUAQIU_API_KEY` credential reference (overlay `apiKeyEnv`). In host mode the
|
|
183
|
+
* authoritative credential lives in HQ Edge (mirroring the EDA/KiCad host), and
|
|
184
|
+
* Workstream A strips `HUAQIU_API_KEY` from the child environment — so this
|
|
185
|
+
* sync is the ONLY source of that credential (task huaqiu-ai-gateway §5.5).
|
|
186
|
+
*
|
|
187
|
+
* The sync watches the HQ Edge token endpoint directly with a per-call
|
|
188
|
+
* loopback GET — deliberately NOT `hqEdge.auth.getAccessToken()`, whose
|
|
189
|
+
* initial-null cache has no invalidation path — and mirrors the token into
|
|
190
|
+
* `ctx.credentials` under the `HUAQIU_API_KEY` reference:
|
|
191
|
+
*
|
|
192
|
+
* - authenticated host session → `set` (only when the token value changed);
|
|
193
|
+
* - explicit unauthenticated verdict (2xx + `authenticated: false`, or a 2xx
|
|
194
|
+
* payload without a usable credential) → `unset`;
|
|
195
|
+
* - transient network error / non-2xx host answer → warn and leave the
|
|
196
|
+
* current value untouched (a blip must never log the operator out).
|
|
197
|
+
*
|
|
198
|
+
* The same path serves both the `hq-eda` and `kicad` hosts — host identity is
|
|
199
|
+
* HQ Edge configuration, never this plugin's concern.
|
|
200
|
+
*/
|
|
201
|
+
/** Credential reference the `huaqiu` provider resolves per request. */
|
|
202
|
+
const HUAQIU_API_KEY_REF = "HUAQIU_API_KEY";
|
|
203
|
+
/** Default poll interval: 5 s, matching the DSH browser half's host refresh. */
|
|
204
|
+
const DEFAULT_HUAQIU_API_KEY_SYNC_MS = 5e3;
|
|
205
|
+
var HuaqiuApiKeySync = class {
|
|
206
|
+
baseUrl;
|
|
207
|
+
path;
|
|
208
|
+
credentials;
|
|
209
|
+
log;
|
|
210
|
+
intervalMs;
|
|
211
|
+
doFetch;
|
|
212
|
+
timer = null;
|
|
213
|
+
/** Guards the poll timer so overlapping rounds can never run (task §5.4). */
|
|
214
|
+
inFlight = false;
|
|
215
|
+
/** Last token mirrored into the credential store (change detection). */
|
|
216
|
+
lastToken = null;
|
|
217
|
+
constructor(baseUrl, path, credentials, log, intervalMs = DEFAULT_HUAQIU_API_KEY_SYNC_MS, doFetch = globalThis.fetch.bind(globalThis)) {
|
|
218
|
+
this.baseUrl = baseUrl;
|
|
219
|
+
this.path = path;
|
|
220
|
+
this.credentials = credentials;
|
|
221
|
+
this.log = log;
|
|
222
|
+
this.intervalMs = intervalMs;
|
|
223
|
+
this.doFetch = doFetch;
|
|
224
|
+
}
|
|
225
|
+
/** Host-mode sync is active iff a base URL was configured. */
|
|
226
|
+
get enabled() {
|
|
227
|
+
return this.baseUrl.length > 0;
|
|
228
|
+
}
|
|
229
|
+
/** Kick off an immediate sync, then poll on a non-overlapping interval. */
|
|
230
|
+
start() {
|
|
231
|
+
this.syncNow();
|
|
232
|
+
this.timer = setInterval(() => {
|
|
233
|
+
if (this.inFlight) return;
|
|
234
|
+
this.inFlight = true;
|
|
235
|
+
this.syncNow().finally(() => {
|
|
236
|
+
this.inFlight = false;
|
|
237
|
+
});
|
|
238
|
+
}, this.intervalMs);
|
|
239
|
+
if (typeof this.timer.unref === "function") this.timer.unref();
|
|
240
|
+
}
|
|
241
|
+
/** Stop the poll timer. Safe to call more than once. */
|
|
242
|
+
dispose() {
|
|
243
|
+
if (this.timer !== null) clearInterval(this.timer);
|
|
244
|
+
this.timer = null;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* One sync round: fetch the host token endpoint and mirror the verdict into
|
|
248
|
+
* the credential store. Exported separately from `start()` so tests can run
|
|
249
|
+
* rounds deterministically without timers.
|
|
250
|
+
*/
|
|
251
|
+
async syncNow() {
|
|
252
|
+
const url = `${this.baseUrl}${this.path}`;
|
|
253
|
+
let res = null;
|
|
254
|
+
let info = null;
|
|
255
|
+
try {
|
|
256
|
+
res = await this.doFetch(url, {
|
|
257
|
+
method: "GET",
|
|
258
|
+
headers: { accept: "application/json" }
|
|
259
|
+
});
|
|
260
|
+
if (res.ok) info = normalizeHostUser(await res.json());
|
|
261
|
+
} catch (err) {
|
|
262
|
+
this.log.warn("huaqiu api-key sync fetch failed", {
|
|
263
|
+
url,
|
|
264
|
+
error: String(err)
|
|
265
|
+
});
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (info !== null && info.authenticated) {
|
|
269
|
+
if (info.token !== this.lastToken) {
|
|
270
|
+
this.lastToken = info.token;
|
|
271
|
+
await this.credentials.set(HUAQIU_API_KEY_REF, info.token);
|
|
272
|
+
this.log.info("huaqiu api-key credential synced", { userId: info.id });
|
|
273
|
+
}
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
if (res !== null && res.ok) {
|
|
277
|
+
if (this.lastToken !== null) {
|
|
278
|
+
this.lastToken = null;
|
|
279
|
+
await this.credentials.unset(HUAQIU_API_KEY_REF);
|
|
280
|
+
this.log.info("huaqiu api-key credential cleared", { explicit: true });
|
|
281
|
+
}
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
this.log.warn("huaqiu api-key sync rejected by host", {
|
|
285
|
+
url,
|
|
286
|
+
status: res?.status ?? null
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
};
|
|
176
290
|
/**
|
|
177
291
|
* Classifies the HTTP status of the validation request.
|
|
178
292
|
*
|
|
@@ -755,10 +869,23 @@ function apply(ctx, config) {
|
|
|
755
869
|
path: AUTH_ROUTE_PREFIX,
|
|
756
870
|
handler: createAuthHandler(service)
|
|
757
871
|
}));
|
|
872
|
+
if (service.hostMode) ctx.effect(() => {
|
|
873
|
+
const resolved = resolveHostConfig(config);
|
|
874
|
+
const sync = new HuaqiuApiKeySync(resolved.hqEdgeBaseUrl ?? "", resolved.hostAuthPath ?? "/api/v1/auth/token", {
|
|
875
|
+
set: (ref, value) => ctx.credentials.set(ref, value),
|
|
876
|
+
unset: (ref) => ctx.credentials.unset(ref)
|
|
877
|
+
}, {
|
|
878
|
+
debug: (msg, ctx2) => log.debug(msg, ctx2),
|
|
879
|
+
info: (msg, ctx2) => log.info(msg, ctx2),
|
|
880
|
+
warn: (msg, ctx2) => log.warn(msg, ctx2)
|
|
881
|
+
});
|
|
882
|
+
sync.start();
|
|
883
|
+
return () => sync.dispose();
|
|
884
|
+
});
|
|
758
885
|
log.info("dsh-auth node half ready", {
|
|
759
886
|
routes: AUTH_ROUTE_PREFIX,
|
|
760
887
|
hostMode: service.hostMode
|
|
761
888
|
});
|
|
762
889
|
}
|
|
763
890
|
//#endregion
|
|
764
|
-
export { AUTH_ROUTE_PREFIX, InMemoryHuaqiuAuthService, apply, createAuthHandler, inject, name };
|
|
891
|
+
export { AUTH_ROUTE_PREFIX, DEFAULT_HUAQIU_API_KEY_SYNC_MS, HUAQIU_API_KEY_REF, HuaqiuApiKeySync, InMemoryHuaqiuAuthService, apply, createAuthHandler, inject, name };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@huaqiu/dsh-auth",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./lib/index.mjs",
|
|
6
6
|
"types": "./lib/index.d.mts",
|
|
@@ -28,11 +28,12 @@
|
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
30
|
"@deepseek-ai/cordis": "^4.0.1",
|
|
31
|
+
"@deepseek-ai/dsh-credentials": "^0.0.1-rc.1",
|
|
31
32
|
"@deepseek-ai/dsh-host-webserver": "^0.1.0-rc.0"
|
|
32
33
|
},
|
|
33
34
|
"dependencies": {
|
|
34
35
|
"@deepseek-ai/dsh-home-paths": "^0.1.0-rc.0",
|
|
35
|
-
"@huaqiu/dsh-plugin-log": "0.5.
|
|
36
|
+
"@huaqiu/dsh-plugin-log": "0.5.8"
|
|
36
37
|
},
|
|
37
38
|
"files": [
|
|
38
39
|
"lib",
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Huaqiu AI gateway credential sync (host mode).
|
|
3
|
+
*
|
|
4
|
+
* The bundled DSH's `huaqiu` provider resolves its api key through the
|
|
5
|
+
* `HUAQIU_API_KEY` credential reference (overlay `apiKeyEnv`). In host mode the
|
|
6
|
+
* authoritative credential lives in HQ Edge (mirroring the EDA/KiCad host), and
|
|
7
|
+
* Workstream A strips `HUAQIU_API_KEY` from the child environment — so this
|
|
8
|
+
* sync is the ONLY source of that credential (task huaqiu-ai-gateway §5.5).
|
|
9
|
+
*
|
|
10
|
+
* The sync watches the HQ Edge token endpoint directly with a per-call
|
|
11
|
+
* loopback GET — deliberately NOT `hqEdge.auth.getAccessToken()`, whose
|
|
12
|
+
* initial-null cache has no invalidation path — and mirrors the token into
|
|
13
|
+
* `ctx.credentials` under the `HUAQIU_API_KEY` reference:
|
|
14
|
+
*
|
|
15
|
+
* - authenticated host session → `set` (only when the token value changed);
|
|
16
|
+
* - explicit unauthenticated verdict (2xx + `authenticated: false`, or a 2xx
|
|
17
|
+
* payload without a usable credential) → `unset`;
|
|
18
|
+
* - transient network error / non-2xx host answer → warn and leave the
|
|
19
|
+
* current value untouched (a blip must never log the operator out).
|
|
20
|
+
*
|
|
21
|
+
* The same path serves both the `hq-eda` and `kicad` hosts — host identity is
|
|
22
|
+
* HQ Edge configuration, never this plugin's concern.
|
|
23
|
+
*/
|
|
24
|
+
import { normalizeHostUser, type ResolvedHostUser } from './host.js'
|
|
25
|
+
|
|
26
|
+
/** Credential reference the `huaqiu` provider resolves per request. */
|
|
27
|
+
export const HUAQIU_API_KEY_REF = 'HUAQIU_API_KEY'
|
|
28
|
+
|
|
29
|
+
/** Default poll interval: 5 s, matching the DSH browser half's host refresh. */
|
|
30
|
+
export const DEFAULT_HUAQIU_API_KEY_SYNC_MS = 5000
|
|
31
|
+
|
|
32
|
+
/** Minimal writable credential store (the DSH `ctx.credentials` seam). */
|
|
33
|
+
export interface CredentialStore {
|
|
34
|
+
set(ref: string, value: string): Promise<void>
|
|
35
|
+
unset(ref: string): Promise<void>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Logger surface used by the sync (kept small so tests can pass a spy). */
|
|
39
|
+
export interface SyncLogger {
|
|
40
|
+
debug(msg: string, ctx?: Record<string, unknown>): void
|
|
41
|
+
info(msg: string, ctx?: Record<string, unknown>): void
|
|
42
|
+
warn(msg: string, ctx?: Record<string, unknown>): void
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class HuaqiuApiKeySync {
|
|
46
|
+
private timer: NodeJS.Timeout | null = null
|
|
47
|
+
/** Guards the poll timer so overlapping rounds can never run (task §5.4). */
|
|
48
|
+
private inFlight = false
|
|
49
|
+
/** Last token mirrored into the credential store (change detection). */
|
|
50
|
+
private lastToken: string | null = null
|
|
51
|
+
|
|
52
|
+
constructor(
|
|
53
|
+
/** HQ Edge base URL ("" → disabled, matches HostSessionResolver.enabled). */
|
|
54
|
+
readonly baseUrl: string,
|
|
55
|
+
/** Token path on the host; default "/api/v1/auth/token". */
|
|
56
|
+
readonly path: string,
|
|
57
|
+
private readonly credentials: CredentialStore,
|
|
58
|
+
private readonly log: SyncLogger,
|
|
59
|
+
private readonly intervalMs: number = DEFAULT_HUAQIU_API_KEY_SYNC_MS,
|
|
60
|
+
private readonly doFetch: typeof fetch = globalThis.fetch.bind(globalThis),
|
|
61
|
+
) {}
|
|
62
|
+
|
|
63
|
+
/** Host-mode sync is active iff a base URL was configured. */
|
|
64
|
+
get enabled(): boolean {
|
|
65
|
+
return this.baseUrl.length > 0
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Kick off an immediate sync, then poll on a non-overlapping interval. */
|
|
69
|
+
start(): void {
|
|
70
|
+
void this.syncNow()
|
|
71
|
+
this.timer = setInterval(() => {
|
|
72
|
+
if (this.inFlight) return
|
|
73
|
+
this.inFlight = true
|
|
74
|
+
void this.syncNow().finally(() => {
|
|
75
|
+
this.inFlight = false
|
|
76
|
+
})
|
|
77
|
+
}, this.intervalMs)
|
|
78
|
+
// Never keep the DSH process alive just for this timer.
|
|
79
|
+
if (typeof this.timer.unref === 'function') this.timer.unref()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Stop the poll timer. Safe to call more than once. */
|
|
83
|
+
dispose(): void {
|
|
84
|
+
if (this.timer !== null) clearInterval(this.timer)
|
|
85
|
+
this.timer = null
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* One sync round: fetch the host token endpoint and mirror the verdict into
|
|
90
|
+
* the credential store. Exported separately from `start()` so tests can run
|
|
91
|
+
* rounds deterministically without timers.
|
|
92
|
+
*/
|
|
93
|
+
async syncNow(): Promise<void> {
|
|
94
|
+
const url = `${this.baseUrl}${this.path}`
|
|
95
|
+
let res: Response | null = null
|
|
96
|
+
let info: ResolvedHostUser | null = null
|
|
97
|
+
try {
|
|
98
|
+
res = await this.doFetch(url, {
|
|
99
|
+
method: 'GET',
|
|
100
|
+
headers: { accept: 'application/json' },
|
|
101
|
+
})
|
|
102
|
+
if (res.ok) {
|
|
103
|
+
info = normalizeHostUser(await res.json() as Record<string, unknown>)
|
|
104
|
+
}
|
|
105
|
+
} catch (err) {
|
|
106
|
+
// Transient network error: never unset — a blip must not log the
|
|
107
|
+
// operator out (task §5.4).
|
|
108
|
+
this.log.warn('huaqiu api-key sync fetch failed', { url, error: String(err) })
|
|
109
|
+
return
|
|
110
|
+
}
|
|
111
|
+
if (info !== null && info.authenticated) {
|
|
112
|
+
if (info.token !== this.lastToken) {
|
|
113
|
+
this.lastToken = info.token
|
|
114
|
+
await this.credentials.set(HUAQIU_API_KEY_REF, info.token)
|
|
115
|
+
this.log.info('huaqiu api-key credential synced', { userId: info.id })
|
|
116
|
+
}
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
// A 2xx answer without a usable authenticated credential is the HOST'S
|
|
120
|
+
// explicit verdict (logout / not-yet-logged-in / cleared token) — mirror
|
|
121
|
+
// the cleared state. Anything else (non-2xx) is a host-side error: warn
|
|
122
|
+
// and keep the current value.
|
|
123
|
+
if (res !== null && res.ok) {
|
|
124
|
+
if (this.lastToken !== null) {
|
|
125
|
+
this.lastToken = null
|
|
126
|
+
await this.credentials.unset(HUAQIU_API_KEY_REF)
|
|
127
|
+
this.log.info('huaqiu api-key credential cleared', { explicit: true })
|
|
128
|
+
}
|
|
129
|
+
return
|
|
130
|
+
}
|
|
131
|
+
this.log.warn('huaqiu api-key sync rejected by host', {
|
|
132
|
+
url,
|
|
133
|
+
status: res?.status ?? null,
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -5,17 +5,29 @@
|
|
|
5
5
|
* mounts the browser→node credential routes on `ctx.webServer`.
|
|
6
6
|
*/
|
|
7
7
|
import type { Context } from '@deepseek-ai/cordis'
|
|
8
|
+
import type { CredentialRef } from '@deepseek-ai/dsh-credentials'
|
|
8
9
|
import type {} from '@deepseek-ai/dsh-host-webserver'
|
|
9
10
|
import { getLogger } from '@huaqiu/dsh-plugin-log'
|
|
11
|
+
import {
|
|
12
|
+
DEFAULT_HOST_AUTH_PATH,
|
|
13
|
+
resolveHostConfig,
|
|
14
|
+
type HuaqiuAuthConfig,
|
|
15
|
+
} from './host.js'
|
|
16
|
+
import { HuaqiuApiKeySync, HUAQIU_API_KEY_REF } from './apiKeySync.js'
|
|
10
17
|
import { InMemoryHuaqiuAuthService, type HuaqiuAuthService } from './service.js'
|
|
11
18
|
import { AUTH_ROUTE_PREFIX, createAuthHandler } from './routes.js'
|
|
12
|
-
import type { HuaqiuAuthConfig } from './host.js'
|
|
13
19
|
|
|
14
20
|
const log = getLogger('dsh-auth')
|
|
15
21
|
|
|
16
22
|
export type { HuaqiuAuthApi, HuaqiuAuthService, HuaqiuUserInfo } from './service.js'
|
|
17
23
|
export { InMemoryHuaqiuAuthService } from './service.js'
|
|
18
24
|
export { AUTH_ROUTE_PREFIX, createAuthHandler, type AuthHandler } from './routes.js'
|
|
25
|
+
export {
|
|
26
|
+
DEFAULT_HUAQIU_API_KEY_SYNC_MS,
|
|
27
|
+
HuaqiuApiKeySync,
|
|
28
|
+
HUAQIU_API_KEY_REF,
|
|
29
|
+
type CredentialStore,
|
|
30
|
+
} from './apiKeySync.js'
|
|
19
31
|
export type { HuaqiuAuthConfig } from './host.js'
|
|
20
32
|
|
|
21
33
|
export const name = '@huaqiu/dsh-auth'
|
|
@@ -55,5 +67,33 @@ export function apply(ctx: Context, config?: Partial<HuaqiuAuthConfig>): void {
|
|
|
55
67
|
handler: createAuthHandler(service),
|
|
56
68
|
}))
|
|
57
69
|
|
|
70
|
+
// Host mode: mirror the operator token into the DSH credential reference
|
|
71
|
+
// `HUAQIU_API_KEY` so the bundled `huaqiu` AI provider can resolve it per
|
|
72
|
+
// request (task huaqiu-ai-gateway §5). The sync re-fetches the loopback
|
|
73
|
+
// token endpoint on an immediate round plus a 5 s non-overlapping poll;
|
|
74
|
+
// it is host-scoped only — standalone DSH keeps its browser-login flow and
|
|
75
|
+
// the reference stays unset (missing credential → explicit
|
|
76
|
+
// MISSING_CREDENTIAL, never a fallback key).
|
|
77
|
+
if (service.hostMode) {
|
|
78
|
+
ctx.effect(() => {
|
|
79
|
+
const resolved = resolveHostConfig(config)
|
|
80
|
+
const sync = new HuaqiuApiKeySync(
|
|
81
|
+
resolved.hqEdgeBaseUrl ?? '',
|
|
82
|
+
resolved.hostAuthPath ?? DEFAULT_HOST_AUTH_PATH,
|
|
83
|
+
{
|
|
84
|
+
set: (ref, value) => ctx.credentials.set(ref as CredentialRef, value),
|
|
85
|
+
unset: (ref) => ctx.credentials.unset(ref as CredentialRef),
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
debug: (msg, ctx2) => log.debug(msg, ctx2),
|
|
89
|
+
info: (msg, ctx2) => log.info(msg, ctx2),
|
|
90
|
+
warn: (msg, ctx2) => log.warn(msg, ctx2),
|
|
91
|
+
},
|
|
92
|
+
)
|
|
93
|
+
sync.start()
|
|
94
|
+
return () => sync.dispose()
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
|
|
58
98
|
log.info('dsh-auth node half ready', { routes: AUTH_ROUTE_PREFIX, hostMode: service.hostMode })
|
|
59
99
|
}
|