2020-01-25 23:48:38 -08:00
|
|
|
import {
|
|
|
|
Credentials,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
ICredentialDataDecryptedObject,
|
2021-01-24 04:33:57 -08:00
|
|
|
ICredentialsExpressionResolveValues,
|
2020-01-25 23:48:38 -08:00
|
|
|
ICredentialsHelper,
|
2020-09-12 03:16:07 -07:00
|
|
|
INode,
|
2020-05-14 05:27:19 -07:00
|
|
|
INodeParameters,
|
2020-05-16 10:05:40 -07:00
|
|
|
INodeProperties,
|
2020-09-12 03:16:07 -07:00
|
|
|
INodeType,
|
|
|
|
INodeTypeData,
|
2020-10-22 06:46:03 -07:00
|
|
|
INodeTypes,
|
2020-05-14 05:27:19 -07:00
|
|
|
NodeHelpers,
|
2020-09-12 03:16:07 -07:00
|
|
|
Workflow,
|
2020-01-25 23:48:38 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
CredentialsOverwrites,
|
2020-05-14 05:27:19 -07:00
|
|
|
CredentialTypes,
|
2020-01-25 23:48:38 -08:00
|
|
|
Db,
|
|
|
|
ICredentialsDb,
|
|
|
|
} from './';
|
|
|
|
|
|
|
|
|
2020-09-12 03:16:07 -07:00
|
|
|
const mockNodeTypes: INodeTypes = {
|
|
|
|
nodeTypes: {},
|
|
|
|
init: async (nodeTypes?: INodeTypeData): Promise<void> => { },
|
|
|
|
getAll: (): INodeType[] => {
|
|
|
|
// Does not get used in Workflow so no need to return it
|
|
|
|
return [];
|
|
|
|
},
|
|
|
|
getByName: (nodeType: string): INodeType | undefined => {
|
|
|
|
return undefined;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-01-25 23:48:38 -08:00
|
|
|
export class CredentialsHelper extends ICredentialsHelper {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the credentials instance
|
|
|
|
*
|
|
|
|
* @param {string} name Name of the credentials to return instance of
|
|
|
|
* @param {string} type Type of the credentials to return instance of
|
|
|
|
* @returns {Credentials}
|
|
|
|
* @memberof CredentialsHelper
|
|
|
|
*/
|
|
|
|
getCredentials(name: string, type: string): Credentials {
|
|
|
|
if (!this.workflowCredentials[type]) {
|
|
|
|
throw new Error(`No credentials of type "${type}" exist.`);
|
|
|
|
}
|
|
|
|
if (!this.workflowCredentials[type][name]) {
|
|
|
|
throw new Error(`No credentials with name "${name}" exist for type "${type}".`);
|
|
|
|
}
|
|
|
|
const credentialData = this.workflowCredentials[type][name];
|
|
|
|
|
|
|
|
return new Credentials(credentialData.name, credentialData.type, credentialData.nodesAccess, credentialData.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-16 10:05:40 -07:00
|
|
|
/**
|
|
|
|
* Returns all the properties of the credentials with the given name
|
|
|
|
*
|
|
|
|
* @param {string} type The name of the type to return credentials off
|
|
|
|
* @returns {INodeProperties[]}
|
|
|
|
* @memberof CredentialsHelper
|
|
|
|
*/
|
|
|
|
getCredentialsProperties(type: string): INodeProperties[] {
|
|
|
|
const credentialTypes = CredentialTypes();
|
|
|
|
const credentialTypeData = credentialTypes.getByName(type);
|
|
|
|
|
|
|
|
if (credentialTypeData === undefined) {
|
|
|
|
throw new Error(`The credentials of type "${type}" are not known.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (credentialTypeData.extends === undefined) {
|
|
|
|
return credentialTypeData.properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
const combineProperties = [] as INodeProperties[];
|
|
|
|
for (const credentialsTypeName of credentialTypeData.extends) {
|
|
|
|
const mergeCredentialProperties = this.getCredentialsProperties(credentialsTypeName);
|
|
|
|
NodeHelpers.mergeNodeProperties(combineProperties, mergeCredentialProperties);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The properties defined on the parent credentials take presidence
|
|
|
|
NodeHelpers.mergeNodeProperties(combineProperties, credentialTypeData.properties);
|
|
|
|
|
|
|
|
return combineProperties;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-25 23:48:38 -08:00
|
|
|
/**
|
|
|
|
* Returns the decrypted credential data with applied overwrites
|
|
|
|
*
|
|
|
|
* @param {string} name Name of the credentials to return data of
|
|
|
|
* @param {string} type Type of the credentials to return data of
|
2020-05-14 05:27:19 -07:00
|
|
|
* @param {boolean} [raw] Return the data as supplied without defaults or overwrites
|
2020-01-25 23:48:38 -08:00
|
|
|
* @returns {ICredentialDataDecryptedObject}
|
|
|
|
* @memberof CredentialsHelper
|
|
|
|
*/
|
2021-01-24 04:33:57 -08:00
|
|
|
getDecrypted(name: string, type: string, raw?: boolean, expressionResolveValues?: ICredentialsExpressionResolveValues): ICredentialDataDecryptedObject {
|
2020-01-25 23:48:38 -08:00
|
|
|
const credentials = this.getCredentials(name, type);
|
|
|
|
|
2020-05-14 05:27:19 -07:00
|
|
|
const decryptedDataOriginal = credentials.getData(this.encryptionKey);
|
|
|
|
|
|
|
|
if (raw === true) {
|
|
|
|
return decryptedDataOriginal;
|
|
|
|
}
|
|
|
|
|
2021-01-24 04:33:57 -08:00
|
|
|
return this.applyDefaultsAndOverwrites(decryptedDataOriginal, type, expressionResolveValues);
|
2020-05-14 05:27:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies credential default data and overwrites
|
|
|
|
*
|
|
|
|
* @param {ICredentialDataDecryptedObject} decryptedDataOriginal The credential data to overwrite data on
|
|
|
|
* @param {string} type Type of the credentials to overwrite data of
|
|
|
|
* @returns {ICredentialDataDecryptedObject}
|
|
|
|
* @memberof CredentialsHelper
|
|
|
|
*/
|
2021-01-24 04:33:57 -08:00
|
|
|
applyDefaultsAndOverwrites(decryptedDataOriginal: ICredentialDataDecryptedObject, type: string, expressionResolveValues?: ICredentialsExpressionResolveValues): ICredentialDataDecryptedObject {
|
2020-05-16 10:05:40 -07:00
|
|
|
const credentialsProperties = this.getCredentialsProperties(type);
|
2020-05-14 05:27:19 -07:00
|
|
|
|
|
|
|
// Add the default credential values
|
2020-09-12 03:16:07 -07:00
|
|
|
let decryptedData = NodeHelpers.getNodeParameters(credentialsProperties, decryptedDataOriginal as INodeParameters, true, false) as ICredentialDataDecryptedObject;
|
2020-05-14 05:27:19 -07:00
|
|
|
|
|
|
|
if (decryptedDataOriginal.oauthTokenData !== undefined) {
|
|
|
|
// The OAuth data gets removed as it is not defined specifically as a parameter
|
|
|
|
// on the credentials so add it back in case it was set
|
|
|
|
decryptedData.oauthTokenData = decryptedDataOriginal.oauthTokenData;
|
|
|
|
}
|
|
|
|
|
2021-01-24 04:33:57 -08:00
|
|
|
if (expressionResolveValues) {
|
|
|
|
try {
|
2021-01-26 07:52:35 -08:00
|
|
|
const workflow = new Workflow({ nodes: Object.values(expressionResolveValues.workflow.nodes), connections: expressionResolveValues.workflow.connectionsBySourceNode, active: false, nodeTypes: expressionResolveValues.workflow.nodeTypes });
|
2021-01-27 00:02:20 -08:00
|
|
|
decryptedData = workflow.expression.getParameterValue(decryptedData as INodeParameters, expressionResolveValues.runExecutionData, expressionResolveValues.runIndex, expressionResolveValues.itemIndex, expressionResolveValues.node.name, expressionResolveValues.connectionInputData, false, decryptedData) as ICredentialDataDecryptedObject;
|
2021-01-24 04:33:57 -08:00
|
|
|
} catch (e) {
|
|
|
|
e.message += ' [Error resolving credentials]';
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const node = {
|
|
|
|
name: '',
|
|
|
|
typeVersion: 1,
|
|
|
|
type: 'mock',
|
|
|
|
position: [0, 0],
|
2021-01-27 00:02:20 -08:00
|
|
|
parameters: {} as INodeParameters,
|
2021-01-24 04:33:57 -08:00
|
|
|
} as INode;
|
|
|
|
|
|
|
|
const workflow = new Workflow({ nodes: [node!], connections: {}, active: false, nodeTypes: mockNodeTypes });
|
|
|
|
|
|
|
|
// Resolve expressions if any are set
|
2021-01-27 00:02:20 -08:00
|
|
|
decryptedData = workflow.expression.getComplexParameterValue(node!, decryptedData as INodeParameters, undefined, decryptedData) as ICredentialDataDecryptedObject;
|
2021-01-24 04:33:57 -08:00
|
|
|
}
|
2020-09-12 03:16:07 -07:00
|
|
|
|
2020-01-25 23:48:38 -08:00
|
|
|
// Load and apply the credentials overwrites if any exist
|
|
|
|
const credentialsOverwrites = CredentialsOverwrites();
|
2020-05-14 05:27:19 -07:00
|
|
|
return credentialsOverwrites.applyOverwrite(type, decryptedData);
|
2020-01-25 23:48:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates credentials in the database
|
|
|
|
*
|
|
|
|
* @param {string} name Name of the credentials to set data of
|
|
|
|
* @param {string} type Type of the credentials to set data of
|
|
|
|
* @param {ICredentialDataDecryptedObject} data The data to set
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
* @memberof CredentialsHelper
|
|
|
|
*/
|
|
|
|
async updateCredentials(name: string, type: string, data: ICredentialDataDecryptedObject): Promise<void> {
|
|
|
|
const credentials = await this.getCredentials(name, type);
|
|
|
|
|
2020-02-08 21:25:46 -08:00
|
|
|
if (Db.collections!.Credentials === null) {
|
|
|
|
// The first time executeWorkflow gets called the Database has
|
|
|
|
// to get initialized first
|
|
|
|
await Db.init();
|
|
|
|
}
|
|
|
|
|
2020-01-25 23:48:38 -08:00
|
|
|
credentials.setData(data, this.encryptionKey);
|
|
|
|
const newCredentialsData = credentials.getDataToSave() as ICredentialsDb;
|
|
|
|
|
|
|
|
// Add special database related data
|
|
|
|
newCredentialsData.updatedAt = new Date();
|
|
|
|
|
|
|
|
// TODO: also add user automatically depending on who is logged in, if anybody is logged in
|
|
|
|
|
|
|
|
// Save the credentials in DB
|
2020-02-08 21:25:46 -08:00
|
|
|
const findQuery = {
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
};
|
|
|
|
|
|
|
|
await Db.collections.Credentials!.update(findQuery, newCredentialsData);
|
2020-01-25 23:48:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|