🐛 Fix issue with credentials which extend others

This commit is contained in:
Jan Oberhauser
2020-05-16 19:05:40 +02:00
parent 97cf7da6c3
commit 95097a8bd7
5 changed files with 131 additions and 36 deletions

View File

@@ -6,6 +6,7 @@ import {
ICredentialDataDecryptedObject,
ICredentialsHelper,
INodeParameters,
INodeProperties,
NodeHelpers,
} from 'n8n-workflow';
@@ -40,6 +41,38 @@ export class CredentialsHelper extends ICredentialsHelper {
}
/**
* 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;
}
/**
* Returns the decrypted credential data with applied overwrites
*
@@ -71,15 +104,10 @@ export class CredentialsHelper extends ICredentialsHelper {
* @memberof CredentialsHelper
*/
applyDefaultsAndOverwrites(decryptedDataOriginal: ICredentialDataDecryptedObject, type: string): ICredentialDataDecryptedObject {
const credentialTypes = CredentialTypes();
const credentialType = credentialTypes.getByName(type);
if (credentialType === undefined) {
throw new Error(`The credential type "${type}" is not known and can so not be decrypted.`);
}
const credentialsProperties = this.getCredentialsProperties(type);
// Add the default credential values
const decryptedData = NodeHelpers.getNodeParameters(credentialType.properties, decryptedDataOriginal as INodeParameters, true, false) as ICredentialDataDecryptedObject;
const decryptedData = NodeHelpers.getNodeParameters(credentialsProperties, decryptedDataOriginal as INodeParameters, true, false) as ICredentialDataDecryptedObject;
if (decryptedDataOriginal.oauthTokenData !== undefined) {
// The OAuth data gets removed as it is not defined specifically as a parameter