Skip to content

Commit

Permalink
⚡️ adjust credentials test route to also allow testing for sharees (#…
Browse files Browse the repository at this point in the history
…3999)

* ⚡️ pull data if user is sharee

* fix: Removed sharedWith and ownedBy from credentialData on testing credentials.

Co-authored-by: Alex Grozav <alex@grozav.com>
  • Loading branch information
BHesseldieck and alexgrozav authored Sep 3, 2022
1 parent d22c314 commit b6ef41a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
38 changes: 36 additions & 2 deletions packages/cli/src/credentials/credentials.controller.ee.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable import/no-cycle */
import express from 'express';
import { LoggerProxy } from 'n8n-workflow';
import { INodeCredentialTestResult, LoggerProxy } from 'n8n-workflow';
import { Db, ResponseHelper } from '..';
import type { CredentialsEntity } from '../databases/entities/CredentialsEntity';

Expand Down Expand Up @@ -99,7 +99,41 @@ EECredentialsController.get(
);

/**
* (EE) POST /credentials/:id/share
* POST /credentials/test
*
* Test if a credential is valid.
*/
EECredentialsController.post(
'/test',
ResponseHelper.send(async (req: CredentialRequest.Test): Promise<INodeCredentialTestResult> => {
const { credentials, nodeToTestWith } = req.body;

const encryptionKey = await EECredentials.getEncryptionKey();

if (
!credentials.data ||
// @ts-ignore
!Object.keys(credentials.data).every((key) => !!credentials.data[key])
) {
const sharing = await EECredentials.getSharing(req.user, credentials.id);
if (!sharing) {
throw new ResponseHelper.ResponseError(
`Credential with ID "${credentials.id}" could not be found.`,
undefined,
404,
);
}

const decryptedData = await EECredentials.decrypt(encryptionKey, sharing.credentials);
Object.assign(credentials, { data: decryptedData });
}

return EECredentials.test(req.user, encryptionKey, credentials, nodeToTestWith);
}),
);

/**
* (EE) PUT /credentials/:id/share
*
* Grant or remove users' access to a credential.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,11 +657,12 @@ export default mixins(showMessage, nodeHelpers).extend({
(access) => !!access,
) as ICredentialNodeAccess[];
const { ownedBy, sharedWith, ...credentialData } = this.credentialData;
const details: ICredentialsDecrypted = {
id: this.credentialId,
name: this.credentialName,
type: this.credentialTypeName!,
data: this.credentialData,
data: credentialData,
nodesAccess,
};
Expand Down

0 comments on commit b6ef41a

Please sign in to comment.