UNPKG

n8n

Version:

n8n Workflow Automation Tool

62 lines 2.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveAllowedDomains = resolveAllowedDomains; exports.createAuthFetch = createAuthFetch; const ai_utilities_1 = require("@n8n/ai-utilities"); const n8n_workflow_1 = require("n8n-workflow"); function headersToRecord(headers) { if (!headers) return {}; if (headers instanceof Headers) return Object.fromEntries(headers.entries()); if (Array.isArray(headers)) return Object.fromEntries(headers); return headers; } function resolveAllowedDomains(credentialData) { const mode = credentialData.allowedHttpRequestDomains; if (mode === 'none') return { mode: 'none' }; if (mode === 'domains') { const domains = typeof credentialData.allowedDomains === 'string' ? credentialData.allowedDomains : ''; return { mode: 'domains', domains }; } return undefined; } function assertDomainPolicyAllowsUrl(url, policy) { if (policy.mode === 'none') { throw new n8n_workflow_1.UserError('Credential is configured to block all outbound requests'); } if (policy.domains.trim().length === 0) { throw new n8n_workflow_1.UserError('Credential restricts requests to specific domains but none are configured'); } (0, n8n_workflow_1.assertUrlAllowed)({ url, allowedDomains: policy.domains }); } function createAuthFetch({ baseFetch, initialHeaders, onUnauthorized, allowedDomains, }) { let headers = initialHeaders; const authedFetch = async (input, init) => { const response = await baseFetch(input, { ...init, headers: { ...headersToRecord(init?.headers), ...headers }, }); if (response.status !== 401 || !onUnauthorized) return response; const refreshed = await onUnauthorized(); if (!refreshed) return response; headers = refreshed; return await baseFetch(input, { ...init, headers: { ...headersToRecord(init?.headers), ...headers }, }); }; if (!allowedDomains) return authedFetch; return async (input, init) => { const startUrl = input instanceof Request ? input.url : input; return await (0, ai_utilities_1.fetchFollowingRedirects)(authedFetch, startUrl, init, { onBeforeHop: (hopUrl) => assertDomainPolicyAllowsUrl(hopUrl, allowedDomains), }); }; } //# sourceMappingURL=auth-fetch.js.map