1
1
mirror of https://github.com/n8n-io/n8n.git synced 2024-11-10 12:35:46 +03:00

feat: Add more credentials tests (#3668)

*   Add injection to notion,
Add test to notion in cred

* 🔥 Remove unuse method

* 🎨  Move testing from node file to cred file

*  Add injection and testing in facebook graph

* Add cred injec with testing

* Add Cred injection and cred test

* Add cred injection, and cred testing for typeform, fix issue in clickup

* Add cred injection, move testing inside creds

* Add cred injection and cred testing to SendGrid

* Add cred injection and cred testing to woocommerce

* Add cred injection, add cred test to gitlab

* 🔥 Fix duplicated imports in Mautic cred

* 🔥 removed unused credentials testing in node

* Add cred injection, cred testing, handles slash trailing for Grafana node

* Add cred injection,  cred testing to shopify

* Add cred injection , add cred testing to stripe

* changed cred injection, add testing to cred for mattermost

* add cred injection and testing for dropbox

* Add cred injection, cred testing to webflow

*  Add cred injection and cred test to nocodb

*  Add cred injection, cred testing to mailchimp

* 🐛 fix a bug In credentials testing

*  Add cred injection, cred testing to sms77

*  Add cred injection, cred testing to ActiveCampaign

* Add cred injection, cred testing to TheHive

*  Add cred injection, add cred testing to ApiTemplateio

*  Add cred injection, add cred testing for zoom

*  Add cred injection, cred testing to rocketchat

*  Add cred injection, add cred test to getResponse

* 🔥 Remove useless authentcate creds and testing from facebookGraphApp

* 🔥 Remove useless imports in FacebookGrappApp credentials file

* 🔥 Removed useless imports and if statement

* 🐛 Add version to header when testing cred

Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: Jan Oberhauser <janober@users.noreply.github.com>
This commit is contained in:
agobrech 2022-07-15 16:20:41 +02:00 committed by GitHub
parent 945e25a77c
commit 683d2dfc98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
52 changed files with 387 additions and 254 deletions

View File

@ -1,4 +1,6 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -22,4 +24,18 @@ export class ActiveCampaignApi implements ICredentialType {
default: '',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
'Api-Token': '={{$credentials.apiKey}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials.apiUrl}}',
url: '/api/3/fields',
},
};
}

View File

@ -1,4 +1,6 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -15,4 +17,18 @@ export class ApiTemplateIoApi implements ICredentialType {
default: '',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
'X-API-KEY': '={{$credentials.apiKey}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: 'https://api.apitemplate.io/v1',
url: '/list-templates',
},
};
}

View File

@ -1,4 +1,6 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -15,4 +17,19 @@ export class ClickUpApi implements ICredentialType {
default: '',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '={{$credentials.accessToken}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: 'https://api.clickup.com/api/v2',
url: '/team',
},
};
}

View File

@ -1,6 +1,9 @@
import {
IAuthenticateGeneric,
ICredentialTestFunction,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
INodeProperties
} from 'n8n-workflow';
export class DropboxApi implements ICredentialType {
@ -31,4 +34,20 @@ export class DropboxApi implements ICredentialType {
default: 'full',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.accessToken}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: 'https://api.dropboxapi.com/2',
url: '/users/get_current_account',
method: 'POST',
},
};
}

View File

@ -1,4 +1,6 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -16,4 +18,18 @@ export class FacebookGraphApi implements ICredentialType {
default: '',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
qs: {
access_token: '={{$credentials.accessToken}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: 'https://graph.facebook.com/v8.0',
url: '/me',
},
};
}

View File

@ -1,4 +1,6 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -15,4 +17,18 @@ export class GetResponseApi implements ICredentialType {
default: '',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
'X-Auth-Token': '=api-key {{$credentials.apiKey}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: 'https://api.getresponse.com/v3',
url: '/campaigns',
},
};
}

View File

@ -1,4 +1,6 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -22,4 +24,19 @@ export class GitlabApi implements ICredentialType {
default: '',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
'Private-Token': '={{$credentials.accessToken}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials.server.replace(new RegExp("/$"), "") + "/api/v4" }}',
url: '/users',
},
};
}

