feat(Mautic Node): Add credential test and allow trailing slash in host (#3080)

* Updated Mautic to stop trailing slashes from causing an issue

* Fixed oauth failing when there is a trailing slash in the mautic host

* Added credential test
This commit is contained in:
Jonathan Bennetts
2022-04-01 16:31:43 +01:00
committed by GitHub
parent 984f62df9e
commit 0a75539cc3
3 changed files with 67 additions and 5 deletions

View File

@@ -10,7 +10,11 @@ import {
} from 'n8n-core';
import {
IDataObject, JsonObject, NodeApiError,
ICredentialDataDecryptedObject,
ICredentialTestFunctions,
IDataObject,
JsonObject,
NodeApiError,
} from 'n8n-workflow';
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
@@ -31,19 +35,21 @@ export async function mauticApiRequest(this: IHookFunctions | IExecuteFunctions
if (authenticationMethod === 'credentials') {
const credentials = await this.getCredentials('mauticApi') as IDataObject;
const baseUrl = credentials!.url as string;
const base64Key = Buffer.from(`${credentials.username}:${credentials.password}`).toString('base64');
options.headers!.Authorization = `Basic ${base64Key}`;
options.uri = `${credentials.url}${options.uri}`;
options.uri = `${baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl}${options.uri}`;
//@ts-ignore
returnData = await this.helpers.request(options);
} else {
const credentials = await this.getCredentials('mauticOAuth2Api') as IDataObject;
const baseUrl = credentials!.url as string;
options.uri = `${credentials.url}${options.uri}`;
options.uri = `${baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl}${options.uri}`;
//@ts-ignore
returnData = await this.helpers.requestOAuth2.call(this, 'mauticOAuth2Api', options, { includeCredentialsOnRefreshOnBody: true });
}
@@ -96,3 +102,30 @@ 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);
}