mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-21 02:56:40 -08:00
* ✨ Endpoint to preset credentials
* ✨ Endpoint to preset credentials * ⚡ Improvements * 🐛 Small fix * 🐛 Small fix
This commit is contained in:
parent
b1ee1efcb1
commit
8aff042e04
|
@ -128,15 +128,23 @@ const config = convict({
|
|||
|
||||
credentials: {
|
||||
overwrite: {
|
||||
// Allows to set default values for credentials which
|
||||
// get automatically prefilled and the user does not get
|
||||
// displayed and can not change.
|
||||
// Format: { CREDENTIAL_NAME: { PARAMTER: VALUE }}
|
||||
doc: 'Overwrites for credentials',
|
||||
format: '*',
|
||||
default: '{}',
|
||||
env: 'CREDENTIALS_OVERWRITE'
|
||||
}
|
||||
data: {
|
||||
// Allows to set default values for credentials which
|
||||
// get automatically prefilled and the user does not get
|
||||
// displayed and can not change.
|
||||
// Format: { CREDENTIAL_NAME: { PARAMTER: VALUE }}
|
||||
doc: 'Overwrites for credentials',
|
||||
format: '*',
|
||||
default: '{}',
|
||||
env: 'CREDENTIALS_OVERWRITE_DATA'
|
||||
},
|
||||
endpoint: {
|
||||
doc: 'Fetch credentials from API',
|
||||
format: String,
|
||||
default: '',
|
||||
env: 'CREDENTIALS_OVERWRITE_ENDPOINT',
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
executions: {
|
||||
|
|
|
@ -20,7 +20,7 @@ class CredentialsOverwritesClass {
|
|||
return;
|
||||
}
|
||||
|
||||
const data = await GenericHelpers.getConfigValue('credentials.overwrite') as string;
|
||||
const data = await GenericHelpers.getConfigValue('credentials.overwrite.data') as string;
|
||||
|
||||
try {
|
||||
this.overwriteData = JSON.parse(data);
|
||||
|
@ -30,6 +30,7 @@ class CredentialsOverwritesClass {
|
|||
}
|
||||
|
||||
applyOverwrite(type: string, data: ICredentialDataDecryptedObject) {
|
||||
|
||||
const overwrites = this.get(type);
|
||||
|
||||
if (overwrites === undefined) {
|
||||
|
|
|
@ -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');
|
||||
|
|
Loading…
Reference in a new issue