n8n/packages/nodes-base/credentials/ElasticSecurityApi.credentials.ts
Bram Kn 26f1af397b
feat: Add new credentials for the HTTP Request node (#9833)
Co-authored-by: Jonathan Bennetts <jonathan.bennetts@gmail.com>
Co-authored-by: Shireen Missi <94372015+ShireenMissi@users.noreply.github.com>
2024-08-23 18:19:05 +01:00

112 lines
2.1 KiB
TypeScript

import type {
ICredentialDataDecryptedObject,
ICredentialTestRequest,
ICredentialType,
IHttpRequestOptions,
INodeProperties,
} from 'n8n-workflow';
export class ElasticSecurityApi implements ICredentialType {
name = 'elasticSecurityApi';
displayName = 'Elastic Security API';
documentationUrl = 'elasticSecurity';
properties: INodeProperties[] = [
{
displayName: 'Base URL',
name: 'baseUrl',
type: 'string',
default: '',
placeholder: 'e.g. https://mydeployment.kb.us-central1.gcp.cloud.es.io:9243',
description: "Referred to as Kibana 'endpoint' in the Elastic deployment dashboard",
required: true,
},
{
displayName: 'Type',
name: 'type',
type: 'options',
options: [
{
name: 'API Key',
value: 'apiKey',
},
{
name: 'Basic Auth',
value: 'basicAuth',
},
],
default: 'basicAuth',
},
{
displayName: 'Username',
name: 'username',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
type: ['basicAuth'],
},
},
},
{
displayName: 'Password',
name: 'password',
type: 'string',
typeOptions: {
password: true,
},
default: '',
required: true,
displayOptions: {
show: {
type: ['basicAuth'],
},
},
},
{
displayName: 'API Key',
name: 'apiKey',
required: true,
type: 'string',
typeOptions: { password: true },
default: '',
displayOptions: {
show: {
type: ['apiKey'],
},
},
},
];
async authenticate(
credentials: ICredentialDataDecryptedObject,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
if (credentials.type === 'apiKey') {
requestOptions.headers = {
Authorization: `ApiKey ${credentials.apiKey}`,
};
} else {
requestOptions.auth = {
username: credentials.username as string,
password: credentials.password as string,
};
requestOptions.headers = {
'kbn-xsrf': true,
};
}
return requestOptions;
}
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials.baseUrl}}',
url: '/api/endpoint/metadata',
method: 'GET',
},
};
}