n8n/packages/@n8n/nodes-langchain/nodes/trigger/ChatTrigger/GenericFunctions.ts
n8n-assistant[bot] 562d867483
chore: Bundle 2026-W7 (#26214)
Signed-off-by: Oleg Ivaniv <me@olegivaniv.com>
Co-authored-by: Tomi Turtiainen <10324676+tomi@users.noreply.github.com>
Co-authored-by: yehorkardash <yehor.kardash@n8n.io>
Co-authored-by: James Gee <1285296+geemanjs@users.noreply.github.com>
Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Stephen Wright <sjw948@gmail.com>
Co-authored-by: oleg <me@olegivaniv.com>
Co-authored-by: Albert Alises <albert.alises@gmail.com>
Co-authored-by: Danny Martini <danny@n8n.io>
2026-02-25 07:08:58 +00:00

66 lines
2.0 KiB
TypeScript

import basicAuth from 'basic-auth';
import type { ICredentialDataDecryptedObject, IWebhookFunctions } from 'n8n-workflow';
import { ChatTriggerAuthorizationError } from './error';
import type { AuthenticationChatOption } from './types';
export async function validateAuth(context: IWebhookFunctions) {
const authentication = context.getNodeParameter(
'authentication',
'none',
) as AuthenticationChatOption;
const req = context.getRequestObject();
const headers = context.getHeaderData();
if (authentication === 'none') {
return;
} else if (authentication === 'basicAuth') {
// Basic authorization is needed to call webhook
let expectedAuth: ICredentialDataDecryptedObject | undefined;
try {
expectedAuth = await context.getCredentials<ICredentialDataDecryptedObject>('httpBasicAuth');
} catch {}
if (expectedAuth === undefined || !expectedAuth.user || !expectedAuth.password) {
// Data is not defined on node so can not authenticate
throw new ChatTriggerAuthorizationError(500, 'No authentication data defined on node!');
}
const providedAuth = basicAuth(req);
// Authorization data is missing
if (!providedAuth) throw new ChatTriggerAuthorizationError(401);
if (providedAuth.name !== expectedAuth.user || providedAuth.pass !== expectedAuth.password) {
// Provided authentication data is wrong
throw new ChatTriggerAuthorizationError(403);
}
} else if (authentication === 'n8nUserAuth') {
const webhookName = context.getWebhookName();
if (webhookName !== 'setup') {
function getCookie(name: string) {
const value = `; ${headers.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) {
return parts.pop()?.split(';').shift();
}
return '';
}
const authCookie = getCookie('n8n-auth');
if (!authCookie) {
throw new ChatTriggerAuthorizationError(401, 'User not authenticated!');
}
try {
await context.validateCookieAuth(authCookie);
} catch {
throw new ChatTriggerAuthorizationError(401, 'Invalid authentication token');
}
}
}
return;
}