Files
deepseek-harness/packages/api/gateway/tests/browser-credentials.ts
T
imccyu 71753fdf91 refactor(connection): add connection auth provider for remote connection (#8266)
* refactor(connection): extract peer authentication providers

* test(connection): cover provider lifecycle and satisfy package gates

* fix(packaging): publish authentication provider locale resources

* test(auth): use runtime login output and cover gateway admission

* fix(auth): preserve Worker requests and migrate Desktop fixtures

* refactor(auth): centralize route admission with required policy

* fix(release): align authentication providers with rc.2

* refactor(connection): keep peer lookup and source policy private

* test(gateway): cover centralized upgrade rejections

* fix(ci): authenticate homepage compression probe

* refactor(auth): let Token own API socket lifecycle

* fix(auth): prevent caching admission refusals and clarify ownership

* refactor(web): collect provider entry URLs after readiness

* refactor(webserver): name the shared entry URL event

* refactor(connection): provide authentication as a nested service

* fix(web-app): restore startup publication defaults

* fix(auth): scope web events and preserve callback flows

* docs(auth): record the implemented provider architecture

* fix(auth): require owner access for administrative routes
2026-11-12 23:14:32 +08:00

53 lines
2.1 KiB
TypeScript

import type { Context } from '@deepseek-ai/cordis'
import type { IncomingMessage, ServerResponse } from 'node:http'
import * as Token from '@deepseek-ai/dsh-host-authentication-token'
import { BrowserAuth } from '@deepseek-ai/dsh-host-authentication-token/src/browser-auth.ts'
const browserCookies = new WeakMap<Context, string>()
/** Provide an in-memory credential-record owner for a mounted Connection plugin. */
export function provideBrowserCredentials(ctx: Context): void {
const records = new Map<unknown, unknown>()
ctx.provide('credentials', {
async modifyRecord(
key: unknown,
mutate: (current: unknown) => Promise<unknown>,
): Promise<unknown> {
const current = records.get(key)
const next = await mutate(current)
if (next !== undefined) records.set(key, next)
return next ?? current
},
} as never)
}
/**
* Exchange a test Host's process token for its WebSocket/HTTP Cookie header.
* @param ctx - Host root with the Web server and Connection mounted.
* @returns the `cookie` header value, cached per root.
*/
export async function browserCookie(ctx: Context): Promise<string> {
const existing = browserCookies.get(ctx)
if (existing !== undefined) return existing
await ctx.plugin(Token, { cookieMaxAgeDays: 30 }).await()
const auth = await BrowserAuth.create(ctx.root, ctx.credentials, 30)
const target = new URL(auth.authenticatedUrl(`http://127.0.0.1:${String(ctx.webServer.port)}`))
let setCookie: string | undefined
const request = {
method: 'GET',
url: `${target.pathname}${target.search}`,
headers: { host: target.host },
socket: { destroyed: false },
} as IncomingMessage
const response = {
writeHead(_status: number, headers?: Readonly<Record<string, string>>) { setCookie = headers?.['set-cookie'] },
end() {},
} as ServerResponse
await ctx.waterfall('webserver/request', request, response, async () => {})
if (setCookie === undefined) throw new Error('gateway fixture did not receive a browser cookie')
const cookie = setCookie.split(';', 1)[0]!
browserCookies.set(ctx, cookie)
ctx.effect(() => () => { browserCookies.delete(ctx) })
return cookie
}