UNPKG

n8n

Version:

n8n Workflow Automation Tool

63 lines 2.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AgentsCredentialProvider = void 0; function toResolvedCredential(data) { const resolved = data !== null && typeof data === 'object' && !Array.isArray(data) ? data : {}; const apiKey = 'apiKey' in resolved && typeof resolved.apiKey === 'string' ? resolved.apiKey : ''; return { ...resolved, apiKey }; } function findCredential(credentials, credentialIdOrName) { return (credentials.find((c) => c.id === credentialIdOrName) ?? credentials.find((c) => c.name.toLowerCase() === credentialIdOrName.toLowerCase()) ?? null); } class AgentsCredentialProvider { constructor(credentialsService, projectId, user) { this.credentialsService = credentialsService; this.projectId = projectId; this.user = user; } async resolve(credentialIdOrName) { const credential = await this.findCredentialEntity(credentialIdOrName); if (!credential) { throw new Error(`Credential "${credentialIdOrName}" not found or not accessible`); } const data = await this.credentialsService.decrypt(credential, true); return toResolvedCredential(data); } async list() { if (this.user) { const workflowCredentials = await this.credentialsService.getCredentialsAUserCanUseInAWorkflow(this.user, { projectId: this.projectId, }); return workflowCredentials.map((c) => ({ id: c.id, name: c.name, type: c.type, })); } const projectCredentials = await this.credentialsService.findAllCredentialIdsForProject(this.projectId); return projectCredentials.map((c) => ({ id: c.id, name: c.name, type: c.type, })); } async findCredentialEntity(credentialIdOrName) { if (!this.user) { const projectCredentials = await this.credentialsService.findAllCredentialIdsForProject(this.projectId); return findCredential(projectCredentials, credentialIdOrName); } const scopedCredential = findCredential(await this.list(), credentialIdOrName); if (!scopedCredential) return null; const projectCredentials = await this.credentialsService.findAllCredentialIdsForProject(this.projectId); const projectCredential = projectCredentials.find((c) => c.id === scopedCredential.id); if (projectCredential) return projectCredential; const globalCredentials = await this.credentialsService.findAllGlobalCredentialIds(true); return globalCredentials.find((c) => c.id === scopedCredential.id) ?? null; } } exports.AgentsCredentialProvider = AgentsCredentialProvider; //# sourceMappingURL=agents-credential-provider.js.map