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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 5 deletions

View file

@ -22,14 +22,14 @@ export class MauticOAuth2Api implements ICredentialType {
displayName: 'Authorization URL',
name: 'authUrl',
type: 'hidden',
default: '={{$self["url"]}}/oauth/v2/authorize',
default: '={{$self["url"].endsWith("/") ? $self["url"].slice(0, -1) : $self["url"]}}/oauth/v2/authorize',
required: true,
},
{
displayName: 'Access Token URL',
name: 'accessTokenUrl',
type: 'hidden',
default: '={{$self["url"]}}/oauth/v2/token',
default: '={{$self["url"].endsWith("/") ? $self["url"].slice(0, -1) : $self["url"]}}/oauth/v2/token',
required: true,
},
{

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);
}

View file

@ -3,8 +3,12 @@ import {
} from 'n8n-core';
import {
ICredentialDataDecryptedObject,
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
ILoadOptionsFunctions,
INodeCredentialTestResult,
INodeExecutionData,
INodePropertyOptions,
INodeType,
@ -17,6 +21,7 @@ import {
import {
mauticApiRequest,
mauticApiRequestAllItems,
validateCredentials,
validateJSON,
} from './GenericFunctions';
@ -80,6 +85,7 @@ export class Mautic implements INodeType {
],
},
},
testedBy: 'mauticCredentialTest',
},
{
name: 'mauticOAuth2Api',
@ -166,6 +172,29 @@ 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