mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 02:21:13 +00:00
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:
committed by
GitHub
parent
984f62df9e
commit
0a75539cc3
@@ -22,14 +22,14 @@ export class MauticOAuth2Api implements ICredentialType {
|
|||||||
displayName: 'Authorization URL',
|
displayName: 'Authorization URL',
|
||||||
name: 'authUrl',
|
name: 'authUrl',
|
||||||
type: 'hidden',
|
type: 'hidden',
|
||||||
default: '={{$self["url"]}}/oauth/v2/authorize',
|
default: '={{$self["url"].endsWith("/") ? $self["url"].slice(0, -1) : $self["url"]}}/oauth/v2/authorize',
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Access Token URL',
|
displayName: 'Access Token URL',
|
||||||
name: 'accessTokenUrl',
|
name: 'accessTokenUrl',
|
||||||
type: 'hidden',
|
type: 'hidden',
|
||||||
default: '={{$self["url"]}}/oauth/v2/token',
|
default: '={{$self["url"].endsWith("/") ? $self["url"].slice(0, -1) : $self["url"]}}/oauth/v2/token',
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -10,7 +10,11 @@ import {
|
|||||||
} from 'n8n-core';
|
} from 'n8n-core';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
IDataObject, JsonObject, NodeApiError,
|
ICredentialDataDecryptedObject,
|
||||||
|
ICredentialTestFunctions,
|
||||||
|
IDataObject,
|
||||||
|
JsonObject,
|
||||||
|
NodeApiError,
|
||||||
} from 'n8n-workflow';
|
} 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
|
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') {
|
if (authenticationMethod === 'credentials') {
|
||||||
const credentials = await this.getCredentials('mauticApi') as IDataObject;
|
const credentials = await this.getCredentials('mauticApi') as IDataObject;
|
||||||
|
const baseUrl = credentials!.url as string;
|
||||||
|
|
||||||
const base64Key = Buffer.from(`${credentials.username}:${credentials.password}`).toString('base64');
|
const base64Key = Buffer.from(`${credentials.username}:${credentials.password}`).toString('base64');
|
||||||
|
|
||||||
options.headers!.Authorization = `Basic ${base64Key}`;
|
options.headers!.Authorization = `Basic ${base64Key}`;
|
||||||
|
|
||||||
options.uri = `${credentials.url}${options.uri}`;
|
options.uri = `${baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl}${options.uri}`;
|
||||||
|
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
returnData = await this.helpers.request(options);
|
returnData = await this.helpers.request(options);
|
||||||
} else {
|
} else {
|
||||||
const credentials = await this.getCredentials('mauticOAuth2Api') as IDataObject;
|
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
|
//@ts-ignore
|
||||||
returnData = await this.helpers.requestOAuth2.call(this, 'mauticOAuth2Api', options, { includeCredentialsOnRefreshOnBody: true });
|
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;
|
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);
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,8 +3,12 @@ import {
|
|||||||
} from 'n8n-core';
|
} from 'n8n-core';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
ICredentialDataDecryptedObject,
|
||||||
|
ICredentialsDecrypted,
|
||||||
|
ICredentialTestFunctions,
|
||||||
IDataObject,
|
IDataObject,
|
||||||
ILoadOptionsFunctions,
|
ILoadOptionsFunctions,
|
||||||
|
INodeCredentialTestResult,
|
||||||
INodeExecutionData,
|
INodeExecutionData,
|
||||||
INodePropertyOptions,
|
INodePropertyOptions,
|
||||||
INodeType,
|
INodeType,
|
||||||
@@ -17,6 +21,7 @@ import {
|
|||||||
import {
|
import {
|
||||||
mauticApiRequest,
|
mauticApiRequest,
|
||||||
mauticApiRequestAllItems,
|
mauticApiRequestAllItems,
|
||||||
|
validateCredentials,
|
||||||
validateJSON,
|
validateJSON,
|
||||||
} from './GenericFunctions';
|
} from './GenericFunctions';
|
||||||
|
|
||||||
@@ -80,6 +85,7 @@ export class Mautic implements INodeType {
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
testedBy: 'mauticCredentialTest',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'mauticOAuth2Api',
|
name: 'mauticOAuth2Api',
|
||||||
@@ -166,6 +172,29 @@ export class Mautic implements INodeType {
|
|||||||
};
|
};
|
||||||
|
|
||||||
methods = {
|
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: {
|
loadOptions: {
|
||||||
// Get all the available companies to display them to user so that he can
|
// Get all the available companies to display them to user so that he can
|
||||||
// select them easily
|
// select them easily
|
||||||
|
|||||||
Reference in New Issue
Block a user