n8n
Version:
n8n Workflow Automation Tool
91 lines • 3.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateCredentialReferences = validateCredentialReferences;
const not_found_error_1 = require("../../../../errors/response-errors/not-found.error");
const fail = (opIndex, message) => ({
ok: false,
opIndex,
error: `Operation ${opIndex} failed: ${message}`,
});
async function validateCredentialReferences(operations, existingWorkflow, user, credentialsService, nodeTypes) {
const nameToNodeMeta = new Map();
for (const node of existingWorkflow.nodes) {
nameToNodeMeta.set(node.name, { type: node.type, typeVersion: node.typeVersion });
}
const credentialCache = new Map();
const lookupCredential = async (credentialId) => {
const cached = credentialCache.get(credentialId);
if (cached)
return cached;
try {
const credential = await credentialsService.getOne(user, credentialId, false);
const result = { type: credential.type };
credentialCache.set(credentialId, result);
return result;
}
catch (error) {
if (error instanceof not_found_error_1.NotFoundError) {
credentialCache.set(credentialId, 'not-found');
return 'not-found';
}
throw error;
}
};
const checkCredentialReference = async (opIndex, nodeMeta, credentialKey, credentialId) => {
let description;
try {
description = nodeTypes.getByNameAndVersion(nodeMeta.type, nodeMeta.typeVersion).description;
}
catch {
return null;
}
const accepted = description.credentials?.find((c) => c.name === credentialKey);
if (!accepted) {
return fail(opIndex, `node type '${nodeMeta.type}' does not accept credential '${credentialKey}'`);
}
const credential = await lookupCredential(credentialId);
if (credential === 'not-found') {
return fail(opIndex, `credential '${credentialId}' not found or not accessible`);
}
if (credential.type !== credentialKey) {
return fail(opIndex, `credential '${credentialId}' is type '${credential.type}' but '${credentialKey}' is expected`);
}
return null;
};
for (let i = 0; i < operations.length; i++) {
const op = operations[i];
if (op.type === 'addNode') {
const nodeMeta = { type: op.node.type, typeVersion: op.node.typeVersion };
if (op.node.credentials) {
for (const [key, value] of Object.entries(op.node.credentials)) {
if (!value.id)
continue;
const failure = await checkCredentialReference(i, nodeMeta, key, value.id);
if (failure)
return failure;
}
}
nameToNodeMeta.set(op.node.name, nodeMeta);
}
else if (op.type === 'renameNode') {
const meta = nameToNodeMeta.get(op.oldName);
if (meta) {
nameToNodeMeta.delete(op.oldName);
nameToNodeMeta.set(op.newName, meta);
}
}
else if (op.type === 'removeNode') {
nameToNodeMeta.delete(op.nodeName);
}
else if (op.type === 'setNodeCredential') {
const meta = nameToNodeMeta.get(op.nodeName);
if (!meta)
continue;
const failure = await checkCredentialReference(i, meta, op.credentialKey, op.credentialId);
if (failure)
return failure;
}
}
return { ok: true };
}
//# sourceMappingURL=credential-validation.js.map