View File

@ -1,4 +1,6 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -25,4 +27,19 @@ export class GrafanaApi implements ICredentialType {
required: true,
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.apiKey}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials.baseUrl.replace(new RegExp("/$"), "") + "/api" }}',
url: '/folders',
},
};
}

View File

@ -1,4 +1,7 @@
import {
IAuthenticateGeneric,
ICredentialTestFunctions,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -15,4 +18,18 @@ export class MailchimpApi implements ICredentialType {
default: '',
},
];
authenticate: IAuthenticateGeneric = {
type:'generic',
properties:{
headers:{
Authorization: '=apikey {{$credentials.apiKey}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: '=https://{{$credentials.apiKey.split("-").pop()}}.api.mailchimp.com/3.0',
url: '/lists',
},
};
}

View File

@ -1,5 +1,7 @@
import {
IAuthenticateGeneric,
ICredentialDataDecryptedObject,
ICredentialTestRequest,
ICredentialType,
IHttpRequestOptions,
INodeProperties,
@ -24,8 +26,18 @@ export class MattermostApi implements ICredentialType {
default: '',
},
];
async authenticate(credentials: ICredentialDataDecryptedObject, requestOptions: IHttpRequestOptions): Promise<IHttpRequestOptions> {
requestOptions.headers!['Authorization'] = `Bearer ${credentials.accessToken}`;
return requestOptions;
}
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.accessToken}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials.baseUrl}}/api/v4',
url: '/users',
},
};
}

View File

@ -1,6 +1,9 @@
import {
ICredentialType,
INodeProperties,
ICredentialDataDecryptedObject,
ICredentialTestRequest,
ICredentialType,
IHttpRequestOptions,
INodeProperties,
} from 'n8n-workflow';
export class MauticApi implements ICredentialType {
@ -31,4 +34,28 @@ export class MauticApi implements ICredentialType {
default: '',
},
];
async authenticate (credentials: ICredentialDataDecryptedObject, requestOptions: IHttpRequestOptions): Promise<IHttpRequestOptions> {
const {
url,
username,
password,
} = credentials as {
url: string,
username: string,
password: string,
};
const credentialUrl = url.endsWith('/') ? `${url}api/users/self` : `${url}/api/users/self`;
const base64Key = Buffer.from(`${username}:${password}`).toString('base64');
requestOptions.headers!['Authorization'] = `Basic ${base64Key}`;
requestOptions.url = credentialUrl ? credentialUrl : requestOptions.url;
return requestOptions;
}
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials.url.replace(new RegExp("/$"), "")}}',
url: '/api/users/self',
},
};
}

View File

@ -1,4 +1,6 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -23,4 +25,18 @@ export class NocoDb implements ICredentialType {
placeholder: 'http(s)://localhost:8080',
},
];
authenticate: IAuthenticateGeneric = {
type:'generic',
properties: {
headers:{
'xc-auth': '={{credentials.apiToken}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials.host}}',
url: '/lists',
},
};
}

View File

@ -28,6 +28,7 @@ export class NotionApi implements ICredentialType {
properties: {
headers: {
'Authorization': '=Bearer {{$credentials.apiKey}}',
'Notion-Version': '2021-05-13',
},
},
};

View File

@ -1,4 +1,6 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -29,4 +31,19 @@ export class RocketchatApi implements ICredentialType {
placeholder: 'https://n8n.rocket.chat',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
'X-Auth-Token': '={{$credentials.authKey}}',
'X-User-Id': '={{$credentials.userId}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials.domain}}',
url: '/api/v1/webdav.getMyAccounts',
},
};
}

View File

@ -1,5 +1,6 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -16,12 +17,19 @@ export class SendGridApi implements ICredentialType {
default: '',
},
];
authenticate = {
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.apiKey}}',
},
},
} as IAuthenticateGeneric;
};
test: ICredentialTestRequest = {
request: {
baseURL: 'https://api.sendgrid.com/v3',
url: '/marketing/contacts',
},
};
}

