fix: share integration transport and enforce access

This commit is contained in:
ajnart committed 2026-09-12 20:26:39 +02:00
1 parent 52c82f72db
commit c2a587bda7
16 files changed
+284 -117

No files matched your search

+1 -1
View File
@@ -12,7 +12,7 @@
}
},
"exports": {
"./custom-widget-source": "./src/custom-widget-source.ts",
"./http-auth": "./src/http-auth.ts",
".": "./index.ts",
"./anchor": "./src/anchor/anchor-types.ts",
"./downloads": "./src/interfaces/downloads/download-client-items.ts",
@@ -1,18 +0,0 @@
import type { IntegrationInput } from "./base/integration";
/** Only integrations with a supported arbitrary HTTP authentication contract belong here. */
export function getCustomWidgetIntegrationConnection(integration: IntegrationInput) {
switch (integration.kind) {
case "sonarr":
case "radarr": {
const apiKey = integration.decryptedSecrets.find((secret) => secret.kind === "apiKey");
if (!apiKey?.value) throw new Error("Integration API key is not configured");
return {
baseUrl: integration.url,
auth: { type: "apiKeyHeader", headerName: "X-Api-Key", secrets: [apiKey] },
};
}
default:
throw new Error("Integration does not support custom widget requests");
}
}
+56
View File
@@ -0,0 +1,56 @@
import type { IntegrationKind } from "@homarr/definitions";
export interface IntegrationHttpAuth {
type: "apiKeyHeader" | "bearer" | "basic";
secrets: Array<{ kind: string; value: string }>;
}
export class IntegrationHttpAuthError extends Error {
readonly code: "BAD_REQUEST" | "PRECONDITION_FAILED";
constructor(input: { code: "BAD_REQUEST" | "PRECONDITION_FAILED"; message: string }) {
super(input.message);
this.code = input.code;
}
}
// Match the existing adapters; never guess authentication from secret names alone.
export const integrationHttpAuth = {
sonarr: "apiKeyHeader",
radarr: "apiKeyHeader",
lidarr: "apiKeyHeader",
readarr: "apiKeyHeader",
prowlarr: "apiKeyHeader",
overseerr: "apiKeyHeader",
jellyseerr: "apiKeyHeader",
seerr: "apiKeyHeader",
immich: "apiKeyHeader",
slskd: "apiKeyHeader",
homeAssistant: "bearer",
coolify: "bearer",
audiobookshelf: "bearer",
speedtestTracker: "bearer",
adGuardHome: "basic",
nextcloud: "basic",
} as const satisfies Partial<Record<IntegrationKind, string>>;
export function resolveIntegrationHttpAuth(
kind: IntegrationKind,
secrets: IntegrationHttpAuth["secrets"],
): IntegrationHttpAuth {
if (!(kind in integrationHttpAuth)) {
throw new IntegrationHttpAuthError({
code: "BAD_REQUEST",
message: "This integration's authentication is not supported by HTTP requests",
});
}
const type = integrationHttpAuth[kind as keyof typeof integrationHttpAuth];
let required = ["apiKey"];
if (type === "basic") required = ["username", "password"];
if (required.some((key) => !secrets.some((secret) => secret.kind === key && secret.value))) {
throw new IntegrationHttpAuthError({
code: "PRECONDITION_FAILED",
message: "Integration credentials are incomplete",
});
}
return { type, secrets };
}