fix: align workbench cloud auth validation

This commit is contained in:
root
2026-07-18 15:01:18 +02:00
parent 635b008077
commit f6357811b0
4 changed files with 17 additions and 4 deletions
+3 -3
View File
@@ -1,4 +1,5 @@
import type { WorkbenchApplication } from "./contracts.ts";
import { normalizeWorkbenchCloudAuthorization } from "./cloud-authorization.ts";
export function createCloudWorkbenchApplication(options: {
baseUrl: string;
@@ -6,15 +7,14 @@ export function createCloudWorkbenchApplication(options: {
fetchImpl?: typeof fetch;
}): WorkbenchApplication {
if (!options.baseUrl) throw codedError("workbench_cloud_api_url_required", "WORKBENCH_CLOUD_API_URL is required");
const authorization = options.authorization.trim();
const authorization = normalizeWorkbenchCloudAuthorization(options.authorization);
if (!authorization) throw codedError("workbench_cloud_api_auth_required", "WORKBENCH_CLOUD_API_AUTHORIZATION is required");
const authorizationHeader = /^Bearer\s+/iu.test(authorization) ? authorization : `Bearer ${authorization}`;
const fetchImpl = options.fetchImpl ?? fetch;
const request = async (method: string, route: string, body: Record<string, unknown> | undefined, actor: { id: string; role?: string | null }) => {
const response = await fetchImpl(`${options.baseUrl.replace(/\/$/u, "")}${route}`, {
method,
headers: {
authorization: authorizationHeader,
authorization,
"content-type": "application/json",
"x-workbench-activity-dispatch": "1",
"x-hwlab-actor-id": actor.id,
@@ -0,0 +1,5 @@
export function normalizeWorkbenchCloudAuthorization(value: string) {
const authorization = value.trim();
if (!authorization) return "";
return /^Bearer\s+/iu.test(authorization) ? authorization : `Bearer ${authorization}`;
}