* Endpoint to preset credentials

*  Endpoint to preset credentials

*  Improvements

* 🐛 Small fix

* 🐛 Small fix
This commit is contained in:
Ricardo Espinoza
2020-07-07 13:03:53 -04:00
committed by GitHub
parent b1ee1efcb1
commit 8aff042e04
3 changed files with 62 additions and 10 deletions

View File

@@ -58,6 +58,9 @@ import {
WorkflowExecuteAdditionalData,
WorkflowRunner,
GenericHelpers,
CredentialsOverwrites,
ICredentialsOverwrite,
LoadNodesAndCredentials,
} from './';
import {
@@ -105,6 +108,7 @@ class App {
testWebhooks: TestWebhooks.TestWebhooks;
endpointWebhook: string;
endpointWebhookTest: string;
endpointPresetCredentials: string;
externalHooks: IExternalHooksClass;
saveDataErrorExecution: string;
saveDataSuccessExecution: string;
@@ -119,6 +123,8 @@ class App {
sslKey: string;
sslCert: string;
presetCredentialsLoaded: boolean;
constructor() {
this.app = express();
@@ -141,6 +147,9 @@ class App {
this.sslCert = config.get('ssl_cert');
this.externalHooks = ExternalHooks();
this.presetCredentialsLoaded = false;
this.endpointPresetCredentials = config.get('credentials.overwrite.endpoint') as string;
}
@@ -1650,6 +1659,40 @@ class App {
});
if (this.endpointPresetCredentials !== '') {
// POST endpoint to set preset credentials
this.app.post(`/${this.endpointPresetCredentials}`, async (req: express.Request, res: express.Response) => {
if (this.presetCredentialsLoaded === false) {
const body = req.body as ICredentialsOverwrite;
if (req.headers['content-type'] !== 'application/json') {
ResponseHelper.sendErrorResponse(res, new Error('Body must be a valid JSON, make sure the content-type is application/json'));
return;
}
const loadNodesAndCredentials = LoadNodesAndCredentials();
const credentialsOverwrites = CredentialsOverwrites();
await credentialsOverwrites.init(body);
const credentialTypes = CredentialTypes();
await credentialTypes.init(loadNodesAndCredentials.credentialTypes);
this.presetCredentialsLoaded = true;
ResponseHelper.sendSuccessResponse(res, { success: true }, true, 200);
} else {
ResponseHelper.sendErrorResponse(res, new Error('Preset credentials can be set once'));
}
});
}
// Serve the website
const startTime = (new Date()).toUTCString();
const editorUiPath = require.resolve('n8n-editor-ui');