This change ensures that things like `encryptionKey` and `instanceId` are always available directly where they are needed, instead of passing them around throughout the code.
71 lines
1.7 KiB
TypeScript
71 lines
1.7 KiB
TypeScript
import { Container } from 'typedi';
|
|
import type { ICredentialDataDecryptedObject, ICredentialsEncrypted } from 'n8n-workflow';
|
|
import { ICredentials, jsonParse } from 'n8n-workflow';
|
|
import { Cipher } from './Cipher';
|
|
|
|
export class Credentials extends ICredentials {
|
|
private readonly cipher = Container.get(Cipher);
|
|
|
|
/**
|
|
* Returns if the given nodeType has access to data
|
|
*/
|
|
hasNodeAccess(nodeType: string): boolean {
|
|
for (const accessData of this.nodesAccess) {
|
|
if (accessData.nodeType === nodeType) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Sets new credential object
|
|
*/
|
|
setData(data: ICredentialDataDecryptedObject): void {
|
|
this.data = this.cipher.encrypt(data);
|
|
}
|
|
|
|
/**
|
|
* Returns the decrypted credential object
|
|
*/
|
|
getData(nodeType?: string): ICredentialDataDecryptedObject {
|
|
if (nodeType && !this.hasNodeAccess(nodeType)) {
|
|
throw new Error(
|
|
`The node of type "${nodeType}" does not have access to credentials "${this.name}" of type "${this.type}".`,
|
|
);
|
|
}
|
|
|
|
if (this.data === undefined) {
|
|
throw new Error('No data is set so nothing can be returned.');
|
|
}
|
|
|
|
const decryptedData = this.cipher.decrypt(this.data);
|
|
|
|
try {
|
|
return jsonParse(decryptedData);
|
|
} catch (e) {
|
|
throw new Error(
|
|
'Credentials could not be decrypted. The likely reason is that a different "encryptionKey" was used to encrypt the data.',
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the encrypted credentials to be saved
|
|
*/
|
|
getDataToSave(): ICredentialsEncrypted {
|
|
if (this.data === undefined) {
|
|
throw new Error('No credentials were set to save.');
|
|
}
|
|
|
|
return {
|
|
id: this.id,
|
|
name: this.name,
|
|
type: this.type,
|
|
data: this.data,
|
|
nodesAccess: this.nodesAccess,
|
|
};
|
|
}
|
|
}
|