View File

@ -1,5 +1,10 @@
import { BINARY_ENCODING } from 'n8n-core';
import {
IAuthenticateGeneric,
ICredentialDataDecryptedObject,
ICredentialTestRequest,
ICredentialType,
IHttpRequestOptions,
INodeProperties,
} from 'n8n-workflow';
@ -37,4 +42,18 @@ export class ShopifyApi implements ICredentialType {
default: '',
},
];
async authenticate(credentials: ICredentialDataDecryptedObject, requestOptions: IHttpRequestOptions): Promise<IHttpRequestOptions> {
requestOptions.headers = {
...requestOptions.headers,
Authorization: `Basic ${Buffer.from(`${credentials.apiKey}:${credentials.password}`).toString(BINARY_ENCODING)}`,
};
return requestOptions;
}
test: ICredentialTestRequest = {
request: {
baseURL: '=https://{{$credentials.shopSubdomain}}.myshopify.com/admin/api/2019-10',
url: '/products.json',
},
};
}

View File

@ -1,4 +1,6 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -15,4 +17,18 @@ export class Sms77Api implements ICredentialType {
default: '',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
'X-Api-Key': '={{$credentials.apiKey}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: 'https://gateway.sms77.io/api',
url: '/balance',
},
};
}

View File

@ -1,4 +1,6 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -9,9 +11,6 @@ export class StripeApi implements ICredentialType {
displayName = 'Stripe API';
documentationUrl = 'stripe';
properties: INodeProperties[] = [
// The credentials to get from user and save encrypted.
// Properties can be defined exactly in the same way
// as node properties.
{
displayName: 'Secret Key',
name: 'secretKey',
@ -19,4 +18,19 @@ export class StripeApi implements ICredentialType {
default: '',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.secretKey}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: 'https://api.stripe.com/v1',
url: '/charges',
json: true,
},
};
}

View File

@ -18,11 +18,11 @@ export class TelegramApi implements ICredentialType {
description: 'Chat with the <a href="https://telegram.me/botfather">bot father</a> to obtain the access token',
},
];
test: ICredentialTestRequest = {
request: {
baseURL: `=https://api.telegram.org/bot{{$credentials?.accessToken}}`,
baseURL: '=https://api.telegram.org/bot{{$credentials.accessToken}}',
url: '/getMe',
method: 'GET',
},
};
}

View File

@ -1,4 +1,6 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -49,4 +51,18 @@ export class TheHiveApi implements ICredentialType {
default: false,
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.ApiKey}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials.url}}',
url: '/api/alert',
},
};
}

View File

@ -1,4 +1,6 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -16,4 +18,19 @@ export class TypeformApi implements ICredentialType {
default: '',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '=bearer {{$credentials.accessToken}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: 'https://api.typeform.com',
url: '/forms',
},
};
}

View File

@ -18,7 +18,7 @@ export class UrlScanIoApi implements ICredentialType {
required: true,
},
];
authenticate = {
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {

View File

@ -20,8 +20,8 @@ export class WebflowApi implements ICredentialType {
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
'Authorization': '=Bearer {{$credentials.accessToken}}',
headers:{
Authorization: '=Bearer {{$credentials.accessToken}}',
},
},
};

View File

@ -1,5 +1,8 @@
import {
ICredentialDataDecryptedObject,
ICredentialTestRequest,
ICredentialType,
IHttpRequestOptions,
INodeProperties,
} from 'n8n-workflow';
@ -35,4 +38,22 @@ export class WooCommerceApi implements ICredentialType {
description: 'Whether credentials should be included in the query. Occasionally, some servers may not parse the Authorization header correctly (if you see a “Consumer key is missing” error when authenticating over SSL, you have a server issue). In this case, you may provide the consumer key/secret as query string parameters instead.',
},
];
async authenticate(credentials: ICredentialDataDecryptedObject, requestOptions: IHttpRequestOptions): Promise<IHttpRequestOptions> {
requestOptions.auth = {
// @ts-ignore
user: credentials.consumerKey as string,
password: credentials.consumerSecret as string,
};
if (credentials.includeCredentialsInQuery === true) {
delete requestOptions.auth;
Object.assign(requestOptions.qs, { consumer_key: credentials.consumerKey, consumer_secret: credentials.consumerSecret });
}
return requestOptions;
}
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials.url}}/wp-json/wc/v3',
url: '/products/categories',
},
};
}

