fix: align workbench cloud auth validation
This commit is contained in:
@@ -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}`;
|
||||
}
|
||||
Reference in New Issue
Block a user