Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N8N-3270 Fix issues with trailing slashes in Mautic Node #3080

Merged
merged 3 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
{
Expand Down
39 changes: 36 additions & 3 deletions packages/nodes-base/nodes/Mautic/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 });
}
Expand Down Expand Up @@ -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);
}
29 changes: 29 additions & 0 deletions packages/nodes-base/nodes/Mautic/Mautic.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import {
} from 'n8n-core';

import {
ICredentialDataDecryptedObject,
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
ILoadOptionsFunctions,
INodeCredentialTestResult,
INodeExecutionData,
INodePropertyOptions,
INodeType,
Expand All @@ -17,6 +21,7 @@ import {
import {
mauticApiRequest,
mauticApiRequestAllItems,
validateCredentials,
validateJSON,
} from './GenericFunctions';

Expand Down Expand Up @@ -80,6 +85,7 @@ export class Mautic implements INodeType {
],
},
},
testedBy: 'mauticCredentialTest',
},
{
name: 'mauticOAuth2Api',
Expand Down Expand Up @@ -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
Expand Down