View File

@ -1,4 +1,4 @@
import { ICredentialType, INodeProperties } from 'n8n-workflow';
import { IAuthenticateGeneric, ICredentialTestRequest, ICredentialType, INodeProperties } from 'n8n-workflow';
export class ZoomApi implements ICredentialType {
name = 'zoomApi';
@ -12,4 +12,18 @@ export class ZoomApi implements ICredentialType {
default: '',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.accessToken}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: 'https://api.zoom.us/v2',
url: '/users/me',
},
};
}

View File

@ -34,7 +34,6 @@ export async function activeCampaignApiRequest(this: IHookFunctions | IExecuteFu
const options: OptionsWithUri = {
headers: {
'Api-Token': credentials.apiKey,
},
method,
qs: query,
@ -47,7 +46,7 @@ export async function activeCampaignApiRequest(this: IHookFunctions | IExecuteFu
}
try {
const responseData = await this.helpers.request!(options);
const responseData = await this.helpers.requestWithAuthentication.call(this, 'activeCampaignApi',options);
if (responseData.success === false) {
throw new NodeApiError(this.getNode(), responseData);

View File

@ -15,13 +15,10 @@ export async function apiTemplateIoApiRequest(
qs = {},
body = {},
) {
const { apiKey } = await this.getCredentials('apiTemplateIoApi') as { apiKey: string };
const options: OptionsWithUri = {
headers: {
'user-agent': 'n8n',
Accept: 'application/json',
'X-API-KEY': `${apiKey}`,
},
uri: `https://api.apitemplate.io/v1${endpoint}`,
method,
@ -41,7 +38,7 @@ export async function apiTemplateIoApiRequest(
}
try {
const response = await this.helpers.request!(options);
const response = await this.helpers.requestWithAuthentication.call(this, 'apiTemplateIoApi',options);
if (response.status === 'error') {
throw new NodeApiError(this.getNode(), response.message);
}

View File

@ -33,11 +33,7 @@ export async function clickupApiRequest(this: IHookFunctions | IExecuteFunctions
const authenticationMethod = this.getNodeParameter('authentication', 0) as string;
if (authenticationMethod === 'accessToken') {
const credentials = await this.getCredentials('clickUpApi');
options.headers!['Authorization'] = credentials.accessToken;
return await this.helpers.request!(options);
return await this.helpers.requestWithAuthentication.call(this, 'clickUpApi', options);
} else {
const oAuth2Options: IOAuth2Options = {

View File

@ -41,12 +41,7 @@ export async function dropboxApiRequest(this: IHookFunctions | IExecuteFunctions
try {
if (authenticationMethod === 'accessToken') {
const credentials = await this.getCredentials('dropboxApi') as IDataObject;
options.headers!['Authorization'] = `Bearer ${credentials.accessToken}`;
return await this.helpers.request(options);
return await this.helpers.requestWithAuthentication.call(this,'dropboxApi',options);
} else {
return await this.helpers.requestOAuth2.call(this, 'dropboxOAuth2Api', options);
}

View File

@ -34,10 +34,7 @@ export async function getresponseApiRequest(this: IWebhookFunctions | IHookFunct
}
if (authentication === 'apiKey') {
const credentials = await this.getCredentials('getResponseApi');
options!.headers!['X-Auth-Token'] = `api-key ${credentials.apiKey}`;
//@ts-ignore
return await this.helpers.request.call(this, options);
return await this.helpers.requestWithAuthentication.call(this, 'getResponseApi', options);
} else {
//@ts-ignore
return await this.helpers.requestOAuth2.call(this, 'getResponseOAuth2Api', options);

View File

@ -1,7 +1,6 @@
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
@ -40,12 +39,8 @@ export async function gitlabApiRequest(this: IHookFunctions | IExecuteFunctions,
try {
if (authenticationMethod === 'accessToken') {
const credentials = await this.getCredentials('gitlabApi');
options.headers!['Private-Token'] = `${credentials.accessToken}`;
options.uri = `${(credentials.server as string).replace(/\/$/, '')}/api/v4${endpoint}`;
return await this.helpers.request(options);
return await this.helpers.requestWithAuthentication.call(this, 'gitlabApi', options);
} else {
const credentials = await this.getCredentials('gitlabOAuth2Api');

View File

@ -25,7 +25,6 @@ export async function grafanaApiRequest(
qs: IDataObject = {},
) {
const {
apiKey,
baseUrl: rawBaseUrl,
} = await this.getCredentials('grafanaApi') as GrafanaCredentials;
@ -33,7 +32,6 @@ export async function grafanaApiRequest(
const options: OptionsWithUri = {
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
method,
@ -52,7 +50,7 @@ export async function grafanaApiRequest(
}
try {
return await this.helpers.request!(options);
return await this.helpers.requestWithAuthentication.call(this, 'grafanaApi', options);
} catch (error) {
if (error?.response?.data?.message === 'Team member not found') {
error.response.data.message += '. Are you sure the user is a member of this team?';
@ -108,4 +106,4 @@ export function deriveUid(this: IExecuteFunctions, uidOrUrl: string) {
}
return uid;
}
}

View File

@ -19,7 +19,6 @@ import {
deriveUid,
grafanaApiRequest,
throwOnEmptyUpdate,
tolerateTrailingSlash,
} from './GenericFunctions';
import {
@ -33,14 +32,9 @@ import {
userOperations,
} from './descriptions';
import {
OptionsWithUri,
} from 'request';
import {
DashboardUpdateFields,
DashboardUpdatePayload,
GrafanaCredentials,
LoadedDashboards,
LoadedFolders,
LoadedTeams,
@ -65,7 +59,6 @@ export class Grafana implements INodeType {
{
name: 'grafanaApi',
required: true,
testedBy: 'grafanaApiTest',
},
],
properties: [
@ -129,38 +122,6 @@ export class Grafana implements INodeType {
return users.map(({ userId, email }) => ({ value: userId, name: email }));
},
},
credentialTest: {
async grafanaApiTest(
this: ICredentialTestFunctions,
credential: ICredentialsDecrypted,
): Promise<INodeCredentialTestResult> {
const { apiKey, baseUrl: rawBaseUrl } = credential.data as GrafanaCredentials;
const baseUrl = tolerateTrailingSlash(rawBaseUrl);
const options: OptionsWithUri = {
headers: {
Authorization: `Bearer ${apiKey}`,
},
method: 'GET',
uri: `${baseUrl}/api/folders`,
json: true,
};
try {
await this.helpers.request(options);
return {
status: 'OK',
message: 'Authentication successful',
};
} catch (error) {
return {
status: 'Error',
message: error.message,
};
}
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {

View File

@ -36,17 +36,12 @@ export async function mailchimpApiRequest(this: IHookFunctions | IExecuteFunctio
try {
if (authenticationMethod === 'apiKey') {
const credentials = await this.getCredentials('mailchimpApi');
options.headers = Object.assign({}, headers, { Authorization: `apikey ${credentials.apiKey}` });
if (!(credentials.apiKey as string).includes('-')) {
throw new NodeOperationError(this.getNode(), 'The API key is not valid!');
}
const datacenter = (credentials.apiKey as string).split('-').pop();
options.url = `https://${datacenter}.${host}${endpoint}`;
return await this.helpers.request!(options);
return await this.helpers.requestWithAuthentication.call(this, 'mailchimpApi', options);
} else {
const credentials = await this.getCredentials('mailchimpOAuth2Api');

View File

@ -1,7 +1,3 @@
import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
@ -10,12 +6,11 @@ import {
} from 'n8n-core';
import {
ICredentialDataDecryptedObject,
ICredentialTestFunctions,
IDataObject,
JsonObject,
NodeApiError,
} from 'n8n-workflow';
import { OptionsWithUri } from 'request';
export async function mauticApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query?: IDataObject, uri?: string): Promise<any> { // tslint:disable-line:no-any
const authenticationMethod = this.getNodeParameter('authentication', 0, 'credentials') as string;
@ -34,17 +29,8 @@ export async function mauticApiRequest(this: IHookFunctions | IExecuteFunctions
let returnData;
if (authenticationMethod === 'credentials') {
const credentials = await this.getCredentials('mauticApi');
const baseUrl = credentials.url as string;
const base64Key = Buffer.from(`${credentials.username}:${credentials.password}`).toString('base64');
options.headers!.Authorization = `Basic ${base64Key}`;
options.uri = `${baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl}${options.uri}`;
//@ts-ignore
returnData = await this.helpers.request(options);
returnData = await this.helpers.httpRequestWithAuthentication.call(this, 'mauticApi', options);
} else {
const credentials = await this.getCredentials('mauticOAuth2Api');
const baseUrl = credentials.url as string;
@ -102,30 +88,3 @@ export function validateJSON(json: string | undefined): any { // tslint:disable-
}
return result;
}
export async function validateCredentials(this: ICredentialTestFunctions, decryptedCredentials: ICredentialDataDecryptedObject): Promise<any> { // tslint:disable-line:no-any
const credentials = decryptedCredentials;
const {
url,
username,
password,
} = credentials as {
url: string,
username: string,
password: string,
};
const base64Key = Buffer.from(`${username}:${password}`).toString('base64');
const options: OptionsWithUri = {
method: 'GET',
headers: {
Authorization: `Basic ${base64Key}`,
},
uri: url.endsWith('/') ? `${url}api/users/self` : `${url}/api/users/self`,
json: true,
};
return await this.helpers.request(options);
}

View File

@ -3,12 +3,8 @@ import {
} from 'n8n-core';
import {
ICredentialDataDecryptedObject,
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
ILoadOptionsFunctions,
INodeCredentialTestResult,
INodeExecutionData,
INodePropertyOptions,
INodeType,
@ -21,7 +17,6 @@ import {
import {
mauticApiRequest,
mauticApiRequestAllItems,
validateCredentials,
validateJSON,
} from './GenericFunctions';
@ -85,7 +80,6 @@ export class Mautic implements INodeType {
],
},
},
testedBy: 'mauticCredentialTest',
},
{
name: 'mauticOAuth2Api',
@ -171,29 +165,6 @@ export class Mautic implements INodeType {
};
methods = {
credentialTest: {
async mauticCredentialTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise<INodeCredentialTestResult> {
try {
let responseData;
responseData = await validateCredentials.call(this, credential.data as ICredentialDataDecryptedObject);
if (responseData.id) {
return {
status: 'OK',
message: 'Authentication successful!',
};
}
} catch (error) {
return {
status: 'Error',
message: 'Invalid credentials',
};
}
return {
status: 'Error',
message: 'Invalid credentials',
};
},
},
loadOptions: {
// Get all the available companies to display them to user so that he can
// select them easily

View File

@ -39,9 +39,6 @@ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoa
query = query || {};
const options: OptionsWithUri = {
headers: {
'xc-auth': credentials.apiToken,
},
method,
body,
qs: query,
@ -59,7 +56,7 @@ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoa
}
try {
return await this.helpers.request!(options);
return await this.helpers.requestWithAuthentication.call(this, 'nocodbApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}

View File

@ -709,19 +709,6 @@ export function getConditions() {
return elements;
}
export function validateCredentials(this: ICredentialTestFunctions, credentials: ICredentialDataDecryptedObject) {
const options: OptionsWithUri = {
headers: {
'Authorization': `Bearer ${credentials.apiKey}`,
'Notion-Version': apiVersion[2],
},
method: 'GET',
uri: `https://api.notion.com/v1/users/me`,
json: true,
};
return this.helpers.request!(options);
}
// tslint:disable-next-line: no-any
export async function downloadFiles(this: IExecuteFunctions | IPollFunctions, records: [{ properties: { [key: string]: any | { id: string, type: string, files: [{ external: { url: string } } | { file: { url: string } }] } } }]): Promise<INodeExecutionData[]> {
const elements: INodeExecutionData[] = [];

View File

@ -35,7 +35,6 @@ export class NotionTrigger implements INodeType {
{
name: 'notionApi',
required: true,
testedBy: 'notionApiCredentialTest',
},
],
polling: true,

View File

@ -30,7 +30,6 @@ import {
notionApiRequest,
notionApiRequestAllItems,
simplifyObjects,
validateCredentials,
validateJSON,
} from '../GenericFunctions';
@ -195,23 +194,6 @@ export class NotionV2 implements INodeType {
return returnData;
},
},
credentialTest: {
async notionApiCredentialTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise<INodeCredentialTestResult> {
try {
await validateCredentials.call(this, credential.data as ICredentialDataDecryptedObject);
} catch (error) {
return {
status: 'Error',
message: 'The security token included in the request is invalid',
};
}
return {
status: 'OK',
message: 'Connection successful!',
};
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {

View File

@ -46,7 +46,6 @@ export const versionDescription: INodeTypeDescription = {
{
name: 'notionApi',
required: true,
testedBy: 'notionApiCredentialTest',
// displayOptions: {
// show: {
// authentication: [

View File

@ -12,15 +12,8 @@ import { NodeApiError, NodeOperationError, } from 'n8n-workflow';
export async function rocketchatApiRequest(this: IExecuteFunctions | ILoadOptionsFunctions, resource: string, method: string, operation: string, body: any = {}, headers?: object): Promise<any> { // tslint:disable-line:no-any
const credentials = await this.getCredentials('rocketchatApi');
const headerWithAuthentication = Object.assign({}, headers,
{
'X-Auth-Token': credentials.authKey,
'X-User-Id': credentials.userId,
},
);
const options: OptionsWithUri = {
headers: headerWithAuthentication,
headers,
method,
body,
uri: `${credentials.domain}/api/v1${resource}.${operation}`,

View File

@ -14,7 +14,6 @@ import {
} from 'n8n-workflow';
export async function sendGridApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, endpoint: string, method: string, body: any = {}, qs: IDataObject = {}, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const host = 'api.sendgrid.com/v3';
const options: OptionsWithUri = {
@ -34,7 +33,6 @@ export async function sendGridApiRequest(this: IHookFunctions | IExecuteFunction
}
try {
//@ts-ignore
return await this.helpers.requestWithAuthentication.call(this, 'sendGridApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);

View File

@ -19,7 +19,6 @@ import {
} from 'change-case';
export async function shopifyApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const authenticationMethod = this.getNodeParameter('authentication', 0, 'oAuth2') as string;
let credentials;
@ -28,11 +27,9 @@ export async function shopifyApiRequest(this: IHookFunctions | IExecuteFunctions
if (authenticationMethod === 'apiKey') {
credentials = await this.getCredentials('shopifyApi');
credentialType = 'shopifyApi';
} else if (authenticationMethod === 'accessToken') {
credentials = await this.getCredentials('shopifyAccessTokenApi');
credentialType = 'shopifyAccessTokenApi';
} else {
credentials = await this.getCredentials('shopifyOAuth2Api');
}

View File

@ -23,12 +23,9 @@ import {
* @returns {Promise<any>}
*/
export async function sms77ApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: IDataObject, qs: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = await this.getCredentials('sms77Api');
const options: OptionsWithUri = {
headers: {
SentWith: 'n8n',
'X-Api-Key': credentials.apiKey,
},
qs,
uri: `https://gateway.sms77.io/api${endpoint}`,
@ -41,7 +38,7 @@ export async function sms77ApiRequest(this: IHookFunctions | IExecuteFunctions,
body.json = 1;
}
const response = await this.helpers.request(options);
const response = await this.helpers.requestWithAuthentication.call(this, 'sms77Api', options);
if (response.success !== '100') {
throw new NodeApiError(this.getNode(), response, { message: 'Invalid sms77 credentials or API error!' });

View File

@ -36,13 +36,9 @@ export async function stripeApiRequest(
body: object,
query?: object,
) {
const credentials = await this.getCredentials('stripeApi');
const options = {
method,
auth: {
user: credentials.secretKey as string,
},
form: body,
qs: query,
uri: `https://api.stripe.com/v1${endpoint}`,
@ -54,7 +50,7 @@ export async function stripeApiRequest(
}
try {
return await this.helpers.request!.call(this, options);
return await this.helpers.requestWithAuthentication.call(this, 'stripeApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}

View File

@ -18,10 +18,7 @@ import { Eq } from './QueryFunctions';
export async function theHiveApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = await this.getCredentials('theHiveApi');
const headerWithAuthentication = Object.assign({}, { Authorization: `Bearer ${credentials.ApiKey}` });
let options: OptionsWithUri = {
headers: headerWithAuthentication,
method,
qs: query,
uri: uri || `${credentials.url}/api${resource}`,
@ -42,7 +39,7 @@ export async function theHiveApiRequest(this: IHookFunctions | IExecuteFunctions
delete options.qs;
}
try {
return await this.helpers.request!(options);
return await this.helpers.requestWithAuthentication.call(this, 'theHiveApi',options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}

View File

@ -64,12 +64,7 @@ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoa
try {
if (authenticationMethod === 'accessToken') {
const credentials = await this.getCredentials('typeformApi');
options.headers!['Authorization'] = `bearer ${credentials.accessToken}`;
return await this.helpers.request!(options);
return await this.helpers.requestWithAuthentication.call(this, 'typeformApi',options);
} else {
return await this.helpers.requestOAuth2!.call(this, 'typeformOAuth2Api', options);
}

View File

@ -18,7 +18,6 @@ export async function urlScanIoApiRequest(
body: IDataObject = {},
qs: IDataObject = {},
) {
const options: OptionsWithUri = {
method,
body,
@ -36,7 +35,7 @@ export async function urlScanIoApiRequest(
}
try {
return await this.helpers.request(options);
return await this.helpers.requestWithAuthentication.call(this, 'urlScanIoApi',options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}

View File

@ -3,10 +3,7 @@ import {
} from 'n8n-core';
import {
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
INodeCredentialTestResult,
INodeExecutionData,
INodeType,
INodeTypeDescription,

View File

@ -40,27 +40,19 @@ export async function woocommerceApiRequest(this: IHookFunctions | IExecuteFunct
const credentials = await this.getCredentials('wooCommerceApi');
let options: OptionsWithUri = {
auth: {
user: credentials.consumerKey as string,
password: credentials.consumerSecret as string,
},
method,
qs,
body,
uri: uri || `${credentials.url}/wp-json/wc/v3${resource}`,
json: true,
};
if (credentials.includeCredentialsInQuery === true) {
delete options.auth;
Object.assign(qs, { consumer_key: credentials.consumerKey, consumer_secret: credentials.consumerSecret });
}
if (!Object.keys(body).length) {
delete options.form;
}
options = Object.assign({}, options, option);
try {
return await this.helpers.request!(options);
return await this.helpers.requestWithAuthentication.call(this,'wooCommerceApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}

View File

@ -36,11 +36,7 @@ export async function zoomApiRequest(this: IExecuteFunctions | IExecuteSingleFun
try {
if (authenticationMethod === 'accessToken') {
const credentials = await this.getCredentials('zoomApi');
options.headers!.Authorization = `Bearer ${credentials.accessToken}`;
//@ts-ignore
return await this.helpers.request(options);
return await this.helpers.requestWithAuthentication.call(this,'zoomApi',options);
} else {
//@ts-ignore
return await this.helpers.requestOAuth2.call(this, 'zoomOAuth2Api', options);